Jump to content

Is there a problem with this code?


kevincro

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.

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.