1

Hi I am trying to populate an entire Drop down list with MySQL but I cant get it to work, can you please help?

My code:

$database=& JFactory::getDBO();

$database->setQuery('SELECT training_id,training,trainingDate FROM training ');

$result = $database->loadObjectList();

 echo '<select name="whatever">';
while($row = mysql_fetch_array($result)) {
        echo '<option value="$row[training_id" />';
    }

echo '</select>';
1
  • Try echo '<option value=$row[training_id] />'; instead. Commented Nov 17, 2011 at 4:06

6 Answers 6

5

Your echo string doesn't allow for embedded variables because you are using single quotes instead of double quotes.

Implement this echo instead:

echo '<option value="' . $row["training_id"] . '" />';
Sign up to request clarification or add additional context in comments.

Comments

1

Without knowing what the output is, it's hard to know whether this is the only issue, but the glaring error in your code is this:

echo '<option value="$row[training_id" />';

Because this is in single quotes, the variable is not interpreted. You need to use double quotes (and close the square brackets!):

echo "<option value=\"{$row['training_id']}\" />";

Note that I have changed the style of variable interpretation to use curly brackets: I believe this is easier to read and clearer.

Comments

1

In addition to using single quotes, you are also:

  1. Missing a closing square bracket.
  2. Missing the closing tag for the <option>.

You probably want to change your output to something like this, so you display some option text to the user:

echo '<select name="whatever">';
while($row = mysql_fetch_array($result)) {
    echo '<option value="' . $row['training_id'] . '"> ' . $row['training'] . '</option>';
}
echo '</select>';

Comments

1
$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>';

Use #__ for table prefix.

1 Comment

The select query works fine, is just that I cannot populate the select box with the ids...
1
$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>';

This code works, but populates the list with empty options. I had to put a simple echo in as shown below, and works just fine. For some reason, $row->teremnev is empty for me. Im sure this is not the right way, but it works.

$db =& JFactory::getDBO();
$query = "SELECT teremnev FROM #__teremlista";
$db->setQuery($query);
$result = $db->loadResultArray(); 
echo '<select name="termek">' ;
foreach ($result as $row) {
echo '<option value="'.$row->teremnev.'" />';
echo $row;
}
echo '</option>';
echo '</select>';

Comments

0
<select>
<option>Select Training</option>

while($row = mysqli_fetch_array($result))
{
 echo "<option>". $row['training_name']."</option>";

}
</select> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.