用于社交网络系统的php分页

用于社交网络系统的php分页,php,pagination,social,Php,Pagination,Social,我正在开发一个简单的社交网络系统。这是我的代码,它显示了登录用户的所有朋友的列表,每个人的名字前面都有一个取消朋友按钮。我需要一个页面,一次只显示5个朋友。在页面底部显示下一个和上一个链接。加载的第一页没有“上一页”链接,最后一页不应该有“下一页”链接。任何帮助都将不胜感激 <?php require_once("settings.php"); $conn = @mysqli_connect($host,$user,$pswd) or die('Failed to connect t

我正在开发一个简单的社交网络系统。这是我的代码,它显示了登录用户的所有朋友的列表,每个人的名字前面都有一个取消朋友按钮。我需要一个页面,一次只显示5个朋友。在页面底部显示下一个和上一个链接。加载的第一页没有“上一页”链接,最后一页不应该有“下一页”链接。任何帮助都将不胜感激

<?php
require_once("settings.php");   
$conn = @mysqli_connect($host,$user,$pswd) or die('Failed to connect to server');//connecting to the database
@mysqli_select_db($conn,$dbnm) or die('Database not available');//unless error
//if ((isset($_POST ["$log "]) && (!empty ($log)))){
//getting profile names and ids of the friends of the logged in user 
$query = "SELECT friends.profile_name,friends.friend_id FROM friends INNER JOIN myfriends ON friends.friend_id=myfriends.friend_id1
            WHERE myfriends.friend_id2='$friendID'";

//unless error
$results = @mysqli_query($conn, $query) or die("<p>Unable to execute the query 011.</p>". "<p>Error code " . mysqli_errno($conn). ": " . mysqli_error($conn)) . "</p>";;
$count=mysqli_num_rows($results);//row count
$row = mysqli_fetch_row($results);//fetching a row from db and soring it inside a variable

if(isset($_GET ["unfriend"]))//if unfriend variable is set
{

    $unfriend=$_GET["unfriend"];
    //echo $friend_id = $row[1];
    //$query ="DELETE FROM myfriends WHERE (friend_id1=".$unfriend." and friend_id2=".$friendID.") OR (friend_id1=".$friendID." and friend_id2=".$unfriend.")";
    //deleting the mutual friendship 
    $query ="DELETE FROM myfriends WHERE (friend_id1=$unfriend and friend_id2=$friendID) OR (friend_id1=$friendID and friend_id2=$unfriend)";
    $results = @mysqli_query($conn, $query) or die("<p>Unable to execute the query 011.</p>". "<p>Error code " . mysqli_errno($conn). ": " . mysqli_error($conn)) . "</p>";;

    //getting num of friends of the deleted friend 
    $query = "SELECT num_of_friends FROM friends WHERE friend_id='$unfriend'";

    $results = @mysqli_query($conn, $query) or die("<p>Unable to execute the query 013.</p>". "<p>Error code " . mysqli_errno($conn). ": " . mysqli_error($conn)) . "</p>";;

    $row = mysqli_fetch_row($results);
    $friendcount=$row[0];
     $friendcount--;
     //updating the number of friends of the deleted friend
    $query2 ="UPDATE friends  
            SET num_of_friends='$friendcount'
            WHERE friend_id='$unfriend'";
    $results2= @mysqli_query($conn, $query2) or die("<p>Unable to execute the query 01234.</p>". "<p>Error code " . mysqli_errno($conn). ": " . mysqli_error($conn)) . "</p>";;

    //getting profile names and ids of the friends of the logged in user 
    $query = "SELECT friends.profile_name,friends.friend_id,friends.num_of_friends FROM friends INNER JOIN myfriends ON friends.friend_id=myfriends.friend_id1
            WHERE myfriends.friend_id2='$friendID'";

    $results = @mysqli_query($conn, $query) or die("<p>Unable to execute the query 013.</p>". "<p>Error code " . mysqli_errno($conn). ": " . mysqli_error($conn)) . "</p>";;
    $count=mysqli_num_rows($results);

    //updating the number of friends of the logged in user.
    $query1 ="UPDATE friends   
            SET num_of_friends='$count'
            WHERE friend_id='$friendID'";
    $results1= @mysqli_query($conn, $query1) or die("<p>Unable to execute the query 012.</p>". "<p>Error code " . mysqli_errno($conn). ": " . mysqli_error($conn)) . "</p>";;       
    //updating the session variable
    $numoffriends=$count;
    $_SESSION["numoffriends"]=$numoffriends;    

    $row = mysqli_fetch_row($results);


}


    echo "<p>Total Number of friends is $count</p>";

echo "<table width='50%' border='1'>";
while ($row) {
    echo "<tr><td>{$row[0]}</td>";
    ?>
    <td><button onclick = "window.location.href='friendlist.php?unfriend=<?php echo $row[1];?>'">Unfriend</button></td></tr>
    <?php

    $row = mysqli_fetch_row($results);
}
echo "</table>";



echo"<p><a href =\"friendadd.php\">Add Friends</a><a href =\"logout.php\">Log Out</a></p>"; 



mysqli_free_result($results);


mysqli_close($conn);
?>

您可以通过limit语句来实现。您需要设置一些变量,如:

  • $records_per_page-负责显示记录的变量
  • $current_page-保存当前页面的变量
您的查询需要基于限制x,y

x-要跳过多少条记录 y-显示多少条记录

因此,如果有人把1作为当前页面,它将类似于

限制$current\u page*$records\u per\u page,$records\u per\u page

此外,您应该无限制地计算查询中有多少记录,这样您就可以使用for循环创建页面链接

另一个好的选择是为此使用适当的已经编写好的类。我使用的最好的类是Zend_Paginator


如果您不想使用此类,请检查本教程:

请格式化您的代码