PHP Dynamic dropdown

I’m having some problems getting a dynamic drop box to work in PHP. The $mem is stored in a previous .php file, this page is included in that file ( hope that makes sense). When i load up the page all that i see is the drop down box with – Please select – in it. It is not pulling from the database. Any suggestions?

<?php
mysql_connect("localhost", "login", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

echo "<br>";
$data = mysql_query("select * from Character WHERE owner='$mem'");
echo "<select name=\\"queryTypeCombo\\" onchange=submitForm()>\
";
echo "<option>-- Please select --</option>\
";

while ($row = mysql_fetch_assoc($data))
{
$ID = $row['CName'];
echo "<option value ='$ID'>"; 
}
echo "</select>\
";
echo "</p>";

?>

What is $mem and where does it come from? Are you sure that this variable is getting populated correctly? You should echo “select * from Character WHERE owner=‘$mem’” and make sure that it says what you expect it to.

Also, if all you need is ‘CName’, why are you getting all of the columns from the database? If you use more columns elsewhere in your script, ignore this question.


$tta = mysql_query("SELECT * FROM `Character` WHERE owner='$mem'") or die(mysql_error());  
$test = mysql_fetch_array ( $tta );
echo $test['CName'];

Placed this above and i was able to get it to display exactly what i needed it to, however i still cannot get it to populate the drop down box. I was having problems with it displaying at all until i added the `'s around Character… So it appears that is now working correctly it seems something is wrong with the rest of the code.

echo "<select name=\\"queryTypeCombo\\" onchange=submitForm()>\
";

echo "<option>-- Please select --</option>\
";



while ($row = mysql_fetch_assoc($data))

{

$ID = $row['CName'];

echo "<option value ='$ID'>"; 

}

echo "</select>\
";

echo "</p>";



?>

Using the following code i am able to get it to display blank spaces in the drop down bar in the exact amount i’d expect however there is no text. Appears to be something wrong with the echo “<option value =\$ID\>”;
line. I’ve tried ‘$ID’

&lt;?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

echo "&lt;br&gt;";


$tta = mysql_query("SELECT * FROM `Character` WHERE owner='$mem'") or die(mysql_error());  

echo "&lt;br&gt;";
echo "&lt;select name=\\"queryTypeCombo\\" onchange=submitForm()&gt;\
";
echo "&lt;option&gt;-- Please select --&lt;/option&gt;\
";

while ($row = mysql_fetch_array($tta))
{
$ID = $row['CName'];
echo "&lt;option value =\\$ID\\&gt;"; 
}
echo "&lt;/select&gt;\
";
echo "&lt;/p&gt;";

?&gt;

Thank you for any help!

Hi,

I think you got almost everything right, except for the HTML.

Each <option> line should be:


<option value=""></option>

Thus:


while ($row = mysql_fetch_array($tta))
{
$ID = $row['CName'];
echo "<option value =\\"$ID\\">$ID</option>"; 
}

Of course you can place any other field value between <option> and </option> given you extract it inside your while loop.

HTH

Note you are only setting the value for the option… you should probably do something like


while ($row = mysql_fetch_array($tta)) 
{ 
   $ID = $row['CName']; 
   echo "<option value =\\"$ID\\">$ID</option>";  
}

*damn you mjpr

“character” is probably a special word to mysql. That’s why you would need the backquotes around it. Good catch.

The other two probably have the reason why your options aren’t displaying.

That did the trick, thank you everyone!

I haven’t looked into it yet, plan on researching more tomorrow, but how can i use the value thats been selected. This is way above my knowledge of PHP I still have a TON to learn, i would guess how it’s setup that the <option value =\“$ID\”> the $ID is the value? Should i change that to something different? Like i said i’ll look around more tomorrow but if it’s something easy would love to know.

Again, thanks for all the help!

<option value=“–XX–”>–YY–</option>

–YY-- is what you would see in the drop down menu on the page with the form.

–XX-- is what the processing script will see when it access the select item.

This is a perfect example of what I like to call “double quotes and extra echo’s for nothing”. Double quotes for echo just make the code more confusing through the use of unneccessary escapes and/or just plain long code. Likewise if you use single quotes you don’t NEED to say “/n” or “/t” since single quotes preserve whitespace. Also no clue why you felt the need for the extra variable inside the loop.


$result=mysql_query("select * from Character WHERE owner='$mem'"); 

echo '<br />
<select name="queryTypeCombo" onchange="submitForm()">
	<option>-- Please select --</option>';

while ($row = mysql_fetch_assoc($data)) { 
	echo '
	<option value ="',$row['CName'],'">';  
} 

echo '
</select>';

I’d also suspect other code issues, since a select is NOT a paragraph, and doesn’t belong in a paragraph… (neither is a label)

I really wonder why anyone bothers with double quotes 99% of the time they use them.

$result=mysql_query("select * from Character WHERE owner='$mem'"); 

echo '<br /><select name="queryTypeCombo" onchange="submitForm()">
	<option>-- Please select --</option>';

while ($row = mysql_fetch_assoc($data)) { 
	echo '<option value ="',$row['CName'],'">' , PHP_EOL ;  
} 

echo '</select>' . PHP_EOL;

I find that even tidier - don’t use hard returns to add new lines in your html source code, use the OS-neutral constant PHP_EOL - and as said don’t pander to feeling to you have to use double quotes just to insert all this "
" nonsense.

(Having said that yesterday I had to add "
" to an xml stream test, so never say never eh?)

Use double quotes when it suits you. e.g when interpolating a scalar variable like so:


echo "This $post_name makes me $body_function, no really.";

There are other ways too, just thought I’d add my 2c.