Jump to content

Looping through arrays


Mr Chris

Recommended Posts

Hello All,

 

I have two arrays.  One called $number_array and one called $title_array.

 

  $number_array  = (1,2,3,4);
  $title_array  = ("Title One", "Title Two", "Title Three", "Title Four");

 

Now what I want to do is run through each of the arrays one by one each time I query my database, so for example the first time it would output the query and title as:

 


//First time
SELECT * FROM tbl WHERE something = 1
<optgroup label="Title One">


//Second time
SELECT * FROM tbl WHERE something = 2
<optgroup label="Title Two">

 

//etc..

 

so here's my code:

 

<?
  echo '<select id="class" class="selectbox" name="class">';
  $number_array  = (1,2,3,4);
  $title_array  = ("Title One", "Title Two", "Title Three", "Title Four");
  
   $result = mysql_query ('SELECT * FROM tbl WHERE something = ".$number_array."') or die ('Error: '.mysql_error ());
     echo '<optgroup label="".$title_array."">';
       while ($row = mysql_fetch_array($result)) { 
         echo '<option value="'.$row['id'].'" class="'.$row['name'].'">'.strtoupper($row['name']).'</option>'; 
       } 
          echo '</optgroup>';			
  echo '
</select>';

?>

 

Anyone please help?

Link to comment
https://forums.phpfreaks.com/topic/213369-looping-through-arrays/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.