CI 分頁問題
function searchs($offset=''){
$this->load->library('pagination'); // loading pagination
$config['base_url'] = base_url()."gf/searchs"; //路徑位置
$config['per_page'] = '15'; // 每頁15筆
$config['full_tag_open'] = '<div id = "page_nav">';
$config['full_tag_close']= '</div>';
$config['first_link'] = '首頁';
$config['last_link'] = '末頁';
$config['next_link'] = '下一頁>';
$config['prev_link'] = '<上一頁'; // << 1 2 3 4 5 > >> 上下頁碼
$config['uri_segment'] = '3';
$devices = $this->input->get('devices');
$foodname = $this->input->get('foodname');
$this->input->set_cookie($devices,$foodname);
$this->input->cookie('foodname', TRUE);
$this->db->start_cache(); // 開始做快取
$this->db->select('*');
$this->db->from('abc');
$this->db->order_by("ID","ASC"); // 排序
if($devices == 0 && $foodname != null) // $devices 改成 0
{
$this->db->like('NAME',"$foodname");
//$query =$this->db->get(); // get() 在判斷後,判斷僅針對SQL條件
}
else if($devices != 0 && $foodname == null)
{
$this->db->where('class',"$devices");
//$query =$this->db->get();
}
else if($devices!=0 && $foodname!=null)
{
$this->db->like('NAME',"$foodname");
$this->db->where('class',"$devices");
//$query =$this->db->get();
}
$this->db->stop_cache(); // 停止快取
$query =$this->db->get(); // 抓取全部資料
$config['total_rows'] = $query->num_rows(); // 全部資料行數
echo $query->num_rows();
$this->pagination->initialize($config); // 設定完成
$this->db->limit($config['per_page'],$offset); // 疊加SQL指令 : 出現行數(15行)
$query_data =$this->db->get(); // 要輸出在畫面上的資料
echo $this->pagination->create_links(); // 產生分頁條
echo "<table border=2>";
echo "<th>".'食材名稱'."<th>".'熱量'."<th>".'水分'."<th>".'備註'."</tr>";
echo $foodname;
foreach ($query_data->result() as $row)
{
echo "<td>".$row->NAME."</td>";
echo "<td>".$row->heat."</td>";
echo "<td>".$row->water."</td>";
echo "<td>"
?>
<a href="gf/detail/<?php echo($row->ID)?>" target="_blank">查看更多</a>
<?php
echo "</tr>";
}
$call_arr = array('type' => $devices , 'f_name' => $foodname);
$this->load->view('gf_page' , $call_arr);
}
以上是我的程式碼
我在搜尋欄輸入完需求之後
第一頁是跑得出來的
但是點選第二頁之後,我取不到我一開始的值,筆數也完全不正確,再點選回上一頁筆數也是完全亂掉。
第一次發文多多指教...
你贴出的代码不全,还有因我个人水平有限,部分代码看不懂。我个人观点是分页时总页数,$config['total_rows']是搜索完需求之后,数据库筛选出的总条数 ,根据你的代码取出的是未搜索 总行数。 $query =$this->db->get(); // 抓取全部資料
$config['total_rows'] = $query->num_rows(); // 全部資料行數
echo $query->num_rows();
这两个不一致,可能会导致分页混乱。
greedefoxes 发表于 2015-8-10 09:15
你贴出的代码不全,还有因我个人水平有限,部分代码看不懂。我个人观点是分页时总页数,$config['total_r ...
我感觉你的代码并没有根据CI的MVC框架结构来搭建。请教下$this->db->start_cache(); // 開始做快取 和//停止快取 这两行代码是什么意思?中断吗?如果是我 这两行之间的搜索代码 我会放在Model文件中。
快取跟停止快取
是依據我在表格內所輸入的值傳到後台進行搜尋
然後
$config['total_rows'] = $query->num_rows(); // 全部資料行數
echo $query->num_rows();
這兩行是同樣的我只是要看筆數的跑動才加上echo那段的
kai 发表于 2015-8-11 14:43
快取跟停止快取
是依據我在表格內所輸入的值傳到後台進行搜尋
然後
既然这样 那问题会不会出现在url上?我看你的 $config['base_url'] = base_url()."gf/searchs"; //路徑位置没有index.php 我的代码是这样写的:$config['base_url'] = base_url().'index.php/user_meterial/user_meterial_updata/pagination/'; //导入分页类URL
$config['uri_segment'] = 4;
我若搜尋全部的資料是可以的
所以URL是沒問題
只有想依照我個人需求搜尋是行不通而已
是有想嘗試像youtube google 他們的搜尋欄位
搜尋完之後我點選下一頁搜尋欄位的文字並不會不見
我是覺得假如我點選下一頁欄位文字如果沒有不見或者我用get記錄到網址再用程式去抓網址的輸入值
這問題應該就是可以解決 但我尚未找到相關資訊
kai 发表于 2015-8-11 15:36
我若搜尋全部的資料是可以的
所以URL是沒問題
只有想依照我個人需求搜尋是行不通而已
那就是用 ajax 做无刷新分页,或者等页面重新刷新后再给搜索栏赋值。
已解決謝謝
页:
[1]