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
https://forums.phpfreaks.com/topic/229589-repeated-assoc-array/
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>


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

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.