2013-05-18 35 views
0

我正在開發一個簡單的社交網絡系統。這是我的代碼,它顯示登錄用戶的所有朋友列表,並在每個姓名的前面有一個Unfriend按鈕。我需要一個只能顯示5個朋友的頁面。使用頁面底部的下一個和上一個鏈接。第一頁加載沒有「上一頁」鏈接,最後一頁不應該有「下一頁」鏈接。任何幫助,將不勝感激。用於社交網絡系統的php分頁

<?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.</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); 
?> 
+0

請格式化你的代碼 –

回答

1

您可以通過限制語句來完成。你需要設置像一些變量:

  • $ records_per_page - 這是可變的負責顯示的記錄
  • $ CURRENT_PAGE - 變量保存當前頁面

您的查詢需要根據極限X1, Ÿ

X - 多少記錄被跳過 Ÿ - 多少記錄顯示

因此,如果有人把1作爲當前頁面它會像

LIMIT $current_page * $records_per_page, $records_per_page;

此外,你應該算多少條記錄中查詢,而無需限制,並以這種方式,你可以使用循環創建網頁的鏈接。

其他不錯的選擇是使用適當的已經寫好的類。我用過的最好的類是Zend_Paginator的

http://framework.zend.com/manual/1.12/en/zend.paginator.html

如果你不想使用這個類檢查本教程:http://www.phpjabbers.com/php--mysql-select-data-and-split-on-pages-php25.html