99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

PHP實(shí)現(xiàn)無限循環(huán)獲取MySQL中的數(shù)據(jù)

發(fā)布時(shí)間:2017-09-03 23:31  回復(fù):0  查看:2364   最后回復(fù):2017-09-03 23:31  


本文和大家分享的主要是php實(shí)現(xiàn)無限循環(huán)獲取MySQL中的數(shù)據(jù)相關(guān)內(nèi)容,一起來看看吧,希望對(duì)大家 學(xué)習(xí)php有所幫助。
  最近有個(gè)需求需要從MySQL獲取數(shù)據(jù),然后在頁面上無線循環(huán)的翻頁展示。主要就是一直點(diǎn)擊一個(gè)按鈕,然后數(shù)據(jù)從最開始循環(huán)到末尾,如果末尾的數(shù)據(jù)不夠了,那么從數(shù)據(jù)的最開始取幾條補(bǔ)充上來。
  其實(shí),這個(gè)功能可以通過JS+PHP實(shí)現(xiàn),也可以通過PHP + MYSQL+JS實(shí)現(xiàn),只不過JS+PHP比較方便而且效率更高罷了。
  下面是PHP + MYSQL+JS實(shí)現(xiàn)辦法。
  每次顯示10條數(shù)據(jù)。
  1 public function get_data($limit){2       $sql="select * from ((select id,name from `mytable` limit {$limit},10) union all (select id,name from `mytable` limit 0,10)) as test limit 0,10";3
     return $this->query($sql);4 }
  上述sql語句通過mysql的union all方法,把兩個(gè)集合拼接到一起,并取前十條數(shù)據(jù)。
  1  public function get_count(){//獲取數(shù)據(jù)的條數(shù) 2
  $sql="select count(id) as t from `mytable`"; 3
  return $this->query($sql); 4 }
  下一步在控制器中獲取數(shù)據(jù),并給ajax提供數(shù)據(jù)接口。
  //測(cè)試數(shù)據(jù)庫無限循環(huán)取數(shù)據(jù)
  public function getInfiniteData(){
  //用戶點(diǎn)擊數(shù)
  $page = $_GET['click'];
  //每次展示條數(shù)
  $pagesize = 10;
  //獲取總條數(shù)
  $total = $this->Mydemo->get_count();
  $t = $total[0][0]['t'];
  //算出每次點(diǎn)擊的其起始位置
  $limit = (($page - 1)*$pagesize)%$t;
  $data = $this->Mydemo->get_data($limit);
  if (!empty($data)) {
  //轉(zhuǎn)換為二維數(shù)組
  $list = [];
  foreach ($data as $key => $v) {
  $list[$key] = $data[$key][0];
  }
  $info['msg'] = $list;
  $info['code'] = '001';
  }else{
  $info['code'] = '002';
  $info['msg'] = '暫無數(shù)據(jù)';
  }
  echo json_encode($info,JSON_UNESCAPED_UNICODE);die;
  }


來源:Linux公社

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?