2012-07-03 125 views
1

我在該表中有一個表有3個字段(category1,category2,category3)。現在我想填充下拉列表,如果下拉一個選擇,下拉列表中的2個值將從數據庫中genarat。誰能幫我。在codeigniter中從數據庫填充下拉列表

我的控制器

  $this->load->model('quiz_mode'); 
      $arrStates = $this->quiz_mode->get_unique_catg1(); 

      foreach ($arrStates as $states) 
      { 
       $arrFinal[$category->category1 ] = $category->category1 ; 
      } 
      $data['category2'] = $arrFinal; 
     function ajax_call() 
    { 

     //Checking so that people cannot go to the page directly. 
     if (isset($_POST) && isset($_POST['category1'])) 
     { 

      $catogory = $_POST['category1']; 
      $arrcategory = $this->sampl_mode->get_cities_from_catgo1($catogory); 

      foreach ($arrcategory as $category) 
      { 
       $arrFinal[$category->category2] = $category->category2; 
      } 

      //Using the form_dropdown helper function to get the new dropdown. 
      print form_dropdown('category2',$arrFinal); 
     } 
     else 
     { 
      redirect('logged_in_user'); //Else redire to the site home page. 
     } 
    } 

我的模型

function get_unique_catg1() 
{ 

    $query = $this->db->query("SELECT DISTINCT category1 FROM sampl_table"); 

    if ($query->num_rows > 0) { 
     return $query->result(); 
    } 
} 
    function get_cities_from_catgo1($category) { 
    $query = $this->db->query("SELECT category2 FROM sampl_table WHERE category2 = '{$category}'"); 

    if ($query->num_rows > 0) { 
     return $query->result(); 
    } 
} 

我的觀點

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>untitled</title> 

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

       $('#catgo1-dropdown').change(function() { 
        var selCat1 = $('#catgo1-dropdown').val(); 
        console.log(selCat1); 
        $.ajax({ 
         url: "<?php echo base_url();?>index.php/sample/ajax_call", //The url where the server req would we made. 
         async: false, 
         type: "POST", //The type which you want to use: GET/POST 
         data: "category1="+selCat1, //The variables which are going. 
         dataType: "html", //Return data type (what we expect). 

         //This is the function which will be called if ajax call is successful. 
         success: function(data) { 
          //data is the html of the page where the request is made. 
          $('#cotegory').html(data); 
         } 
        }) 
       }); 
      }); 
     </script> 
</head> 

    <body> 
    <div id="wrapper"> 
       <div id="catgo1-dropdown"><?php print form_dropdown('category1'); ?></div> 
       <div id="cotegory2"></div> 

      </div> 
    </body> 

</html> 

回答

2

有這個

第一一個非常簡單的解決方案僅顯示第一個下拉
然後,當用戶選擇某個值通過ajax發送給控制器方法時,該值將被接收並傳遞給模型。
模型應該有查詢與WHERE條件將取回結果
然後回到控制器調用視圖和查詢結果傳遞給這個
你的觀點應該包含這些代碼

<?php 
foreach($queryresult as $row) 
{ 
?> 
<option value = "<?php echo $row->id?>"><?php echo $row->value;?</option> 
<?php 
} 
?> 

在你第一個視圖那裏有一個下拉它應該包含以下代碼

<select name = "first_drop_down" id = "first_drop_down" onchange = "calljavascriptfunction();"> 
<?php foreach($first_drop_down_values as $row){?> 
<option value = "<?php echo $row->id?>"><?php echo $row->value;?</option> 
<?php}?> 

<select name = "second_drop_down" id = "second_drop_down" > 
<option value = "0">please select first dropdown</option> 
</select> 

和Ajax功能

function calljavascriptfunction(){ 


    $.ajax({ 
    type : 'POST', 
    data : 'first_drop_down_id='+ $('#first_drop_down').val(), 
    url : 'controller/method', 
    success : function(data){ 
       $('#second_drop_down').val(data); 
    } 
}); 
} 

完成,這裏是一個教程太

http://mikeheavers.com/index.php/site/code_single/dynamic_dependent_dropdown_filtering_with_codeigniter_and_jquery

+0

謝謝您answer.I試過,但我沒有得到,只有組別只有到來。可你幫我。 – Naveen

0

我的下拉

<script type="text/javascript"> 
    $(document).ready(function() { 
    $('[name="wilaya"]').change(function() { 
      var input= this.value; 
      //alert(input); 
      $.ajax({ 
      url: "<?php echo base_url();?>/register/ajax_call", 
      data: "input="+input 
      }) 
      .done(function(msg) { 
      $(".commune").html(msg); 
      }); }); 
     }); 
    </script> 

和CONTROLER註冊的DIV容器中使用.html(msg); .php方法ajax_call()

public function ajax_call(){ 

if (isset($_REQUEST['input'])) 
     { 
     // echo("hello"); 
     $input = $_REQUEST['input']; 
     $this->load->model('Pro_model'); 
     $commune=$this->Pro_model->get_dropdown_commune($input); 
     //print_r($commune); 
     //$this->load->view('registerpro_view', $commune); 
     echo form_dropdown('commune', $commune, 'Adrar'); 
     } 


} 

,並在模型邊Pro_model.php

function get_dropdown_commune($input) 
{ 
$this->db->from('ea_commune'); 
$this->db->where('id_wilaya', $input); 
$this->db->order_by('id'); 
$result = $this->db->get(); 
$return = array(); 
if($result->num_rows() > 0) { 

foreach($result->result_array() as $row) { 
$return[$row['id']] = $row['label_commune']; 
} 
} 

     return $return; 

}