Jump to content

Select Option List Help


twilitegxa

Recommended Posts

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

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>";
?>

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";
?>

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.