Reputation: 1
I am trying to dynamically populate a select tag in php, by printing values from an array of objects.
this is my code
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[i]->getId().'">'.$z[i]->getDescription().'</option>');
}
the array is populated, in fact if i try to print only the fields i get a result, but in the combo-box nothing appears
Upvotes: 0
Views: 35
Reputation: 1
ok, i resolved by creating two support arrays
$ids = array();
$descs = array();
for ($i = 0; $i < count($z); $i++) {
$ids[$i] = $z[$i]->getId();
$descs[$i] = $z[$i]->getDescritpion();
}
and using them instead of the objects
for ($i = 0; $i < count($z); $i++) {
print('<option value ="' . $ids[$i] . '">' . $descs[$i] . '</option>');
}
not very efficient, but working. thanks for the answer :)
Upvotes: 0
Reputation: 828
Add select tag and replace i
with $i
echo "<select>";
for($i = 0; $i < count($z); $i++) {
echo '<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>';
}
echo "</select>";
Upvotes: 0
Reputation: 9707
Replace i
with $i
<select name="name">
<?php
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>');
}?>
</select>
Upvotes: 1