twilitegxa Posted January 30, 2010 Share Posted January 30, 2010 I have the following code: <?php $get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'"; $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error()); while ($list_scouts = mysql_fetch_array($get_scouts_res)) { $identity = ucwords($list_scouts['identity']); $topic_id = $list_scouts['id']; echo "<select><OPTION>$identity</OPTION></select>"; } ?> How can I make each option appear in the select list? Currently, each record is in its own select list, side by side. I know I am doing something wrong, but I don't know how to fix it. Can anyone help? I think it's something with num_rows or something like that. can anyone help? Link to comment https://forums.phpfreaks.com/topic/190329-select-option-list-help/ Share on other sites More sharing options...
twilitegxa Posted January 30, 2010 Author Share Posted January 30, 2010 Oh, sorry guys, I figured it out. I had my code a little out of order: <?php $get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'"; $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error()); echo "<select>"; while ($list_scouts = mysql_fetch_array($get_scouts_res)) { $identity = ucwords($list_scouts['identity']); $topic_id = $list_scouts['id']; echo "<OPTION>$identity</OPTION>"; } echo "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/190329-select-option-list-help/#findComment-1004113 Share on other sites More sharing options...
Psycho Posted January 30, 2010 Share Posted January 30, 2010 You need to create the OPTIONS inside the loop. Then after you have generated all the options, then create the SELECT field and add the options. <?php //Query the data $username = mysql_real_escape_string($_SESSION['userName']); $get_scouts = "select * from scouts where username = '{$username}'"; $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error()); //Create the options $scout_options = ''; while ($list_scouts = mysql_fetch_array($get_scouts_res)) { $identity = ucwords($list_scouts['identity']); $topic_id = $list_scouts['id']; $scout_options .= "<option value=\"{$topic_id}\">{$identity}</option>\n"; } //Create the select list echo "<select name=\"scount_list\">\n"; echo $scout_options; echo "</select>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/190329-select-option-list-help/#findComment-1004115 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.