Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to run a query off multiple array variables and display the results in a table.

The user selects 1 or more records, which includes BOL and CONTAINER. These selections are put in their own arrays and they are always an equal amount.

 <?php
   $bolArray = explode(',', $_POST['BOL']);
   $containerArray = explode(',', $_POST['CONTAINER']);

   $count = count($bolArray);  // to get the total amount in the arrays

I use a FOR loop to separate each value from the 2 arrays:

   for($i = 0; $i < $count; $i++)
   {
     $bol = $bolArray[$i];
     $container = $containerArray[$i];
   }

Here is the part where I'm stuck and probably where I am messing up.

I need to take each variable from the FOR loop and run query using both variables.

First, I'll start the table:

 echo "<table><thead><tr><th>BOL</th><th>Container</th></thead><tbody>";

Here is where I tried a FOREACH loop:

 foreach($containerArray as $container) // I am not sure if I am using this FOREACH correctly
 {

And now, the query. Please take note of the variables from the first FOR loop:

   $preQuery = "SELECT * FROM mainTable WHERE CONTAINER = '".$container."' AND BOL = '".$bol."'";
   $preRes = mysql_query($preQuery) or die(mysql_error());
   $preNum = mysql_num_rows($preRes);

I use a WHILE loop with a mysql_fetch_assoc:

   while($preRow = mysql_fetch_assoc($preRes))
   {
     echo '<tr>'
     echo '<td>'.$preRow[BOL_NUMBER].'</td>';
     echo '<td>'.$preRow[CONTAINER_NUMBER].'</td>';
     echo '<td>'.$preRow[ANOTHER_COLUMN].'</td>';
     echo '</tr>'
   }
 }
 echo '</tbody></table>';

 ?>

The query actually works. Problem is, it only returns 1 record, and it's always the last record. The user could select 4 records, but only the last record is returned in the table.

I tried to use the same query and paste it inside the first FOR loop. I echoed out the query and it displayed the same amount of times as the number of array values, but will only return data for the last record.

I do not understand what I am doing wrong. I just want to display data for each value from the array.

Please help me fix this.

Thank you in advance.

EDIT

Here is what the code looks like when I throw the query in the first FOR loop

 echo "<table class='table table-bordered'><thead><tr><th>BOL</th><th>Container</th></tr></thead><tbody>";
 for($i = 0; $i < $count; $i++)
 {
  $bol = $bolArray[$i];
  $container = $containerArray[$i];

  $preQuery = "SELECT BOL_NUMBER, CONTAINER_NUMBER FROM `intermodal_main_view` WHERE BOL_NUMBER = '". $bol ."' AND CONTAINER_NUMBER = '".$container."'";
  $preRes = mysql_query($preQuery) or die();
  $preNum = mysql_num_rows($preRes);

  while($preRow = mysql_fetch_assoc($preRes))
  {
    echo '<tr>';
    echo '<td>'.$preRow[BOL_NUMBER].'</td>';
    echo '<td>'.$preRow[CONTAINER_NUMBER].'</td>';
    echo '</tr>';
  }
 }
 echo "</tbody></table>";          
share|improve this question
    
Please, don't use mysql_* functions, They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi. You will also want to Prevent SQL Injection! –  Jay Blanchard Nov 19 '14 at 20:40
    
You need to do it all in the first for-loop. Right now in that loop you are overwriting the first value with the second, the second with the third and so on. Only the last value will stick. –  RST Nov 19 '14 at 20:46
    
@JayBlanchard I should have stated that I was just using the basic of PHP and MySql. I have taken steps to prevent SQL injections and have also switched to Mysqli. Now back to my issue at hand... –  HoodCoderMan Nov 19 '14 at 20:50
    
You are missing semicolons when you echo your opening and closing <tr> tags. There are cases when you can omit semicolons but it is good practice to always include them. –  zack.lore Nov 19 '14 at 20:54
    
@zack.lore minor typo. Nothing that prevents the query from running. –  HoodCoderMan Nov 19 '14 at 20:59

1 Answer 1

I think you can use "IN" if your POST vars are comma separated.

   $preQuery = "SELECT * FROM mainTable WHERE CONTAINER IN ($_POST['CONTAINER']) AND BOL IN ($_POST['BOL']);

   $preRes = mysql_query($preQuery) or die(mysql_error());

   $preNum = mysql_num_rows($preRes);

Then go to your while loop....

This would omit the need for creating an array and looping it.

Also, you need to switch to PDO for your query. It will take all of an hour to learn.

share|improve this answer
    
Edited the $_POST keys to match yours. –  silversunhunter Nov 19 '14 at 21:06
    
Should I apply this in the first FOR loop? –  HoodCoderMan Nov 19 '14 at 21:07
    
I tried to use IN before and was unsuccessful. I don't PHP likes me using IN. –  HoodCoderMan Nov 19 '14 at 21:16
    
You would omit the for loop and use the comma separated values directly –  silversunhunter Nov 19 '14 at 21:16
    
I would try it in your PHPMyAdmin if you are using that. run the query directly: SELECT * FROM mainTable WHERE CONTAINER IN (12,33,55,6) AND BOL IN (3,4,5,6). Are there words in the values? –  silversunhunter Nov 19 '14 at 21:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.