使用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.

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>';

Remove this line -- it's not doing anything:

$start_from, 20"; // use LIMIT (and options) to make sure only 20 are displayed

You'd already defined $start_from here:
$start_from = ($page-1) * 20;

Then check out your results and see if you are all set.