【问题标题】:How to query for example only 30 users from row and then continue the query on the next page?例如,如何从行中仅查询 30 个用户,然后在下一页继续查询?
【发布时间】:2015-03-26 20:05:38
【问题描述】:

我只想在一页上查询有限的结果。
例如,查询将在第 1 页显示来自数据库的 30 个用户,然后在第 2 页继续显示另外 30 个用户,等等。例如,当您转到对话历史记录时,您必须单击“显示更多”才能加载更多以前的消息.

编辑:

<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM members ORDER BY username";

mysql_select_db('');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    if($row['picture'] == ""){
    echo "<img src='profile_pictures/public_icon.png' width='46' height='50'><br>";}else {
                                        echo " <img width='50' height='50' src='profile_pictures/".$row['picture']."' alt='Profile Pic'><br>";
                                }
    echo "<b>Email:</b> {$row['email']} <br> ";

      echo "<b>Username: </b>{$row['username']}  </p> ";
    echo "<hr class='hr' />";

} 
?>

这显示了所有用户的信息(电子邮件、个人资料图片和用户名),以 hr 分隔。
问题是它在一页上显示 ALL。如果注册例如1000个用户,那将是一个大问题。

【问题讨论】:

  • 向我们展示你到目前为止所做的事情。
  • 查看限制和偏移量。 select * from users limit 30 offset 0;给你页面 1. select * from users limit 30 offset 30;给你第 2 页。以此类推。
  • 这里有一个链接bit.ly/YHa2nQ
  • @dudeman - 看来我必须手动添加每 30 个...
  • 有很多关于分页主题的好教程。我不知道这个有多好,但可能是一个不错的起点:tutorialspoint.com/php/mysql_paging_php.htm

标签: php mysqli rows


【解决方案1】:

您将需要两个页面:第一个从数据库中获取数据,第二个显示分页的数据。下一个示例使用数组,将代码复制粘贴到具有给定名称的文件中,打开浏览器,然后在地址栏中输入“localhost/show5_first.php”:

show5_first.php

<?php
session_start();
$my_array = array( "aa","bb","cc","dd","ee","ff","gg","hh","ii","jj",
                   "kk","ll","mm","nn","oo","pp","qq","rr","ss","tt",
                   "uu","vv","ww","xx","yy","zz" );
$_SESSION["my_data"] = implode( "|",$my_array );
$_SESSION["index"] = 0; // DATA WILL BE SHOWN STARTING WITH ITEM ZERO.
?>
<html>
  <head>
    <title>Jose Manuel Abarca Rodriguez</title>
  </head>
  <body>
    <form method="post" action="show5_rest.php">
      Enter how many items do you want to see per page:
      <input type="text" name="how_many" value="5"/>
      <br/>
      <input type="submit" value="Show items paged"/>
    </form>
  </body>
</html>

show5_rest.php

<?php
session_start();
if ( IsSet( $_POST["how_many"] ) )
   $_SESSION["how_many"] = (int)$_POST["how_many"];
?>
<html>
  <head>
    <title>Jose Manuel Abarca Rodriguez</title>
  </head>
  <body>
    Paging elements from <?php echo $_SESSION["index"]; ?> to
    <?php echo $_SESSION["index"] + ($_SESSION["how_many"]-1); ?>
    <br/>
    <br/>
<?php
// SHOW ITEMS OF THIS PAGE.
$my_array = explode( "|",$_SESSION["my_data"] );
for ( $i = $_SESSION["index"]; $i < ( $_SESSION["index"] + $_SESSION["how_many"] ); $i++ )
  if ( $i < count( $my_array ) )
    echo $my_array[$i] . "<br/>";
?>
    <br/>
<?php
// INDEX POR THE NEXT PAGE.
$_SESSION["index"] = $_SESSION["index"] + $_SESSION["how_many"];
if ( $_SESSION["index"] < count( $my_array ) )
   echo "<form action='show5_rest.php'>" .
        "<input type='submit' value='Next page'/>" .
        "</form>";
?>
  </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-02-26
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多