How to query for example only 30 users from row and then continue the query on the next page?

房东的猫 提交于 2019-12-14 03:28:53

问题


I want to query limited result only on one page.
For example, the query will show 30 users from database on page 1, and then continue with another 30 on page 2 etc. For example Facebook messages when you go to conversation history and you have to click "show more" to load more previous messages.

EDIT:

<?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' />";

} 
?>

This shows ALL user's information (email, profile picture and username) separated with hr.
The problem is that it displays ALL on one page. It would be big problem if there would be registered for example 1000 users.


回答1:


You will need two pages : the first one gets the data from database, the second shows the data paged. Next example works with an array, copy-paste the code in files with the given names, open your browser, and type "localhost/show5_first.php" in the address bar :

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>


来源:https://stackoverflow.com/questions/29287818/how-to-query-for-example-only-30-users-from-row-and-then-continue-the-query-on-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!