fife Posted March 4, 2011 Share Posted March 4, 2011 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 More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 If you need to repeat the <option></option> selections, why not just produce that part of your output in a php variable and then simply echo the contents of that variable every time you need it? Link to comment https://forums.phpfreaks.com/topic/229589-repeated-assoc-array/#findComment-1182858 Share on other sites More sharing options...
fife Posted March 4, 2011 Author Share Posted March 4, 2011 I would but I need 5 separate entries from the same array entered into my database Link to comment https://forums.phpfreaks.com/topic/229589-repeated-assoc-array/#findComment-1182868 Share on other sites More sharing options...
fife Posted March 4, 2011 Author Share Posted March 4, 2011 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 https://forums.phpfreaks.com/topic/229589-repeated-assoc-array/#findComment-1182919 Share on other sites More sharing options...
kenrbnsn Posted March 4, 2011 Share Posted March 4, 2011 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 https://forums.phpfreaks.com/topic/229589-repeated-assoc-array/#findComment-1182927 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.