codeigniter 3.0.3上的num_rows问题

时间:2015-11-05 20:31:08

标签: php codeigniter

当我使用CodeIgniter 3.0.3时,我遇到NUM_ROWS function()无法正常工作的问题......

我有这样的代码:

控制器

function search()
{
      // Filter input keyword
      $keyword = $this->input->get('keyword');
      $data['searchtext'] = strip_tags($keyword);
      $check = strlen(preg_replace('/[^a-zA-Z0-9]/', '', $keyword));
      if($check > 3 && $check != NULL):

           if(isset($_GET['page']))
           {
                 $page = ($_GET['page'] != NULL) ? $this->security->xss_clean($_GET['page']) : 1;
            }
            else
            {
                 $page = 1;
            }

            $limit = 15;
            $total = $this->m_songs->total_data_search($keyword); 

            $config['base_url']  = site_url().'songs/search_keyword?keyword='.$keyword;
            $config['total_rows'] = $total;
            $config['per_page']  = $limit;
            $config['page_query_string'] = TRUE;
            $config['use_page_numbers'] = TRUE;
            $config['query_string_segment'] = 'page';
            $config['uri_segment']  = 4;
            $config['full_tag_open'] = '<ul class="pagination pagination-sm">';
            $config['full_tag_close'] = '</ul>';
            $config['first_tag_open'] = '<li>';
            $config['first_link'] = '&laquo; First';
            $config['first_tag_close'] = '</li>';
            $config['last_tag_open'] = '<li  class="last-page">';
            $config['last_link'] = 'Last &raquo;';
            $config['last_tag_close'] = '</li>';
            $config['next_link'] = '<span aria-hidden="true">&raquo;</span><span class="sr-only">Next</span>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_link'] = '<span aria-hidden="true">&laquo;</span><span class="sr-only">Previous</span>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['num_tag_open'] = '<li class="page">';
            $config['num_tag_close'] = '</li>';

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

            $offset = ($page  == 1) ? 0 : ($page * $config['per_page']) - $config['per_page'];
            $result = $this->m_songs->get_paging_search($keyword,$limit,$offset);

            $data['pagination'] = $this->pagination->create_links();
            $data['total'] = $total;

            if($result)
            {
                 $data['offset'] = $offset;
                 $data['dummies'] = $result;
            }
            else
            {
                 $data['dummies'] = NULL;
            }
            else:
                    $data['total'] = 0;
                    $data['dummies'] = NULL;
                    $data['pagination'] = NULL;
            endif;


            $data['team'] = $this->m_home->get_team();
            $data['contact'] = $this->m_home->get_contact();
            $data['pagetitle'] = 'Songs Library';
            $data['title'] = 'Tirtasvara Choir & Chamber Orchestra';
            $data['body'] = $this->load->view('songs', $data, TRUE);

            $this->load->view('header');
            $this->load->view('songs',$data);
            $this->load->view('footer');
    }

模型

function total_data_search($keyword)
{
     $query = $this->db->like('no_reg', $keyword)
                       ->or_like('judul_lagu', $keyword)
                       ->or_like('pencipta', $keyword)
                       ->or_like('aransemen', $keyword)
                       ->or_like('penulis_lirik', $keyword)
                       ->or_like('genre', $keyword)
                       ->or_like('gaya', $keyword)
                       ->or_like('bahasa', $keyword)
                       ->or_like('tema', $keyword)
                       ->or_like('masa_liturgi', $keyword)
                       ->or_like('kelompok_ritus', $keyword)
                       ->get('database_lagu');

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

查看

<div class="row">
   <div class="col-xs-12 col-sm-12 col-md-12">
     <?php if(isset($searchtext)): ?>
         <div class="alert alert-success" role="alert">
             <h4>Found <span class="text-info"><?php echo $total; ?></span> reference with keyword: <span class="text-info"><?php echo $searchtext;?></span></h4>
          </div>
      <?php endif; ?>
    </div>
 </div>

当我执行搜索功能并显示结果时,为什么函数会产生空结果?

但是当我使用CodeIgniter 2.X时一切顺利。 如果有人可以帮助解决这个问题?

sample picture when using Codeigniter 3.0.3

sample picture when using Codeigniter 2.X

2 个答案:

答案 0 :(得分:1)

您在if语句中使用$query->num_rows作为属性。将它用作方法:

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

有关CI forums:

的详情

答案 1 :(得分:0)

尝试使用count()

function total_data_search($keyword)
{
    $query = $this->db->like('no_reg', $keyword)
             ->or_like('judul_lagu', $keyword)
             ->or_like('pencipta', $keyword)
             ->or_like('aransemen', $keyword)
             ->or_like('penulis_lirik', $keyword)
             ->or_like('genre', $keyword)
             ->or_like('gaya', $keyword)
             ->or_like('bahasa', $keyword)
             ->or_like('tema', $keyword)
             ->or_like('masa_liturgi', $keyword)
             ->or_like('kelompok_ritus', $keyword)
             ->get('database_lagu');

    $result = $query->result_array();
    $count = count($result);
    if(!empty($count))
    {
            return $count;
    }
    else
    {
            return NULL;
    }
}