动态codeigniter选择不起作用

时间:2015-07-21 04:46:51

标签: codeigniter dynamic

controller
car.php

<?php  
   class Car extends CI_Controller {  
   public function __construct()  
   {  
      parent::__construct();  
      $this->load->database();  
      $this->load->helper('url');  
      $this->load->helper('form');  
      $this->load->model('company_model');  
   }  
   public function index()  
   {  
      //starts by running the query for the countries  
      //dropdown  
      $data['companydrop'] = $this->company_model->company();  
      //loads up the view with the query results  
      $this->load->view('car_view', $data);  
   }  
   //call to fill the second dropdown with the cities  
   public function car_model()  
   {  
      //set selected country id from POST  
      echo $company_id = $this->input->post('company_id',TRUE);  
      //run the query for the cities we specified earlier  
      $cardata['cardrop']=$this->company_model->car($company_id);  
      print_r($cardata);
      $output = null;  
      foreach ($cardata['cardrop'] as $row)  
      {  
         //here we build a dropdown item line for each  
        // query result  
         $output .= "<option value='".$row->car_model."'>".$row->car_model."</option>";  
      }  
      echo $output;  
   }  
}  
?>


model
company_model
<?php  
class Company_model extends CI_Model {  
   public function __construct()  
   {  
      $this->load->database();  
   }  
   //fill your contry dropdown  
   public function company()  
   {  
      $this->db->select('company_id,company_name');  
      $this->db->from('company');  
      $query = $this->db->get();  
      // the query mean select cat_id,category from  
      //category  
      foreach($query->result_array() as $row){  
         $data[$row['company_id']]=$row['company_name'];  
      }  
      // the fetching data from database is return  
      return $data;  
   }  
   //fill your cities dropdown depending on the selected city  
   public function car($company_id=string)  
   {  
      $this->db->select('car_id,car_model');  
      $this->db->from('car');  
      $this->db->where('company',$company_id);  
      $query = $this->db->get();  
      return $query->result();  
   }  
}  

?>
 view 
car_view

<html>
<head>
<title>car dealers</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>  
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>  
               <script type="text/javascript">  
                  $(document).ready(function() {  
                     $("#companydrop").change(function(){  
                     /*dropdown post *///  
                     $.ajax({  
                        url:"<?php echo base_url();?>index.php/car/car_model",

                        data: {id:$(this).val()},  
                        type: "POST",  
                        success:function(data){  
                        $("#cardrop").html(data);
                    alert(data);

                     }  
                  });  
               });  
            });  


</script>
<style>
body{


        no-repeat;
        background:url(../../../video-fallback-background.jpg)

    }
</style>
</head>
<body>

      <!--company dropdown-->  
      <?php echo form_dropdown('companydrop',$companydrop,'','class="required" id="companydrop"'); ?>  
      <br />  
      <br />  
      <!--car dropdown-->  
      <select name="cardrop" id="cardrop">  
         <option value="">Select</option>  
      </select>  
      <br /> 
   </body>  
</html>

动态下拉列表不能作为第一个选择 公司名称正在从数据库中获取,但是汽车模型不起作用,它没有被提取到dropdown.i需要从数据库中获取汽车公司模型然后在选择公司后该指定公司的模型必须是列在第二个dropdown.i中已经在phpmyadmin中创建了数据库,并在公司copany_id和company_name中创建了两个表车和公司,其中汽车中有car_id,car_name和company_id

3 个答案:

答案 0 :(得分:0)

检查此示例代码,以便在codeigniter中创建下拉列表。

<?php
$js = 'id="unicode" class="form-control"';
$unicode = array(
'2' => 'No',
'1' => 'Yes'
);
echo form_dropdown('unicode', $unicode, set_value('unicode'), $js);
?>

这里Dropdown id是unicode,class是form-control。

Html看起来像:

<select name="unicode" id="unicode" class="form-control">
<option value="2">No</option>
<option value="1">Yes</option>
</select>

您可以从数组中获取db中的值,然后将其存储在$unicode之类的变量中。希望这会有所帮助。查看此引用link

根据第一个下拉列表设置另一个下拉列表:

$("#dropdown1").change(function () {
    var end = this.value;
    $('#dropdown2').val(end );         
});

答案 1 :(得分:0)

在您的汽车控制器中请先删除print_r($cardata);。 然后在您的控制台中查看您从通话中获得的响应。我建议你以json格式获取数据并在客户端解析它。这是最好的做法。

答案 2 :(得分:0)

i corrected the code and finally it worked,i will post the correct code, if it helps anyone in future.thanks to everyone who tried to help me..

controller

car.php



 <?php  
class Car extends CI_Controller {  
public function __construct()  
{  

    parent::__construct();  
    $this->load->database();  
    $this->load->helper('url');  
    $this->load->helper('form');  
    $this->load->model('company_model');  
    }  
    public function index()  
    {  


    $data['companydrop'] = $this->company_model->company();  

    $this->load->view('car_view', $data);  
    }  

    public function car_model()  
    {  

    $company_id = $this->input->post('company_id',TRUE);  

    $cardata['cardrop']=$this->company_model->car($company_id);


    $output = null;  

    foreach ($cardata['cardrop'] as $row)  
    {  

    $output .= "<option value='".$row->car_model."'>".$row->car_model."</option>";  

    }  
    echo $output;
    }  

    }  
    ?>

model
company_model

        <?php  
    class Company_model extends CI_Model {  
    public function __construct()  
    {  
    $this->load->database();  
    }  

    public function company()  
    {  
    $this->db->select('company_id,company_name');  
    $this->db->from('company');  
    $query = $this->db->get();  


    foreach($query->result_array() as $row){  
       $data[$row['company_id']]=$row['company_name'];  
    }  

    return $data;  
    }  

    public function car($company_id)  
    {


    $this->db->select('car_id,car_model');  
    $this->db->from('car');  
    $this->db->where('company_id',$company_id); 


    $query = $this->db->get();  

    return $query->result();  
    }  
    }  

    ?>


view
car_view



     <html>
    <head>
    <title>car dealers</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>  
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>  
    <script type="text/javascript">  
    $(document).ready(function() {  


    $("#companydrop").change(function(){  


    /*dropdown post *///  
    $.ajax({  

       url:"<?php echo base_url();?>index.php/car/car_model",                       
       data: {company_id:$(this).val()},  
       type: "POST",  
       success:function(data){ 
               $('#cardrop option[value!=0]').remove()
           $("#cardrop").append(data);  


          }  
       });  
    });  
    });  


    </script>
    <style>
    body{       

    no-repeat;
    background:url(../../../video-fallback-background.jpg)

    }
    </style>
    </head>
    <body>
      <center><font color="#333366"><strong></strong><h2>CR Motors</h2></font></center>
    <center><font color="#FF8000"><h3>Select the car to purchase...</h3></center></font>
    <!--company dropdown-->  
    <tr>
    <td>
    <font color="#00FF99">
    Select the company</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <?php echo form_dropdown('companydrop',$companydrop,'','class="required" id="companydrop"'); ?>  </td>
     </tr>  
    <br />  
    <br />  
    <!--car dropdown-->  
    <tr>
    <td>
    <font color="#00FF99">
    Select the model</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <select name="cardrop" id="cardrop">  
    <option value="0">Select</option>  

    </select>
    </td>
     </tr>  
    <br />  
    </body>  
    </html>