Jump to content

repeated assoc array


fife

Recommended Posts

how do you use a while loop 2 twice from one assoc array? eg

 


<tr>
    <td>1st favourite</td>
    <td><select name = 'fav_1'>"; while($Cat = mysql_fetch_assoc($rCat)) { echo " <option value='{$Cat['catID']}'>{$Cat['categorys']}</option>";}

echo "</td>
  </tr>
  <tr>
    <td>2nd favourite</td>
    <td><select name = 'fav_2'>"; while($Cat = mysql_fetch_assoc($rCat)) { echo " <option value='{$Cat['catID']}'>{$Cat['categorys']}</option>";}

echo "</td>
  </tr>

 

The first one works fine but the second is empty.

Link to comment
Share on other sites

ok I have updated the query to look as follows but still nothing is displayed in the 2nd box.

 

  $qCat = "SELECT * FROM category"; 
$rCat = mysql_query($qCat);
$Cat = mysql_fetch_assoc($rCat);

<td>1st favourite</td>
    <td><select name = 'fav_1'>"; while($Cat) { echo " <option value='{$Cat['catID']}'>{$Cat['categorys']}</option>";}

echo " </select></td>
  </tr>
  <tr>
    <td>2nd favourite</td>
    <td><select name = 'fav_2'>"; while($Cat){ echo " <option value='{$Cat['catID']}'>{$Cat['categorys']}</option>";}

echo "</select></td>
  </tr>


Link to comment
Share on other sites

Here's one way of doing it:

<?php
$tmp = array();
while($Cat = mysql_fetch_assoc($rCat)) {
    $tmp[] = " <option value='{$Cat['catID']}'>{$Cat['categorys']}</option>";
}
$options = implode("\n",$tmp);
?>

<tr>
    <td>1st favourite</td>
    <td><select name = 'fav_1'>" <?php echo $options; ?></td>
</tr>
<tr>
    <td>2nd favourite</td>
    <td><select name = 'fav_2'>" <?php echo $options; ?></td>
  </tr>

 

Ken

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.