【问题标题】:How to select only first 5 results and then show more.. option?如何只选择前 5 个结果,然后显示更多.. 选项?
【发布时间】:2011-03-16 22:20:55
【问题描述】:

如何选择前 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++;
      }

       ?>

【问题讨论】:

  • 我想你知道LIMIT...对吧?或者……这是你想知道的吗?

标签: php mysql search


【解决方案1】:

您应该尝试第一次选择 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";
...
...

如果您能够获取请求的记录数,则每次“显示更多选项”。

【讨论】:

  • 您缺少第一条记录 - LIMIT 从 0 开始
  • 我怎样才能检测到超过 5 条记录并显示查看更多的选项...谢谢 Anubhava。
  • 好吧,如果您注意到在每个 SQL 中我们获取的记录多于我们必须显示的记录(例如:在第二次选择中,我们最多获取第 11 条记录,但您最多只能显示第 10 条记录)。如果您的最新选择确实从数据库返回了 5 条记录,则在浏览器中始终显示“显示更多”链接,一旦用户点击该链接获取下 5 条记录。当您到达表中最后少于 5 条记录时,您的
  • @AAA:如果你想看这个分页机制的工作php代码,请看我对这个问题的回答:stackoverflow.com/questions/5354244/…
【解决方案2】:
$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

如果存在第 6 个显示还有更多选项,您可以考虑 LIMIT 6 个显示最多 5 个...

【讨论】:

  • 一个跟进。如何仅在超过 5 个结果的情况下显示“查看更多”??
  • 你可以考虑 LIMIT 6 display only up to 5 if 6th exists display 还有更多选项...
【解决方案3】:
window.onload = function(){

$(".box").hide();
$(".box").slice(0, 5).show(); // select the first ten
$("#load").click(function(e){ // click event for load more
    e.preventDefault();
    $("div:hidden").slice(0, -1).show(); // select next hidden divs and show them
    $("#load-more-div").html(' ');
});}

【讨论】:

  • 能否详细说明以帮助我们理解这段代码?
  • @SimasJoneliunas 是的。假设您有 100 个带有“box”类的 div,并且您首先只想显示 5 个,然后在单击“加载更多按钮”(将具有 id =load)后,您想要显示其余的部门。代码:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多