使用ajax在codeigniter中实时搜索操作

时间:2018-04-11 05:43:45

标签: javascript php jquery ajax codeigniter

我是codeIgniter的新手。想要学习这个。我面临着问题。搜索页面中未填充数据。

在模型中

function fetch_data($query)
{
   $this->db->select('*');
        $this->db->from('casedata');
        if($query!='')
        {
            $this->db->like('status',$query);

                }
                $this->db->order_by('SrNo','DESC');
                return $this->db->get();
    }

在控制器中,获取函数无法从数据库中获取数据

    function fetch()
        {
             $output='';
            $query='';
            $this->load->model('crud_model');
            if ($this->input->post('query'));
            {
               $query= $this->input->post('query');
                            }
                            $data=$this->Crud_model->fetch_data($query);
                            $output .='<div class="table-responsive">
                                <table class="table table_bordered table-striped
                                <tr>
                                <th>SrNo.</th>
                                <th>Tile</th>
                            <th>File No.</th>
                            <th>Division</th>
                            <th>Section</th>
                            <th>LDH</th>
                         <th>NDH</th>
                            <th>PDH</th>
                            <th>STATUS</th>
                            </tr>';
                            if($data->num_rows()>0)
                            {
                                foreach($data->result() as $row)
                                {
                                    $output.='<tr> <td>'.$row->SrNo.'</td>
                                   $output.='<tr> <td>'.$row->title.'</td>
                            <td>'.$row->fileno.'</td>
                            <td>'.$row->division.'</td>
                            <td>'.$row->section.'</td>
                            <td>'.$row->ldh.'</td>
                            <td>'.$row->ndh.'</td>
                            <td>'.$row->pdh.'</td>
                            <td>'.$row->status.'</td></tr>';
                            }
                            }

                else{
                $output.='<tr> <td colspan="5">No Data Found</td></tr> ';
            }
                $output.='</table>';
                echo $output;
             }

在视图中

  <html>       
    <body>   
      <div class="row">
        <div class="col-md-12">
           <div class="panel panel-primary" data-collapsed="0">
               <div class="panel-heading">
                  <div class="panel-title" >
                     <i class="entypo-plus-circled"></i>
                     <?php echo get_phrase('Search Data');?>
                  </div>
               </div>
               <div class="panel-body">                 
                  <?php echo form_open(base_url() . 'index.php? 
   admin/student/create/' , array('class' => 'form-horizontal form-groups- 
   bordered validate', 'enctype' => 'multipart/form-data'));?>      
                  <div class="form-group">
                     <input type="text" name="search_text" id="search_text" 
    placeholder="Search by Customer Detsils" class="form-control"/>                
                     <div id="result">  
                     </div> 
                  </div> 
               </div>    
            </div>
          </div>        
        </div>
    </body>
  </html>

    <script>
    $(document).ready(function()

    {

        load_data();

        function load_data(query)
        {
            $.ajax({
                url:"<?php echo base_url();?>index.php? 
     admin/fetch",method:"POST",
                data:{query:query},
                success:function(data){
                    $('#result').html(data);
                }
            })
                  }
                  $('#search_text').keyup(function(){
                  var search=(this).val();
                  if (search!='')
                  {
                      load_data(search);
                  }
                  else
                  { load_data();
                  }
                  } );
       });
    </script>

表中未填充数据。如何从中获得输出?

1 个答案:

答案 0 :(得分:0)

你需要遍历$ data变量。请更改“如果&#39;并且&#39; foreach&#39;如下面的代码。

if(is_array($data) && count($data)>0)
   {
     foreach($data as $row)
    {

如果不能正常工作,请尝试var_dump($ data)并检查结果是否即将到来。