Jump to content

Recommended Posts

This script populates a select box with MySQL data.  The problem I'm having is that it only displays the first result.  Does anyone know of a better way to populate a select box with MySQL data?

 

echo "<TD>".'<select name="wgr" >';
echo '<option value="">Applicants</option>';
$conn = mysql_connect(host, database user, password) or die($msg_no_connect);
mysql_select_db(database, $conn) or die("Error connect: ".mysql_error());
$result=mysql_query("SELECT * FROM Stake_Applicants where Stake_Number= $data_view ");
while ($row=mysql_fetch_assoc($result)) {
  $sn = $row['column'];
  $nn = $row['column 2'];
  echo "<option value='$sn'>$sn-$nn</option>".'</select>'."</td>";
}

Link to comment
https://forums.phpfreaks.com/topic/73885-is-there-a-problem-with-this-code/
Share on other sites

You ended your SELECT inside your while loop..

 

Try this:

 

echo "<TD><select name='wgr' >";
echo '<option value="">Applicants</option>';
$conn = mysql_connect(host, database user, password) or die($msg_no_connect);
mysql_select_db(database, $conn) or die("Error connect: ".mysql_error());
$result=mysql_query("SELECT * FROM Stake_Applicants where Stake_Number= $data_view ");
while ($row=mysql_fetch_assoc($result)) {
  $sn = $row['column'];
  $nn = $row['column 2'];
  echo "<option value={$sn}>{$sn}-{$nn}</option>";
}

echo "</select></td>";

 

 

And here is a cleaner version: (the code below is the same as i posted above.. just easier to read)

 

// Get data
$conn = mysql_connect(host, database user, password) or die($msg_no_connect);
mysql_select_db(database, $conn) or die("Error connect: ".mysql_error());
$result=mysql_query("SELECT * FROM Stake_Applicants where Stake_Number= $data_view ");

echo "
<TD>
<select name='wgr' >
  <option value=''>Applicants</option>
";

while ($row=mysql_fetch_assoc($result))
{
  $sn = $row['column'];
  $nn = $row['column 2'];
  echo "<option value={$sn}>{$sn}-{$nn}</option>";
}

echo "
</select>
</td>";

Maybe not related but, an array index cannot contain a space. ie;

 

$nn = $row['column 2'];

 

Actually it can.. I even use UTF8 encoded foreign language strings as array indices.  Plenty of them have spaces inside.  An associative array index can have anything a string can have.

maybe this will help

 

$whatever = mysql_query("SELECT something FROM sometable;");

 

// Then, where you want your drop down list

 

echo "<FORM NAME=\"select\" ACTION=\"select.php\" METHOD=POST>\n";

 

echo "<font class=\"text1\">What to select:</font>&nbsp<SELECT CLASS = \"small\" NAME = selected>";

 

for ($j=0; $j<mysql_num_rows($whatever); $j++){

 

$list = mysql_fetch_array($whatever);

 

  echo "<OPTION  VALUE=$list[somevalue]> $list[showsomename]   \n";}

 

echo "</SELECT>";

echo "<INPUT CLASS=\"button\" TYPE=submit VALUE='GO!'>\n";

 

echo "</form>\n";

 

Hope that can help

 

Maybe not related but, an array index cannot contain a space. ie;

 

$nn = $row['column 2'];

 

Actually it can.. I even use UTF8 encoded foreign language strings as array indices.  Plenty of them have spaces inside.  An associative array index can have anything a string can have.

 

Well I'll be. I guess this is just one of those things I never thought about using. Seems logical now that I think about it. I meen, an associative array's index is simply a string. Still, doesn't look right somehow.

 

But yeah... learn something new.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.