在CodeIgniter中使用分页搜索数据

时间:2014-02-24 02:43:50

标签: php codeigniter

我的代码中存在问题 我尝试在我的网站上创建搜索功能。 但我发现错误,我搜索的数据没有显示......

这是我控制器上的代码(news.php)

function search_news() {
    if(isset($_POST['submit']))
        {
        $data['sinopsis'] = $this->input->post('sinopsis');

            //set session user data untuk pencarian, untuk paging pencarian
            $this->session->set_userdata('sess_sinopsis', $data['sinopsis']);
        } else {
            $data['sinopsis'] = $this->session->userdata('sess_sinopsis');
        }

        $this->db->like('sinopsis', $data['sinopsis']);
        $this->db->from('news');

        //Pagination init
        $pagination['base_url']     = base_url().'/pencarian/index/page/';
        $pagination['total_rows']   = $this->db->count_all_results();
        $pagination['full_tag_open'] = "<p><div class=\"pagination\">";
        $pagination['full_tag_close'] = "</div></p>";
        $pagination['cur_tag_open'] = "<span class=\"current\">";
        $pagination['cur_tag_close'] = "</span>";
        $pagination['num_tag_open'] = "<span class=\"disabled\">";
        $pagination['num_tag_close'] = "</span>";
        $pagination['per_page']     = "3";
        $pagination['uri_segment'] = 4;
        $pagination['num_links']    = 4;

        $this->pagination->initialize($pagination);

        $data['query'] = $this->news_model->SearchResult($pagination['per_page'],$this-   >uri->segment(4,0),$data['sinopsis']);

        $this->load->vars($data);
        $data['hasil5'] = $this->news_model->tampil_populer(2);
        $data = array_merge($data,admin_info());
        $this->parser->parse('frontend/indeks/search',$data);
}

在我的模型上(news_model.php)

function SearchResult($perPage, $uri, $sinopsis) {
    $this->db->select('*');
    $this->db->from('news');
    if (!empty($sinopsis)) {
        $this->db->like('sinopsis', $sinopsis);
    }
    $this->db->order_by('id', 'asc');
    $getData = $this->db->get('', $perPage, $uri);

    if ($getData->num_rows() > 0)
        return $getData->result_array();
    else
        return null;
}

和我的观点(search.php)

<?php
    if(count($query) > 0) {
    foreach ($query as $row){
?>

<?php

?>  
  <p><strong><?php echo $row['kategori']; ?></strong></p>
  <p><?php echo anchor($row['kategori'].'/detail/'.$row['id'],$row['title']) ?></a></p>
  <p><?php echo $row['sinopsis']; ?></p>
  <p>Dimuat Pada : <?php echo $row['created_at']; ?>   |  Read :<?php echo $row['viewed']; ?> Times</p>
  <hr>
<?php
 }
 } else {
    echo "<div><strong>Data not found, try again!!.</strong></div>";
    }
?>
<?php echo $this->pagination->create_links(); ?>
你能说出我的错吗? 感谢

1 个答案:

答案 0 :(得分:0)

控制器搜索

$search_term = 'psj7293'; //or

$search_term = $this->input->post('Input type value name here');

$search_term = $this->input->post('bde_name');
$this->session->set_userdata('search_term', $search_term);
$config['base_url'] = base_url().'Profiledetails/search/';
                    $config['total_rows'] = $this->Search_model->countselect($search_term);
                    $config['per_page'] = 1; 
                   $this->pagination->initialize($config);
                    $pages = $this->uri->segment(3)?$this->uri->segment(3):0;

                      $resulted = $this->Search_model->select($config['per_page'],$pages,$search_term);

                       $this->load->view('searchlist',array('resulted' => $resulted,'pagination' => $this->pagination->create_links()));





        }