模型::
public function vendor_services($id)
{
$q=$this->db->where(['id'=>$id])
->get('vendor_services');
return $q->result();
}
public function add_vendor_services($id,$service)
{
$query = $this->db->get('vendor_services');
foreach ($query->result() as $row)
{
$ids[]=$row->id;
}
foreach($ids as $vid){
if($id==$vid){
$q1 =$this->db->get_where('vendor_services',array('id'=>$vid));
$serv=$q1->row()->service_offered;
if($serv===$service)
return;
else{
$this->db->set('id',$id)
->set('service_offered',$service)
->insert('vendor_services');
return;
}}else{$this->db->set('id',$id)
->set('service_offered',$service)
->insert('vendor_services');
return;
}
}
}
{
$q =$this->db->get_where('services_list',array('id'=>$id));
if($q->num_rows())
{
return $q->row()->name;
}
else{ return FALSE; }
}

**
$vendor=$this->input->post();
$service_id=$this->input->post('service');
unset($vendor['service']);
$loc_id=$this->input->post('locations');
$login_email=$this->session->userdata('email_id');
$vendor['locations']=$this->Vendormodel->get_location($loc_id);
$this->Vendormodel->add_vendor_profile($login_email,$vendor);
$id1=$this->Vendormodel->get_vendor_id($login_email);
if(isset($service_id))
foreach($service_id as $id){
$post[]=$this->Vendormodel->get_services($id);
}
if(isset($service_id))
foreach($post as $service){
$this->Vendormodel->add_vendor_services($id1,$service);
}
$vendordash['det']=$this->Vendormodel->vendor_details($login_email);
$vendordash['services']=$this->Vendormodel->vendor_services($id1);
$this->load->view('vendor/vendor_dashboard',$vendordash);

**
<div class="container">
<label>Service Offering</label>
<?php if(isset($services))
foreach($services as $service): ?>
<div style="width:20%; float:left;">
<div class="checkbox"><label>
<input type="checkbox" name='service[]' value="1" <?php if($service->service_offered==="Engineering and Design")echo "checked";?> >Engineering and Design</label></div>
<div class="checkbox"><label>
<input type="checkbox" name='service[]' value="2" <?php if($service->service_offered==="Operations and support")echo "checked";?> >Operations and support</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="3" <?php if($service->service_offered==="Product management")echo "checked";?> >Product management</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="4" <?php if($service->service_offered==="Developer relations and technical solutions")echo "checked";?> >Developer relations and technical solutions</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="5" <?php if($service->service_offered==="Sales and account management")echo "checked";?> >Sales and account management</label></div>
</div>
<div style="width:20%; float:left;">
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="6" <?php if($service->service_offered==="Partnerships")echo "checked";?> >Partnerships</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="7" <?php if($service->service_offered==="Sales and operations")echo "checked";?> >Sales and operations</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="8" <?php if($service->service_offered==="Administrative services")echo "checked";?> >Administrative services</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="9" <?php if($service->service_offered==="Business strategy planning")echo "checked";?> >Business strategy planning</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="10" <?php if($service->service_offered==="Finance solutions")echo "checked";?> >Finance solutions</label></div>
</div>
<div style="width:20%; float:left;">
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="11" <?php if($service->service_offered==="Legal and government relations")echo "checked";?> >Legal and government relations</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="12" <?php if($service->service_offered==="Marketing and communications")echo "checked";?> >Marketing and communications</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="13" <?php if($service->service_offered==="Real estate and workplace services")echo "checked";?> >Real estate and workplace services</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="14" <?php if($service->service_offered==="Social impact solutions")echo "checked";?> >Social impact solutions</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="15" <?php if($service->service_offered==="Consultancy services")echo "checked";?> >Consultancy services</label></div>
<div class="checkbox"> <label>
<input type="checkbox" name='service[]' value="16" <?php if($service->service_offered==="Investors and funding")echo "checked";?> >Investors and funding</label></div>
</div>
<?php endforeach; ?>
</div>
&#13;
强文 这是我的视图页面我想从控制器中获取多个输入并在复选框中实现.....当我放置多个检查时,页面显示所有复选框的另一个重复列表....显示的列表=否。输入很尴尬............帮助我让它以它应该的方式运行
答案 0 :(得分:0)
$new_array=array('1'=>'Engineering and Design','2'=>'Operations and support'.....);
将键值设置为1,因为您的值看起来像那样
假设此数组键将设置复选框
的值将您的值存储在数组
中使用array_search
array_search()
函数在数组中搜索值并返回键。
<?php if(isset($services))
foreach($services as $service): ?>
<input type="checkbox" name='service[]' <?php if($key = array_search($service->service_offered, $new_array)){ echo "checked"; ?> value="<?php echo $key; ?>" <?php } else { echo 'value=""'; } ?> ><?php echo $new_array[$key]; ?> <br/>
<?php endforeach; ?>
修改强>
像这样循环$ new_array
<?php if(isset($new_array))
foreach($new_array as $key=>$val): ?>
if($db_key = array_search($val, $services))
{
<input type="checkbox" name='service[]' value="<?php echo $key ?>" checked ><?php echo $val; ?> <br/>
}
else
{
<input type="checkbox" name='service[]' value="<?php echo $key ?>" ><?php echo $val; ?><br/>
}
<?php endforeach; ?>
编辑2:
public function vendor_services($id)
{
$q=$this->db->where(['id'=>$id])
->get('vendor_services');
return $q->result_array();
^^^^^^^^^^^^
//here fetch the value as array
}