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>';
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"] . '" />';
$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>';
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.
In addition to using single quotes, you are also:
<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>';
$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.
<select>
<option>Select Training</option>
while($row = mysqli_fetch_array($result))
{
echo "<option>". $row['training_name']."</option>";
}
</select>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.