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) {
echo '<option value="'.$lead->id.'"';
if ($lead->selected)
echo ' selected="selected"';
echo '>'.$lead->lead_name.'</option>\n';
} ?>
</select>
对于第二个,它确实需要更多澄清,但我怀疑答案将涉及使用 in_array 检查当前字段是否在估计的字段数组中。然后,只有当该子句为 true 时,您才会打印 html 数据。
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.
发布评论
评论(2)
所选选项将具有属性
selected="selected"
,因此在循环期间进行检查并有条件地添加此属性:我可以对第二部分进行一些说明。什么定义了某个项目是否在估算中?
The selected option(s) will have the attribute
selected="selected"
, so do a check during your loop and conditionally add this attribute:I could use some clarification on the second part. What defines whether or not an item is in the estimate?
对于第一个问题,您需要使用选项标签中的selected属性。
对于第二个,它确实需要更多澄清,但我怀疑答案将涉及使用
in_array
检查当前字段是否在估计的字段数组中。然后,只有当该子句为 true 时,您才会打印 html 数据。For the first question, you need to use the selected attribute in the option tag.
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.