Welcome Guest, Not a member yet? Register   Sign In
Model in View - MVC no loop?
#11

[eluser]Rick Jolly[/eluser]
Yeah, you can remove the "unset($products);". It's not necessary to unset a local variable in a method since it will be out of scope after the method returns anyway.
#12

[eluser]ywftdg[/eluser]
Ok, but one vague part to me are these array events:

Code:
$products = $query->result();
$result[] = array();


Code:
$result[] = $product;

So on first call, when we start the result array, its empty right? Then we start the foreach, and rename products to product (each row that is), and then slide each row back into the result array? I think this is what this is doing, but want to double check. Either way, thanks guys, this helps so much, I was really starting to get frustrated...
#13

[eluser]gon[/eluser]
If you made this:

Code:
$products = $query->result();

foreach ($products as $product) {
   $product->bundlesum = $this->get_bundlesumfilter($product->pSkulist);  
}

return $products;

bundlesum wouldn't be set on the returned array. That's beacause a foreach loop produces COPIES of the actual rows. So in every foreach iteration, you get a copy of every row assigned to $product. And you add a property to that copy, but it gets lost in the next iteration.

So the solution is to create a void array to which you add the copies of the rows after adding the new property.

That's why I was putting the & on the foreach. In PHP 5, that makes $product be a REFERENCE to the actual row, so adding a property to it modifies the real row. No need to make another array.

Regards
#14

[eluser]ywftdg[/eluser]
Ok makes sense now, so basically in my code, I don't send my first query's result directly at the end, just the copy of it that was put into the new array along with the new bundlesum row.
#15

[eluser]ywftdg[/eluser]
I have found the information above very helpful for future developments and looping through arrays. Although, I have found rebuilding some older code, which I was close to getting working, to be too much of a overhaul. In the end, I have to move on due to deadlines, since I have been on this for the entire work week, with no working example yet. I did the nastiness of just tweaking the old php code to work in my view file. Sometimes, time wins in the end. I thought I post my original code I was trying to port to CI, just to show how something like this, is very difficult to port over. I am sure most of this is because I am not the best coder really. but this was the first time I found CI to be more of a time waster, harder to use, than coding in my old fashion non framework methods. Either way, thanks for all the help guys, I will keep trekking on with CI, this in no way will stop my interest or progression with CI.

Old code snippet

Code:
<?
// Get designer's base payout percentage
$query = "select dpercentages from designer where ddesignerid = $DesignerId";
$result = mysql_query($query);
if($row = mysql_fetch_row($result)) {
    $BasePercentage = $row[0];
} else {
    $BasePercentage = 0;
}

$query = "select psku, pname, pcost, pprice, pskulist from products where pskulist <> ''";
$result = mysql_query($query);
$GrandTotal = 0;
$GrandQty = 0;
$GrandSales = 0;

while($row = mysql_fetch_row($result)) {

    $BundleTotal = 0;
    $BundleProfit = 0;

    // Get the non-discounted total for this bundle
    $query2 = "select sum(pprice) from products where psku in ($row[4])";
    $result2 = mysql_query($query2);
    if($row2 = mysql_fetch_row($result2)) {    $BaseBundlePrice = $row2[0];    } else {    $BaseBundlePrice = 0;    }

    //echo $BaseBundlePrice."<br>";
    
    $Discount = $row[3] / $BaseBundlePrice;

    $query2 = "select psku, pname, pcost, pprice from products where psku in ($row[4]) and pdesignerid = $DesignerId";
    $result2 = mysql_query($query2);
    
    if(mysql_num_rows($result2) > 0) {
    while($row2 = mysql_fetch_row($result2)) {

        $query3 = "select sum(oquantity) from orderdetail inner join receipt on oOrderNum = rOrderNum where osku = '$row[0]' and " . $query_date;
        $result3 = mysql_query($query3);
        if($row3 = mysql_fetch_row($result3)) {
            $BundleProfit += ($row2[3] * $Discount) * ($BasePercentage / 100);
            $BundleTotal += ((($row2[3] * $Discount) * $row3[0]) * ($BasePercentage / 100));
            }

    }

    $GrandQty += $row3[0];
    $GrandTotal += $BundleTotal;
    $GrandSales += $row[3] * $row3[0];
?&gt;
        <tr>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText">&lt;? echo $row[0]; ?&gt;</font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText">&lt;? echo $row[1]; ?&gt;</font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText">$&lt;? echo number_format($row[2] / 100, 2); ?&gt;</font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText">$&lt;? echo number_format($row[3] / 100, 2); ?&gt;</font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText">$&lt;? echo number_format(($BundleProfit / 100), 2); ?&gt;</font></td>
            <td nowrap bgcolor="#DDDDDD"><font class="blackText">&lt;? echo $row3[0]; ?&gt;</font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText">$&lt;? echo number_format((($row[3] * $row3[0]) / 100), 2); ?&gt;</font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText">$&lt;? echo number_format($BundleTotal / 100, 2); ?&gt;</font></td>
        </tr>
&lt;?
    }
}
?&gt;
        <TR>
            <TD colspan=5 nowrap>&nbsp;</TD>
            <TD nowrap><font class="lightText"><B>#Sold</B></font></TD>
            <TD nowrap><font class="lightText"><B>Sales Total</B></font></TD>
            <TD nowrap><font class="lightText"><B>Sales Profit</B></font></TD>
        </tr>
        <tr>
            <td colspan=5 nowrap bgcolor="#B9B9B9">&nbsp;</td>
            <td nowrap bgcolor="#DDDDDD"><font class="blackText"><b>&lt;? echo $GrandQty; ?&gt;</b></font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText"><b>$&lt;? echo number_format(($GrandSales / 100), 2); ?&gt;</b></font></td>
            <td nowrap bgcolor="#B9B9B9"><font class="blackText"><b>$&lt;? echo number_format($GrandTotal / 100, 2); ?&gt;</b></font></td>
        </tr>
    </table>
</p>




Theme © iAndrew 2016 - Forum software by © MyBB