2016-07-07 2 views
-1

View :::SQL запросов, связанных с CodeIgniter

     <form method="post" action="<?php echo base_url(); ?>Signup/profile"> 
 

 

 
         <div class="form-group"> 
 
          <label>Company Name</label> 
 
          <input type="text" class="form-control" name="company_name" value="<?php echo $det->company_name; ?>" required> 
 
         </div> 
 
         <div class="form-group"> 
 
          <label>Upload Company Logo</label> 
 
          <input type="file" name="company_pic" accept="image/*" class="form-control" vlaue="<?php echo $det->company_pic; ?>" required> 
 
         </div> 
 
         <div class="form-group"> 
 
          <label>Upload Profile Picture</label> 
 
       \t \t \t \t <input type="file" name="profile_pic" accept="image/*" class="form-control" vlaue="<?php echo $det->profile_pic; ?>" required> 
 
         </div> 
 

 
         <div class="form-group"> 
 
          <label>Projects Done</label> 
 
          <input type="number" class="form-control" name="projects_done" value="<?php echo $det->projects_done; ?>" required> 
 
         </div> 
 

 
         <div class="form-group"> 
 
          <label>No Of Employees</label> 
 
          <input type="number" class="form-control" name="num_of_emp" value="<?php echo $det->num_of_emp; ?>"required> 
 
         </div> 
 

 
         <div class="form-group"> 
 
          <label>Founding Year</label> 
 
\t \t \t \t \t \t \t </br> 
 
\t \t \t \t \t \t \t </br> 
 
          <input type="number" class="form-control" name="found_year" value="<?php echo $det->found_year; ?>" required> 
 
         </div> 
 

 
         <div class="container"> 
 
          <label>Service Offering</label></br> 
 
\t \t \t \t \t \t \t <?php 
 
$new_array=array('1'=>'Engineering and Design','2'=>'Operations and support','3'=>'Product management','4'=>'Developer relations and technical solutions','5'=>'Sales and account management','6'=>'Partnerships','7'=>'Sales and operations','8'=>'Administrative services','9'=>'Business strategy planning','10'=>'Finance solutions','11'=>'Legal and government relations','12'=>'Marketing and communications','13'=>'Real estate and workplace services','14'=>'Social impact solutions','15'=>'Consultancy services','16'=>'Investors and funding'); 
 

 
    if(isset($new_array)) 
 
\t $services = array_column($services,'service_offered'); 
 
     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/> 
 
     <?php } 
 
     else 
 
     { 
 
?> 
 
      <input type="checkbox" name='service[]' value="<?php echo $key ?>" ><?php echo $val; ?><br/> 
 
     <?php } 
 

 
endforeach; ?> 
 

 
\t \t \t \t \t \t \t 
 
          
 
         </div> 
 
<br><br> 
 
         <div class="form-group"> 
 
          <label>Locations</label> 
 
          <div class="checkbox"> 
 
           <label> 
 
            <input type="checkbox" name='locations' value="1" <?php if($det->locations==="Hyderabad")echo "checked";?>>Hyderabad 
 
           </label> 
 
          </div> 
 
          <div class="checkbox"> 
 
           <label> 
 
            <input type="checkbox" name='locations' value="2" <?php if($det->locations==="Pune")echo "checked";?>>Pune 
 
           </label> 
 
          </div> 
 
          <div class="checkbox"> 
 
           <label> 
 
            <input type="checkbox" name='locations' value="3" <?php if($det->locations==="Bangalore")echo "checked";?>>Banglore 
 
           </label> 
 
          </div> 
 
          <div class="checkbox"> 
 
           <label> 
 
            <input type="checkbox" name='locations' value="4" <?php if($det->locations==="Delhi")echo "checked";?>>Delhi 
 
           </label> 
 
           <div class="checkbox"> 
 
            <label> 
 
             <input type="checkbox" name='locations' value="5" <?php if($det->locations==="Kolkata")echo "checked";?>>Kolkata 
 
            </label> 
 
           </div> 
 
           <div class="checkbox"> 
 
            <label> 
 
             <input type="checkbox" name='locations' value="6" <?php if($det->locations==="Chennai")echo "checked";?>>Chennai 
 
            </label> 
 
           </div> 
 
          </div> 
 
          <button type="submit" value="Update" class="btn btn-primary">Update</button> 
 
          </form>

Controller ::

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

Модель ::

public function add_vendor_services($id,$service) 
 
\t \t { 
 
\t \t \t $query = $this->db->get('vendor_services'); 
 

 
      foreach ($query->result() as $row) 
 
      { 
 
      $ids[]=$row->id; 
 
\t \t \t 
 
      } 
 
\t \t \t if(isset($ids)){ 
 
      foreach($ids as $vid){ 
 
\t \t \t if($id==$vid){ 
 
\t \t \t \t $q1 =$this->db->get_where('vendor_services',array('id'=>$vid)); 
 
\t \t \t \t $serv=$q1->row()->service_offered; 
 
\t \t \t \t if($serv===$service) 
 
\t \t \t \t \t return; 
 
\t \t \t \t else{ 
 
\t \t $this->db->set('id',$id) 
 
\t \t   ->set('service_offered',$service) 
 
\t \t   ->insert('vendor_services'); 
 
\t \t \t return; 
 
\t \t \t }}else{$this->db->set('id',$id) 
 
\t \t   ->set('service_offered',$service) 
 
\t \t   ->insert('vendor_services'); 
 
\t \t \t \t return; 
 
\t \t \t \t } 
 
\t \t \t }} 
 
\t \t else 
 
\t \t \t {$this->db->set('id',$id) 
 
\t \t   ->set('service_offered',$service) 
 
\t \t   ->insert('vendor_services'); 
 
\t \t \t \t return; 
 
\t \t \t \t } 
 
\t \t \t 
 
\t \t 
 
\t \t } 
 
\t \t

У меня есть таблица в базе данных с именем таблицы: vendor_services. Он содержит «id» и «service_offered». Я пытаюсь получить идентификатор поставщика и службу из формы, где служба находится в массиве, и поместить ее в таблицу vendor_services. Но я не хочу вставлять службу для определенного идентификатора поставщика, который уже существует.

Предположим, что service1 для поставщика 1 уже присутствует в таблице, если service1 снова появляется для идентификатора поставщика 1, его не следует вставлять.

ответ

0

Если в vendor_services (id, service_offered) нет уникального ключа, вам необходимо вручную вручную проверить комбинацию, а затем решить, куда вставить или нет. Это должно работать для этой ситуации:

public function add_vendor_services($id,$service) { 
      $this->db->select('*')->from('vendor_services') 
       ->where('id',$id) 
       ->where('service_offered',$service); 

      if($this->db->get()->num_rows() > 0){ 
       // already have this service for this vendor 
      } else { 
       // need to add this service to this vendor 
       $this->db->insert('vendor_services',array('id'=>$id,'service_offered'=>$service)); 
      } 
} 

Если есть уникальный ключ на идентификатор & service_offered столбцов, то вы можете просто сделать это:

public function add_vendor_services($id,$service) { 
    $this->db->replace('vendor_services',array('id'=>$id,'service_offered'=>$service)); 
} 
+0

Спасибо дружище свою работу !! –