Jump to content

Drop Down Select via PHP


Bradley99

Recommended Posts

So here's how I tried it. .

 

$eventname = mysql_fetch_object(mysql_query("SELECT * FROM events ORDER by event_id ASC"));
while($row = mysql_fetch_array($eventname));

 

With

 

			  <tr>
                <td class="subtableheader"><b>Event:</b></td>
                <td class="profilerow"><select name=event><option value=1><?php echo "$row"; ?></option></td>
              </tr>

 

And i get error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in . . .  on line 28

 

Line 28 is the "while($row = mysql. . . )

This is what I got, But it isn't working, If I just echo the results as text, it works, but when i try and do it as a select drop down, it shows none.

 

                <td class="profilerow"><select name=event>      <?
   $select_event = mysql_query("SELECT * FROM events ORDER by id ASC");
   while($the=mysql_fetch_object($select_event)){
   
   }
   echo "<option value=1>$the->event</option>";
   
   
   
   
   ?></td>

Yeah i missed the closing select tag, made no difference though. Here's the HTML source. .

			  <tr>
                <td class="subtableheader"><b>Event:</b></td>
                <td class="profilerow">      <select name=event><option value=1></option></select></td>
              </tr>

 

And I do know that but don't know how I'd change that?

 

Thanks

Your query is either failing, or returning an empty results set. Do you have error reporting set up to display errors to the screen? You also need to verify that your query not only executed, but also returned more than 0 records before attempting to use the data.

<select name="event">      
<?php
$select_event = mysql_query("SELECT id, event FROM events ORDER by id ASC");
while($the=mysql_fetch_object($select_event)){
	echo "<option value=\"" .$the->id . "\">" .$the->event . "</option>\r";
}
?>
</select>

 

You don't want an empty select if there are no options.

<?php
// Define query
$query = mysql_query("SELECT id, event FROM events ORDER by id ASC");

// Ensure you have values returned
if( mysql_num_rows( $query ) >= 1 ) {

// Rows is more than 1, build select menu
$dropdown = '<select name="horse" id="horse_drop">';

// Now build loop
while( $item = mysql_fetch_assoc( $query ) {
	$dropdown .= '<option value="' . $item["value"] . '">' . $item["name"] . '</option>';
}

// Close select menu
$dropdown .= '</select>';
} else {
// Error checking
$dropdown = 'bad horsey!';
?>

// Wherever you want your select menu to be, you simply
echo $dropdown;
</select>

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.