Populating a dropdown box

I have an HTML form containing a PHP section within it.

[php]

Connect to a database

Create Form


Choose a Module Mark Value

; <?php

$con=mysqli_connect(‘localhost’, ‘username’, ‘password’) or die(‘No connection’);
mysqli_select_db($con, ‘labuser’) or die (‘test will not open’);

$query=“SELECT distinct studentnum from takes order by studentnum asc”;

//Run a query
//result_variable=mysqli_query(connection_variable, query_variable)
$result=mysqli_query($con, $query) or die (’ test will not open’);

//Fetch a result row as an associative array, a numeric array, or both
//- If an html tag contains a double quote, a backslash is used
while ($rows=mysqli_fetch_array($result)){
echo “<option value=”".$rows[0]."">".$rows[0]."";
}
//echo “”;

// Close a connection
//mysqli_close(connection_variable)
mysqli_close($con);

?>

[/php]

The table it is trying to use ‘takes’ table, to populate the dropdown box with the ‘studentnum’ column’s values in ascending order.

But when I try to run it, the following occurs in the browser. The dropdown box does not populate, and inbetween the unpopulated dropdown box and the “Submit” button is the following:

"; // Close a connection //mysqli_close(connection_variable) mysqli_close($con); ?>

What have I missed that is causing no values to populate the dropdown box? And for PHP code to spill onto the HTML form?

Any help much appreciated. Thanks.

try changing :

[php]
echo “<option value=”".$rows[0]."">".$rows[0]."";
[/php]

to:
[php]echo “{$rows[0]}”;[/php]

I believe there is an error in the quotation marks here that is messing with the query

Tried it, still something wrong.

The dropdown box now contains the following inside it:

{$rows[0]}
and inbetween the unpopulated dropdown box and the “Submit” button is the following:
"; } // Close a connection //mysqli_close(connection_variable) mysqli_close($con); ?>

Thanks for trying plintu.

The option code is fine, but my preference would be to use single quotes to remove the need for escaping:
[php]echo ‘’.$rows[0].’’;[/php]

Not overly important though! Out of interest, what sort of output do you get if you uncomment this line:
[php]//echo “”;[/php]

Because that is the line that comes just before the PHP that gets output.

Smokey,

Fair suggestion, but I get the same output as per my previous post.

And again I get the same output when I uncomment the line you mentioned:

[php]//echo “”;[/php]
I wrote this code two years ago and I actually can’t remember why I uncommented the line at the time since it appears that it doesn’t change the ouput.

Maybe the contents of $row would shed some light on the matter? Have you output the variable inside the while to see if anything strange is going on there?

[php]while ($rows=mysqli_fetch_array($result)){
echo ‘

’.print_r($rows,true).’
’;
//echo “<option value=”".$rows[0]."">".$rows[0]."";
}[/php]

You will want to comment the starting select tag as well for this.

Smokey, just to go back to your previous post rergarding commenting out the This may sound odd but, instead of commenting out the:

[php]//echo “”;[/php]

…I removed it which changed the output so I now see the following in the browser:

An unpopulated dropdown box with the submit button beside it. Progress? - I’m not sure.

Maybe the contents of $row would shed some light on the matter? Have you output the variable inside the while to see if anything strange is going on there?

PHP Code: [Select]
while ($rows=mysqli_fetch_array($result)){
echo ‘

’.print_r($rows,true).’
’;
//echo “<option value=”".$rows[0]."">".$rows[0]."";
}

You will want to comment the starting select tag as well for this.

Done that (with the Select tags removed as mentioned above ). The output I see is the unpopulated dropdown box with the submit button beside it (same as the output mentioned at the start of this post). The output I see is exactly the same as when the following code is used:

[php]while ($rows=mysqli_fetch_array($result)){
echo “<option value=”".$rows[0]."">".$rows[0]."";
}[/php]

Hopefully I explained that well enough. Thanks.

besides this

<select name="valuelist">;

should be

<select name="valuelist">

I think it’s not processing the php code at all. My money is that you have this saved as htm or html

Silly… The code ; does NOT end with a semi-colon!!!

It is an HTML line, not a PHP line.

Also, YOU MUST end all with a or it will not populate. Uncomment this line.
( echo “”; >

And, if it does not work, repost your code so we can see the latest version with these two lines fixed.

Lastly, use this trick… RIGHT-CLICK on the output and VIEW-SOURCE. Then, you can see exactly where your code is failing. You can see if the select “block” is in place and what is missing from the code. Wherever the code ends, that is where you error is. Let us know the results…

Thanks ErnieAlex and Laffin. Problem solved.

I think it's not processing the php code at all. My money is that you have this saved as htm or html

That seems to have been the problem. Sorry for not being clearer about that in my first post. I thought I could do it from an html page with PHP code inside the page, but thanks again.

One further note on your last comment, PHP is run SERVER-SIDE. But, it is handled before the browser sees it. So, actually you CAN run your PHP on your HTML. But, it is when it runs and how to understand.

The PHP code is totally completed before the page is sent CLIENT-SIDE to the browser. So, any items pulled from the database or calculated by formulas are all handled and then added into the html.

Next, the code is send to the browser and rendered by the various parts of HTML and CSS, etc.

Also, CLIENT-SIDE, Javascript kicks in to be used “LIVE” on the browser.

I find that a lot of PHP programmers forget these facts. Especially on this site. So, I repeat them a lot whenever someone says “I can’t” or “thought I could do…” . Usually you can do anything! Just takes a little thought and preliminary layout…

Sponsor our Newsletter | Privacy Policy | Terms of Service