duangang4940 2013-11-14 17:02
浏览 14
已采纳

使用sql和php的多页结果

I'm having trouble with the following code from a tutorial, it seems very simple but there must be something I'm missing. There is nothing at all where my numbered page menu should be, can anyone see why?

if (isset($_GET["page"])) { // get page number for query
$page = $_GET["page"];
}
else {
$page = 1; // no page number? set it
};

$start_from = ($page-1) * 20;

$query = "SELECT * FROM posts";
$query.= " WHERE isstart = 'y' AND iscomplete = 'n' ORDER BY date DESC LIMIT $start_from, 20";             
$start_from, 20"; // use LIMIT (and options) to make sure only 20 are displayed

$result = mysql_query($query);

$query_count = "SELECT COUNT(post_ID) FROM posts WHERE isstart = 'y'"; 
$count_result = mysql_query($query_count); 
$count_results = mysql_fetch_row($count_result); 
$total_posts = $count_results[0];
$total_pages = ceil($total_posts / 20); // get total pages needed for page menu

for ($i=1; $i<=$total_pages; $i++) { // set the page numbers
$pagelink = "Page: <a href='index_test.php?page=".$i."'>".$i."</a>"; // make the page menu
}; 

$top_body_text = '<p align="left">'.$pagelink.'</p>';

Currently my statement echo'ing $pagelink creates noting.

  • 写回答

2条回答 默认 最新

  • dongqiulei6805 2013-11-14 17:20
    关注

    First thing, there is a error on this line

    $start_from, 20"; 
    

    It should be

       $start_from = 20; 
    

    and lastly, you need to echo '$pagelink' inside the for loop in order to see each pages number listed out. Example below:

    if (isset($_GET["page"]))
     { // get page number for query
       $page = $_GET["page"];
     }
    else {
       $page = 1; // no page number? set it
     };
    
     $start_f = 20; // use LIMIT (and options) to make sure only 20 are displayed
     $start_from = ($page-1) * $start_f;
    
     $query = "SELECT * FROM posts";
     $query.= " WHERE isstart = 'y' AND iscomplete = 'n' ORDER BY date DESC LIMIT $start_from, 20";             
    
    
    
      $result = mysql_query($query);
    
     $query_count = "SELECT COUNT(post_ID) FROM posts WHERE isstart = 'y'"; 
     $count_result = mysql_query($query_count); 
      $count_results = mysql_fetch_row($count_result); 
     $total_posts = $count_results[0];
    
     $total_pages = ceil($total_posts / 20); // get total pages needed for page menu
    
     for ($i=1; $i<=$total_pages; $i++)
      { // set the page numbers
       echo $pagelink = "Page: <a href='index_test.php?page=".$i."'>".$i."</a>"; ///Echo here
      }; 
      $top_body_text = '<p align="left">'.$pagelink.'</p>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!