我想只在一页上查询有限的结果 例如,查询将在第1页上显示来自数据库的30个用户,然后在第2页上继续显示另外30个用户等。例如,当您转到对话历史记录时,您需要点击“#34;显示更多"加载更多以前的消息。
修改
<?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个用户,那将是一个大问题。
答案 0 :(得分:0)
您将需要两个页面:第一个页面从数据库获取数据,第二个页面显示分页数据。下一个示例使用数组,将代码复制粘贴到具有给定名称的文件中,打开浏览器,然后键入&#34; localhost / show5_first.php&#34;在地址栏中:
<强> 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>