0

Im having two foreach loops: one is having purchase info and another one is having sales info.

what i want to print like this, here one foreach loop will hold many items for purchase and another foreach loop will hold many items for sales. i want to loop one item in purchase and one item in sales, and so on...

can anyone please suggest me how to do that

//purchase infos
foreach($purchase_array as $row){
   echo "<tr>";
        echo "<td>purchase</td>";
        echo "<td>".$row['quantity']."</td>";
        echo "<td>".$row['total']."</td>";
      //if one item finished here it has to go to $sales_array foreach loop and come back to this loop after echoing one item there. so that it can be one row
}
foreach($sales_array as $row){
//sales infos
        echo "<td>sales</td>";
        echo "<td>".$row['bill_number']."</td>";
        echo "<td>".$row['quantity']."</td>";
        echo "<td>".$row['total']."</td>";
    echo "</tr>";
}

2 Answers2

1

I guess you need to loop and print one row per all the items so if this is the case you can try nested loops like this

//purchase infos
foreach($purchase_array as $row){
      echo "<tr>";
        echo "<td>purchase</td>";
        echo "<td>".$row['quantity']."</td>";
        echo "<td>".$row['total']."</td>";

        //if one item finished here it has to go to $sales_array foreach loop and come back to this loop after echoing one item there. so that it can be one row
    foreach($sales_array as $row){
        //sales infos
        echo "<td>sales</td>";
        echo "<td>".$row['bill_number']."</td>";
        echo "<td>".$row['quantity']."</td>";
        echo "<td>".$row['total']."</td>";    
    }

    echo "</tr>";
}
themhz
  • 8,335
  • 21
  • 84
  • 109
  • im having a situation here, what if purchase info array has only one value and sales array has more value. i want to print purchase in first half and sales in second half, then empty purchase first half and sales in second half –  May 13 '15 at 19:41
  • I think I understand. What is the relation between purchase and sales entities? is it 1:1 1:n? I think that it is ok to do a 2 nested loop or 3 but for your situation it seems to be a bad practice. Why not make an sql query with an inner join or something before you return all your values. That way you can use only one foreach. Is it possible? – themhz May 13 '15 at 19:46
  • i already have switch case checking 1) both array counts equal to one, 2) n:1 3) 1:n, i solved all the above now only this is pending. maybe it is a bad practice, cause im new. –  May 13 '15 at 19:50
  • I Think you need to clarify your question. If you are all ok with the above then what is the question? – themhz May 13 '15 at 19:55
  • "im having a situation here, what if purchase info array has only one value and sales array has more value". If $purchase_array has one value and sales more, then you will get more td columns referring to that particular sales next to the first one. Not sure if this is a good idea. If you have more sales per purchase which I find logically impossible then you should make a table displaying only the purchases and when you click on one purchase I would display the sales. – themhz May 13 '15 at 19:58
  • n:n is the problem, i want to print one row in first array and one row in second array... then again another row in first array and another row in second array –  May 13 '15 at 19:59
  • So the first purchase is associated with the first sale and the second purchase with the second sale? Could it be that the first purchase is associated with two sales? How do you know which purchase is associated with what sale? – themhz May 13 '15 at 20:05
  • there is a unique key, based on that unique key i fetch all results, so the result belong to only one product. there any many any number of row in sales, any number of row in purchase. –  May 14 '15 at 04:13
1

If you plan ahead for them to have identical keys, you would do this:

foreach (array_keys($purchase_array) as $index) {
    echo "<tr>";
    echo "<td>purchase</td>";
    echo "<td>".$purchase_array[$index]['quantity']."</td>";
    echo "<td>".$purchase_array[$index]['total']."</td>";
    echo "<td>sales</td>";
    echo "<td>".$sales_array[$index]['bill_number']."</td>";
    echo "<td>".$sales_array[$index]['quantity']."</td>";
    echo "<td>".$sales_array[$index]['total']."</td>";
    echo "</tr>";
}
mike.k
  • 3,277
  • 1
  • 12
  • 18