在codeigniter中从数据库动态下拉

时间:2015-06-07 18:00:57

标签: php codeigniter codeigniter-2

我有两个下拉列表,我想从表deviceType填充(device_typt_tableiddevice_typebrand来自brand_table {1}} id, brand_name, device_type_id

我尝试这样做并使用控制器打印数据,但无法在选择框中打印 但可以在我的视图中打印除了选择框

我的观点:

<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>

型号:

    <?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();
    } 


     }

控制器:

<?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 个答案:

答案 0 :(得分:0)

您必须从控制器获取所有设备数据和品牌数据,然后在视图中传递它。

在视图中,您必须在foreach循环中打印所有设备和波段并创建选项。

例如:

在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');

在模型中:

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

在视图中:

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/>

希望这会帮助你。