Forum Moderators: coopster

Message Too Old, No Replies

Advice on an option statement

         

egibberate

10:51 am on Nov 23, 2008 (gmt 0)

10+ Year Member



Hi,
beginner question. I'm studying the block of code shown below that populates a drop down list. Here are the table name and field names;

student (table name)
id (field name)
name (field name)
class (field name)
mark (field name)
sex (field name)

Could some wise expert explain the purpose of the words 'Student Name' in the following line of code for me please (like I said, beginner, don't laugh!)

echo "<select name=student value=''>Student Name</option>";

Here is the full block of code,

<HTML>
<HEAD>
<TITLE>Test Drop Down List</TITLE>
</HEAD>
<BODY>
<?
include 'config.php';
include 'opendb.php';
echo 'Connected successfully';
$link = mysql_connect('localhost','root','mypassword');
mysql_select_db('mydatabase');
$query="SELECT name,id FROM student";
$result = mysql_query ($query);
echo "<select name=student value=''>Student Name</option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[name]</option>";
}
echo "</select>";
?>
</BODY>
</HTML>

Thank you for your time.

Anyango

11:21 am on Nov 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that is wrong html, so i would recommend not to be bothered by it.

echo "<select name=student value=''>Student Name</option>";

that is incorrect. "Select" is the name of the field that will contain all the different options/values and will essentially become a drop down box. so we would start it as

<select name='myDropDownBox'>

and we would end it as

</select>

And to add values to this drop down box we will use the "option" tags. An option tag requires 2 things, 1 is its value and second is its Caption (which will be shown on the site)

so it will be like this

<HTML>
<HEAD>
<TITLE>Test Drop Down List</TITLE>
</HEAD>
<BODY>
<?
include 'config.php';
include 'opendb.php';
echo 'Connected successfully';
$link = mysql_connect('localhost','root','mypassword');
mysql_select_db('mydatabase');
$query="SELECT name,id FROM student";
$result = mysql_query ($query);
echo "<select name='student'>
<option value=''>Student Name</option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[name]</option>";
}
echo "</select>";
?>
</BODY>
</HTML>

So that way we are first defining the select field, then we are giving it the top option "Student Name" and its value is empty. then we are adding all other options to it in your while loop, where our values are $nt[id] and the caption/text for that option is $nt[name].

Hope that answers it

[edited by: Anyango at 11:22 am (utc) on Nov. 23, 2008]

egibberate

11:34 am on Nov 23, 2008 (gmt 0)

10+ Year Member



You most certainly did answer my question. Thank you for your detailed & clear explanation, I'm very grateful.