user959443
user959443

Reputation: 97

populating a select box from php mysql database

I am trying to populate a selection box (dropdown) from a mysql table in php, but I get blank results, Can someone look in my code and tell me what is wrong?

I need to populate the select with the dates available from my sql query, so they show up as seletion options....

<?php

echo JText::_('Please select the date:');

$database= &JFactory::getDBO();
$database->setQuery('SELECT training_id,training,trainingDate FROM training');
$result = $database->loadObjectList();

echo '<select name="whatever">';
foreach ($result as $row) {
   echo '<option value="'.$row->training_id.'" />';

}
echo '</select>';

?>

Upvotes: 0

Views: 2551

Answers (3)

Dave
Dave

Reputation: 29121

Try this:

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

instead of this:

foreach($result as $row) {

Upvotes: 0

Nick
Nick

Reputation: 6346

echo '<option value="'.$row->training_id.'">'.$row->trainingDate.'</option>';

If that doesn't work then your $row->training_id isn't set and so you'll need to debug that (e.g. doing a print_r($row) just before that line to see what is inside the $row object)

Upvotes: 1

Nemoden
Nemoden

Reputation: 9056

You assign an option value, but you don't provide human-readable option text:

echo '<select name="whatever">';
foreach ($result as $row) {
                                                 <!-- here we go -->
   echo '<option value="'.$row->training_id.'">'.$row->training.'</option>'; // always close options!!!

}
echo '</select>';

Upvotes: 3

Related Questions