thecase Posted August 5, 2010 Share Posted August 5, 2010 Hi, This is my code $query = mysql_query("SELECT id, username, lastname FROM users ORDER BY username ASC"); echo '<h1>Add Rota</h1> <table> <tr> <td><b>Presenter 1:</b></td> <td> <select name="presenter1"> <option value="0">Pick presenter</option> '; while ($row = mysql_fetch_array($query, MYSQL_NUM)) { echo "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n"; } echo ' </select> </td> <td></td> <td></td> <td></td> </tr> <tr> <td><b>Presenter 2:</b></td> <td> <select name="presenter2"> <option value="0">Pick presenter</option> '; while ($row = mysql_fetch_array($query, MYSQL_NUM)) { echo "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n"; } echo ' </td> I am trying to define the $query to use in a dropdown twice although I cannot re use it and this doesnt work for the second dropdown. I was trying to only write the query out once to save on resources. Any suggestions would be great Thanks Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 5, 2010 Share Posted August 5, 2010 Form (concatenate) the entire <option> ... </option> list in a variable, then echo the variable any place you want it or if your whole <select> ... </select> menu is the same each time, form the whole <select> ... </select> menu in a variable and echo the variable any place you want it. Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095753 Share on other sites More sharing options...
thecase Posted August 5, 2010 Author Share Posted August 5, 2010 Do you mean something like this $dropdown = " while ($row = mysql_fetch_array($query, MYSQL_NUM)) { <option value={$row['0']}>{$row['1']} {$row['2']} </option>\n } </select> "; It comes back with Notice: Undefined variable: row Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095764 Share on other sites More sharing options...
jcbones Posted August 5, 2010 Share Posted August 5, 2010 No what they mean is something on the lines of this: $query = mysql_query("SELECT id, username, lastname FROM users ORDER BY username ASC"); echo '<h1>Add Rota</h1> <table> <tr> <td><b>Presenter 1:</b></td> <td> <select name="presenter1"> <option value="0">Pick presenter</option> '; while ($row = mysql_fetch_array($query, MYSQL_NUM)) { $options[] = "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n"; } foreach($options as $v) { echo $v; } echo ' </select> </td> <td></td> <td></td> <td></td> </tr> <tr> <td><b>Presenter 2:</b></td> <td> <select name="presenter2"> <option value="0">Pick presenter</option> '; reset($options); foreach($options as $v) { echo $v; } echo ' </td> Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095766 Share on other sites More sharing options...
thecase Posted August 5, 2010 Author Share Posted August 5, 2010 Perfect. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095768 Share on other sites More sharing options...
ngreenwood6 Posted August 5, 2010 Share Posted August 5, 2010 Please do not follow the last persons post. You will have more time processing it than you need to. He is essentially doing 3 loops and messing allocating memory for arrays. My solution just puts it into one variable and only does one loop. This is what the original poster actually meant: <?php $query = mysql_query("SELECT id, username, lastname FROM users ORDER BY username ASC"); //setup options to hold the options; $options = ''; while ($row = mysql_fetch_array($query, MYSQL_NUM)){ $options .= "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n"; } ?> <h1>Add Rota</h1> <table> <tr> <td><b>Presenter 1:</b></td> <td> <select name="presenter1"> <option value="0">Pick presenter</option> <?php echo $options; ?> </select> </td> <td></td> <td></td> <td></td> </tr> <tr> <td><b>Presenter 2:</b></td> <td> <select name="presenter2"> <option value="0">Pick presenter</option> <?php echo $options; ?> </select> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095771 Share on other sites More sharing options...
jcbones Posted August 5, 2010 Share Posted August 5, 2010 I would concur with the last posters assessment. Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095773 Share on other sites More sharing options...
ngreenwood6 Posted August 5, 2010 Share Posted August 5, 2010 Thanks, wasnt trying to put you down but the whole point of him wanting to do that was to make it more efficient and the method that I provided was. I think its too late though because it doesnt seem like he is viewing this post anymore lol. Quote Link to comment https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.