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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.