Jump to content

[SOLVED] loading mysql results into a drop down box


blueman378

Recommended Posts

hi guys (and PHP_PhREEEk who is the man)

i am trying to get some information from a database and write it to a drop down box,

 

heres the code im using,

<?php
include("include/session.php");    
global $database;
    $q = "SELECT cName FROM " . gsubcat . "
          ORDER BY `cName` ASC
         ";   
    $result = $database->query($q) or die("Error: " . mysql_error());
    /* Error occurred, return given name by default */
    $num_rows = mysql_numrows($result);
    if( $num_rows == 0 ){
      return 'Cats not found!';
    }
?>

<form name="cats" action="cats.php">
<select>
<?php
    while( $row = mysql_fetch_assoc($result) ) {
    echo '<option value="$row[cName];">
    echo $row[cName];</option>';
    }

?>
</select>
</form>

 

and heres the result

 

<form name="cats" action="cats.php">
<select>
<option value="$row[cName];">echo $row[cName];</option>
<option value="$row[cName];">echo $row[cName];</option>
<option value="$row[cName];">echo $row[cName];</option>
<option value="$row[cName];">echo $row[cName];</option>
<option value="$row[cName];">echo $row[cName];</option>
<option value="$row[cName];">echo $row[cName];</option>
<option value="$row[cName];">echo $row[cName];</option>
<option value="$row[cName];">echo $row[cName];</option>
</select>
</form>

 

obviously not the desired result,

 

i know it is getting the information from the database ok because the amount of options = the information in the database,

 

so any1 help?

 

cheers

you should try something like this

 

   

while( $row = mysql_fetch_assoc($result) ) {
    echo "<option value='{$row[cName]}'>{row['cName']}</option>";
    }

 

basically you were using single quotes so it was taking the literal string and not as a variable

ah thanks guys,

PHP_PhREEEk's one worked perfectly,

rajivgonsalves's one half worked...

<form name="cats" action="cats.php">
<select>
<option value='Action / Adventure'>{row['cName']}</option>
<option value='Beat em up'>{row['cName']}</option>
<option value='Other'>{row['cName']}</option>
<option value='Puzzle'>{row['cName']}</option>
<option value='Racing'>{row['cName']}</option>
<option value='Retro'>{row['cName']}</option>
<option value='Shoot em up'>{row['cName']}</option>
<option value='Sports'>{row['cName']}</option>
</select>
</form>

but its all good thanks peoples

 

hey PHP_PhREEEK how long do u spend on here a day?

cheers

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.