Donavon Yelton
Donavon Yelton

Reputation: 1237

PHP loop for SQL query assistance requested

I have a bit of PHP code that's baffling me for some reason. I know that I need to iterate through the array and run a SQL query for each value in the array, but I'm getting lost of how to do that in my code:

    $kit_bin_count = 0;
    $kit_itemno_array = null;
    $kit_bin_array = null;

     // iterate through array
    while ($kit_row = odbc_fetch_array($result_ickitd)) {
      // store results from mysql in our own PHP array
      $kit_itemno_array[$kit_count] = $kit_row['COMPONENT'];
      // increment counter
      $kit_count++;
    }
    for($k=0;$k<sizeof($kit_itemno_array);$k++) {
        $var_kit_item = $kit_itemno_array[$k];
        $newvar_kit_item = trim($var_kit_item);
        }

/*  $params = "'".implode("','",$kit_itemno_array)."'";
    echo "$params"; */

/*  foreach ($kit_itemno_array as $country)
    {
        $query .= "OR stv.name = '{$country}', ";
    } */

    $sql_kit_bin="SELECT * FROM icitem, iciloc WHERE icitem.itemno = '$kit_itemno_array[$k]' AND iciloc.itemno = icitem.itemno";
    $result_kit_bin=odbc_exec($conn,$sql_kit_bin);
    while ($kit_bin_row = odbc_fetch_array($result_kit_bin)) {
        $kit_bin_array[$kit_bin_count] = $kit_bin_row['PICKINGSEQ'];
        $kit_bin_count++;
        print_r($kit_bin_array);
    }
    // END KITTING
        echo "<span style=\"color:#66CCFF\">";
        echo "$kit_bin_array[$k]";
        foreach ($kit_bin_array as $kit_bin) {
    //      if(strpos($file_array, $kit_item ) !== FALSE) {
            echo '<br />' . $kit_bin ; }
    //      else {
    //          $kit_post_array[] = $kit_item;
    //          echo '<br />' . 'KI: ' . $kit_item ; }
    //      }
    echo "</span>";

My code is returning the first value correctly and placing it in the $kit_item_array variable, but additional rows are getting lost. I'm unsure of how to loop this inside of my existing loop so everything is returned properly.

Here is a screenshot of the results, including a print_r() of the arrays being returned.

results

Upvotes: 0

Views: 123

Answers (1)

DaveRandom
DaveRandom

Reputation: 88697

If you indent your code properly, you will find it much easier to spot problems in your code. In your case, I suspect the problem lies in an extra } here:

for($k=0;$k<sizeof($kit_itemno_array);$k++) {
    $var_kit_item = $kit_itemno_array[$k];
    $newvar_kit_item = trim($var_kit_item);
    } // <<--- This closes the loop when I don't think you meant to

You code can be reduced down to one loop and a single query, I suspect (assuming you are using MySQL, as one of your comments suggests). I can;t give you the complete code because your first query is not shown, but lets for the purposes of this example say it is:

SELECT COMPONENT FROM items

If we make your second query, the one shown above, into:

SELECT *
FROM icitem t
JOIN iciloc l ON l.itemno = t.itemno
WHERE t.itemno IN (
  SELECT COMPONENT
  FROM items
)

...it pretty much renders all the code prior to that query pointless.

Upvotes: 2

Related Questions