Welcome Guest, Not a member yet? Register   Sign In
Multi-Dimensional Array Problem
#1

[eluser]rochellecanale[/eluser]
hello guys i have a problem about displaying array.
Here's my sample function in my controller:
Code:
<?php
            foreach($product->result_array() as $row){
                
                echo "<tr>";
                    echo "<td>{$row['product_name']}</td>";
                    echo "<td>{$row['date_of_sales']}</td>";
                    echo "<td>{$row['qty_purchased']}</td>";
                    echo "<td>{$row['subtotal']}</td>";
                echo "</tr>";
                
                $product_id = $row['product_id']; //id
                $product_name = $row['product_name'];
                $date_of_sales = $row['date_of_sales'];
                $qty_purchased = $row['qty_purchased'];
                $subtotal = $row['subtotal'];
                
                $transact[$product_id]['id'] = $product_id;
                $transact[$product_id]['product_name'] = $product_name;
                $transact[$product_id]['date_of_sales'] = $date_of_sales;
                $transact[$product_id]['qty_purchased'] = $qty_purchased;
                $transact[$product_id]['subtotal'] = $subtotal;
                
                echo "<pre>";
                print_r($transact);
}

My problem is my array display only the last value per id. For example let's say we have the following result.
Code:
Product ID           Product Name     Qty      Total      
     1                product a        1         5.00  
     1                product a        2        10.00
     2                product b        1         5.00
     3                product c        1         8.00
     3                product c        2        16.00
     4                product d        1         5.00
     5                product e        1         5.00

My output when i try to get the value of array is like this:
Code:
Product ID           Product Name     Qty      Total      
     1                product a        2        10.00  //display last value of id 1
     2                product b        1         5.00
     3                product c        2        16.00 //display last value of id 3
     4                product d        1         5.00
     5                product e        1         5.00

The first id is not showing. What is the problem in my code? How can i display all the values?
#2

[eluser]LuckyFella73[/eluser]
Its because you use the product id as the array index.
If you have more than 1 row with the same product id
the array entry is overwritten.

What you can do is:
Code:
$i = 0;
foreach($product->result_array() as $row){
                
                echo "<tr>";
                    echo "<td>{$row['product_name']}</td>";
                    echo "<td>{$row['date_of_sales']}</td>";
                    echo "<td>{$row['qty_purchased']}</td>";
                    echo "<td>{$row['subtotal']}</td>";
                echo "</tr>";
                
                $product_id = $row['product_id']; //id
                $product_name = $row['product_name'];
                $date_of_sales = $row['date_of_sales'];
                $qty_purchased = $row['qty_purchased'];
                $subtotal = $row['subtotal'];
                
                $transact[$i]['id'] = $product_id;
                $transact[$i]['product_name'] = $product_name;
                $transact[$i]['date_of_sales'] = $date_of_sales;
                $transact[$i]['qty_purchased'] = $qty_purchased;
                $transact[$i]['subtotal'] = $subtotal;
                $i++;
                

}
echo "<pre>";
print_r($transact);
echo "</pre>";
#3

[eluser]rochellecanale[/eluser]
thanks i will try that.
#4

[eluser]rochellecanale[/eluser]
thanks it works!!
#5

[eluser]LuckyFella73[/eluser]
Nice to hear! Btw you can shorten your code:

Code:
&lt;?php
$i = 0;
foreach($product->result_array() as $row)
{
echo "<tr>";
echo "<td>{$row['product_name']}</td>";
echo "<td>{$row['date_of_sales']}</td>";
echo "<td>{$row['qty_purchased']}</td>";
echo "<td>{$row['subtotal']}</td>";
echo "</tr>";

// no need to assign the values to other variables before pushing them into your array

$transact[$i]['id'] = $row['product_id'];
$transact[$i]['product_name'] = $row['product_name'];
$transact[$i]['date_of_sales'] =  $row['date_of_sales'];
$transact[$i]['qty_purchased'] = $row['qty_purchased'];
$transact[$i]['subtotal'] = $row['subtotal'];
$i++;
}
?&gt;
#6

[eluser]rochellecanale[/eluser]
Ok. i'll try that also. I need this result in my PDF report file. Thanks again and hope that you can help more people like me that having a hard time in debugging codes. Smile




Theme © iAndrew 2016 - Forum software by © MyBB