Reputation: 1029
I'm not sure how to make a few parts of my form to populate from data from an array I'm passing from the database.
First is this <select> object. The key estimate_lead_id in the database holds the value, and I want the dropdown to auto-select based on the value from the database.
<select name="estimate_lead_id">
<? foreach($leads->result() as $lead) { ?>
<option value="<?=$lead->id?>"><?=$lead->lead_name?></option>
<? } ?>
</select>
EDIT: I'll break the second part into another question.
Upvotes: 0
Views: 171
Reputation: 17341
For the first question, you need to use the selected attribute in the option tag.
<select name="estimate_lead_id">
<? foreach($leads->result() as $lead) {
echo '<option value="'.$lead->id.'"';
if ($lead->selected)
echo ' selected="selected"';
echo '>'.$lead->lead_name.'</option>\n';
} ?>
</select>
For the second, it does need some more clarification, but I suspect the answer would involve and if clause that used in_array
to check if the current field is in the array of field in the estimate. You would then only print the html data when the clause is true.
Upvotes: 0
Reputation:
The selected option(s) will have the attribute selected="selected"
, so do a check during your loop and conditionally add this attribute:
<select name="estimate_lead_id">
<? foreach($leads->result() as $lead) { ?>
<option value="<?=$lead->id?>" <?php echo $estimate_lead_id == $lead->id ? 'selected="selected"' : ''; ?>><?=$lead->lead_name?></option>
<? } ?>
</select>
I could use some clarification on the second part. What defines whether or not an item is in the estimate?
Upvotes: 1