Dynamic drop down from database in codeigniter

4k views Asked by At

I have two dropdowns, I want to fill (deviceType) from table device_typt_table with two columns id, device_type and (brand) from brand_table with id, brand_name, device_type_id.

I try to do that and print data with controller but can't print it in select box but can print in my view Except select box

My view:

<html>
<head></head>
</body>
<?php echo validation_errors(); ?>
<?php echo form_open('speed/insert_to_db'); ?>


Branch: <input type="text"  name="branch" /><br/>
Business Unit: <input type="text"  name="buinessUnit" /><br/>

Device Type:<select name="deviceType" >
               <? foreach $devices as $post { ?>
              <option value="<?php echo $post->device_type;?>"><?php echo $post->device_type;?></option>
                <? }?>
          </select><br/>
Brand:<select name="brand" >

          </select><br/>
Device Model: <input type="text"  name="deviceModel" /><br/>
SN: <input type="text"  name="SN" /><br/>
status: <input type="text"  name="status" /><br/>
department: <input type="text"  name="department" /><br/>
username: <input type="text"  name="username" /><br/>
notes: <input type="textarea"  name="notes" /><br/>
computername: <input type="text"  name="computerName" /><br/>
Save:<input type="submit"  name="submit" value="save" />


<?php foreach($devices as $post){?>
<?php echo $post->device_type;?>
<?php }?>  


<?php echo form_close(); ?>
</body>
</html>

Model:

    <?php
    class add_model extends CI_Model {

           public function insert_into_db(){
               $post=$this->input->post();
               $data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
               $this->db->insert('hardware_assets', $data);
    return $this->db->insert_id(); // if using mysql

           }
    function all_devices()
    {
     $this->db->select("id,device_type");
      $this->db->from('device_type_table');
      $query = $this->db->get();
      return $query->result();
    } 


     }

controller:

<?php

class Speed extends CI_Controller {

        function insert_to_db()
           {
               $this->load->helper(array('form', 'url'));
              $this->load->library('form_validation');
               $this->form_validation->set_rules('branch', 'Branch', 'trim|required|xss_clean');
               $this->form_validation->set_rules('buinessUnit', 'BuinessUnit', 'trim|required|xss_clean');
               $this->form_validation->set_rules('deviceType', 'DeviceType', 'trim|required|xss_clean');
               $this->form_validation->set_rules('brand', 'Brand', 'trim|required|xss_clean');
               $this->form_validation->set_rules('deviceModel', 'DeviceModel', 'trim|required|xss_clean');
               $this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean|is_unique[hardware_assets.SN]');
               $this->form_validation->set_rules('status', 'Status', 'trim|required|xss_clean');
               $this->form_validation->set_rules('department', 'Department', 'trim|required|xss_clean');
               $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
               $this->form_validation->set_rules('notes', 'Notes', 'trim|required|xss_clean');
               $this->form_validation->set_rules('computerName', 'ComputerName', 'trim|required|xss_clean');
            /*   

           */
$this->data['devices'] = $this->add_model->all_devices(); // calling Post model meAthod all_devices()
   $this->load->view('pages/home', $this->data);

           if ($this->form_validation->run() == FALSE)
             { 

             $this->load->view('pages/home');//loading success view     
         }
         else
         {
             $formSubmit = $this->input->post('submit');
                 if( $formSubmit == 'save' ){
                     $this->add_model->insert_into_db();
                     //loading success view
                     redirect('speed/insert_to_db');
                    }
             }
           }
}
1

There are 1 answers

6
David Coder On

You have to fetch your all devices data and brand data from controller then pass it in view.

In view you have to print all devices and band in foreach loop and create options.

For Example:

In Cotroller:

$this->load->model('add_model');
$data['devices']=$this->add_model->all_devices();
$data['brand']=$this->add_model->all_brands();
$this->load->view('your view for insert this data');

In Model:

function all_devices()
{
   code for fetch all devices
}
function all_brand()
{
   code for fetch all brands
}

In view :

Device Type:<select name="deviceType" >
               <? foreach ($devices as $d) { ?>
              <option value="<?php echo $d['device_id']?>"><?php echo $d['device_name']?></option>
                <? }?>
          </select><br/>
Brand:<select name="brand" >
                <? foreach($brands as $b) { ?>
              <option value="<?php echo $b['brand_id']?>"><?php echo $b['brand_name']?></option>
                <? }?>
          </select><br/>

Hope this will Help you.