2011-03-16 40 views
1

如何選擇前5個結果,然後添加更多選項?如何只選擇前5個結果,然後顯示更多..選項?

下面是當前的代碼:

<?php 

    $query="SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC"; 
    $result=mysql_query($query); 

    $num=mysql_numrows($result); 

    mysql_close(); 

    echo ""; 

    $i=0; 
    while ($i < $num) { 

    $otheris=mysql_result($result,$i,"sender_full_name"); 
    $sysid=mysql_result($result,$i,"sender_id"); 
    $dob=mysql_result($result,$i,"dob"); 

    // If $dob is empty 
    if (empty($dob)) { 

    $dob = "No new messages - 
    <a id=missingdob href=/test.php?id=$uid> 
    <bold>check later</bold></a>"; 
    } 

    echo "<br><div id=linkcontain> 
    <a id=otherlink href=$mem/profile.php?id=$uid> 
    $manitis</a> 
     <br><div id=dobpres>$dob</div></div>"; 

    echo ""; 

    $i++; 
     } 

     ?> 
+1

我假設你知道'LIMIT' ...對嗎?或者......那是你想知道的嗎? – 2011-03-16 22:22:54

+0

不確定關於限制.. – AAA 2011-03-16 22:24:22

回答

1
$query="SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 5"; 

http://dev.mysql.com/doc/refman/5.5/en/select.html

你可以考慮LIMIT 6只顯示多達5個,如果存在6顯示有更多的選擇......

+0

一個跟進。我如何僅在有超過5個結果的地方顯示「查看更多」? – AAA 2011-03-16 22:31:15

+0

你可以考慮限制6只顯示最多5如果第6存在顯示有更多的選項... – bensiu 2011-03-16 22:37:34

2

您應該嘗試首次選擇6行,如果您獲得6條記錄,則首先顯示5個「顯示更多選項」

"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 0, 6"; 

對於隨後的時間,你應該有你的查詢是這樣的:每次

"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 6, 5"; 
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 11, 5"; 
"SELECT * FROM messages where u_id = '$uid' ORDER BY id DESC LIMIT 16, 5"; 
... 
... 

和「顯示更多選項」,如果你能獲取的記錄請求數量。

+1

你缺少第一個記錄 - 限制開始於0 – bensiu 2011-03-16 22:37:09

+1

@ bensiu:感謝指出,我更新了它。 – anubhava 2011-03-16 22:55:00

+0

如何檢測超過5條記錄並顯示選項以查看更多...感謝Anubhava。 – AAA 2011-03-17 15:42:30

相關問題