动态生成页码

时间:2013-12-13 04:36:58

标签: php mysql

我目前有一个index.php页面,它作为“我的书签”页面的索引。该页面将MySQL数据库查询的结果返回到自动生成的表中。在生成一个额外的页面(因此在底部插入页码/链接)之前,代码只允许每页五个记录 - 由于某种原因,我的代码只是停止工作/生成页码。

我的代码出了什么问题?我似乎无法找到问题,我不知道在论坛上搜索什么;任何指导都非常感谢。

这是我的index.php代码:

 <?php
 session_start();
 //check session first
 if (!isset($_SESSION['email'])){
echo "You are not logged in!";
exit();
 }else{
//include the header
include ("../includes/header.php");
require_once ('../../mysql_connect.php');
echo ("<center>"); 
echo ("<div class='bookmarkMenu'><h1 style='text-decoration:underline;'>Q & A Database</h2><p>");
echo ("<p><a class='bookmarkAdd' href=add.php>Add Record</a>    "); 
echo ("<a class='bookmarkSearch' href=searchform.php>Search Records</a></p><hr /></div><br />"); 
//Set the number of records to display per page
$display = 5;
//Check if the number of required pages has been determined
if(isset($_GET['p'])&&is_numeric($_GET['p'])){//Already been determined
    $pages = $_GET['p'];
}else{//Need to determine
    //Count the number of records;
    $query = "SELECT COUNT(ID) FROM bookmark";
    $result = @mysql_query($query); 
    $row = @mysql_fetch_array($result, MYSQL_NUM);
    $records = $row[0]; //get the number of records
    //Calculate the number of pages ...
    if($records > $display){//More than 1 page is needed
        $pages = ceil($records/$display);
    }else{
        $pages = 1;
    }
}// End of p IF.

//Determine where in the database to start returning results ...
if(isset($_GET['s'])&&is_numeric($_GET['s'])){
    $start = $_GET['s'];
}else{
    $start = 0;
}

//Make the paginated query;
$query = "SELECT * FROM answers LIMIT $start, $display"; 
$result = @mysql_query ($query);

//Table header:
echo "<table class='bookmarksTable' cellpadding=5 cellspacing=5 border=1><tr>
<th>ID</th><th>Question</th><th>Answer</th><th>Comment</th><th>*</th><th>*</th></tr>"; 

//Fetch and print all the records...
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<tr><td class='bookmarkInfo'>".$row['ID']."</td>"; 
    echo "<td class='bookmarkInfo'>".$row['Question']."</td>";
    echo "<td class='bookmarkInfo'>".$row['Answer']."</td>"; 
    echo "<td class='bookmarkInfo'>".$row['Comment']."</td>"; 
    echo "<td class='bookmarkInfo'><a href=deleteconfirm.php?id=".$row['ID'].">Delete</a></td>"; 
    echo "<td class='bookmarkInfo'><a href=updateform.php?id=".$row['ID'].">Update</a></td></tr>"; 
} // End of While statement
echo "</table>"; 
mysql_free_result ($result); // Free up the resources.         
mysql_close(); // Close the database connection.

//Make the links to other pages if necessary.
if($pages>1){
    echo '<br/><table><tr>';
    //Determine what page the script is on:
    $current_page = ($start/$display) + 1;
    //If it is not the first page, make a Previous button:
    if($current_page != 1){
        echo '<td><a href="index.php?s='. ($start - $display) . '&p=' . $pages. '"> Previous </a></td>';
    }
    //Make all the numbered pages:
    for($i = 1; $i <= $pages; $i++){
        if($i != $current_page){ // if not the current pages, generates links to that page
            echo '<td><a href="index.php?s='. (($display*($i-1))). '&p=' . $pages .'"> ' . $i . ' </a></td>';
        }else{ // if current page, print the page number
            echo '<td>'. $i. '</td>';
        }
    } //End of FOR loop
    //If it is not the last page, make a Next button:
    if($current_page != $pages){
        echo '<td><a href="index.php?s=' .($start + $display). '&p='. $pages. '"> Next </a></td>';
    }

    echo '</tr></table>';  //Close the table.
}//End of pages links
//include the footer
include ("../includes/footer.php");
 }
 ?>

非常感谢, -Rockmandew

1 个答案:

答案 0 :(得分:0)

你应该从answers表开始计算,然后根据你的页面逻辑进行计数,然后在底部的answers表上查询