0

I try to insert values from a database into a 2d array in php, but the inner for loop does not work after the 1st iteration of the outer loop. Here is the code

for ($i=0; $i<$recCount; $i++) {            
            $row=mysqli_fetch_array($result, MYSQLI_ASSOC);             
            $orderNo[$i]        = $row['orderNo'];
            $orderDate[$i]      = $row['orderDate'];
            $subTotal[$i]       = $row['subTotal'];
            $discount[$i]       = $row['discount'];
            $deliveryCharge[$i] = $row['deliveryCharge'];
            $grandTotal[$i]     = $row['grandTotal'];           
            $k = 0;
            for ($j=0; $j<$recCount2; $j++){
                $row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);                 
                $orderNo2[$i][$j]           = $row2['orderNo'];
                echo "orderNo2[$i][$j]: " .$orderNo2[$i][$j]. "&nbsp";
                if ($orderNo2[$i][$j] == $orderNo[$i]){
                    /*$subNo($i,$k)         = $row2['subNo'];
                    $ItemNo($i,$k)          = $row2['ItemNo'];
                    $Description($i,$k) = $row2['Description'];
                    $Qty($i,$k)         = $row2['Qty'];         
                    $Price($i,$k)           = $row2['Price'];
                    $Amount($i,$k)          = $row2['Amount'];*/
                    $k++;
                }
            }

$recCount = 3, $orderNo[$i] has the value of 1, 2, 3. $recCount2 = 15, the $orderNo2[$i][$j] should have five 1s, five 2s, and five 3s for each $i, but the echoed result of orderNo2 is

orderNo2[0][0]: 1 orderNo2[0][1]: 1 orderNo2[0][2]: 1 orderNo2[0][3]: 1 orderNo2[0][4]: 1 orderNo2[0][5]: 2 orderNo2[0][6]: 2 orderNo2[0][7]: 2 orderNo2[0][8]: 2 orderNo2[0][9]: 2 orderNo2[0][10]: 3 orderNo2[0][11]: 3 orderNo2[0][12]: 3 orderNo2[0][13]: 3 orderNo2[0][14]: 3 orderNo2[1][0]: orderNo2[1][1]: orderNo2[1][2]: orderNo2[1][3]: orderNo2[1][4]: orderNo2[1][5]: orderNo2[1][6]: orderNo2[1][7]: orderNo2[1][8]: orderNo2[1][9]: orderNo2[1][10]: orderNo2[1][11]: orderNo2[1][12]: orderNo2[1][13]: orderNo2[1][14]: orderNo2[2][0]:enter code here orderNo2[2][1]: orderNo2[2][2]: orderNo2[2][3]: orderNo2[2][4]: orderNo2[2][5]: orderNo2[2][6]: orderNo2[2][7]: orderNo2[2][8]: orderNo2[2][9]: orderNo2[2][10]: orderNo2[2][11]: orderNo2[2][12]: orderNo2[2][13]: orderNo2[2][14]:

After $i = 1, the later ones are all empty. I don't know why the inner for loop does not work after the 1st outer for loop iteration

Jack
  • 1
  • 1
  • what's the value for $recCount2 ? – zam3858 Apr 29 '20 at 02:58
  • the value is 15, as the database has 15 rows of dataset – Jack Apr 29 '20 at 03:05
  • I suspect this is a case of not seeing `E_NOTICE` level errors. Please check [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – Phil Apr 29 '20 at 03:22
  • I have tried to add the codes from the link you provided into my file, but no error popped out, ini_set('display_errors', 'On'); ini_set('html_errors', 0); error_reporting(E_All); – Jack Apr 29 '20 at 03:34
  • @Jack, "the value is 15, as the database has 15 rows of dataset " is actually expectation/assumption based on what you were trying to do. when something like this happens, you have to really see what the value is so... var_dump($recCount2); exit; if it is not 15, as you expected. then find out why it is so. – zam3858 Apr 30 '20 at 04:14

0 Answers0