Forum Moderators: coopster

Message Too Old, No Replies

drop down box issues

drop down box issues

         

scooby545

8:56 am on Jun 14, 2005 (gmt 0)

10+ Year Member


Hi,

i am trying to create a drop down box which references to a mysql database for its options.

So far i have managed to get the Box working by manually putting in the php in the option commands eg...

<select name="select1" styple="width:300">
<option><?php ........ limit 0,1 orderby date DESC?></option>
<option><?php ........ limit 1,1 orderby date DESC?></option>
</select>

The problem with this is if you allow for more inputes to be entered into the list later on you get loads of blank options.

Does anyone know of a way to make php automatically insert a new option if it exists in the database?

Any help on this would be most appreciated
thankyou

Blackie

10:37 am on Jun 14, 2005 (gmt 0)

10+ Year Member



Put the option into a loop, like this:

<select name="select1" styple="width:300">
<?php
while (mysql_fetch_row($result)) {
echo "<option>$row[x]</option>";
}
?>
</select>

Hope this helps you get started...

scooby545

11:16 am on Jun 14, 2005 (gmt 0)

10+ Year Member


I have now modified the code to below :

<select name="select1" style="width:300">

<?php

if (!$link = mysql_connect('localhost', 'root')) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db('reports', $link)) {
echo 'Could not select database';
exit;
}

$sql = 'SELECT * FROM comments1 order by added DESC limit 1,1';
$result = mysql_query($sql, $link);

if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

while (mysql_fetch_row($result)) {
echo "<option>$row['comment']</option>";

}

?>

</select>

and am getting this error :

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\inetpub\wwwroot\Reports\comment1chooser.php3 on line 35

Can anyone shed some light on this one?

thankyou for all the help so far!

Blackie

1:06 pm on Jun 14, 2005 (gmt 0)

10+ Year Member



Which is line 35?

P.S. and you should remove "limit 1,1" since now you need all the lines that match...

scooby545

1:29 pm on Jun 14, 2005 (gmt 0)

10+ Year Member


line 35 is the:
echo "<option>$row['comment']</option>";

line

dreamcatcher

1:48 pm on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are using mysql_fetch_row, which has numerical indices. ie:

echo "<option>$row[0]</option>";

RonPK

1:55 pm on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also,

while (mysql_fetch_row($result)) {

should be

while ($row = mysql_fetch_row($result)) {