PHP / SQL - 限制特定页面中的结果数量

时间:2016-05-01 13:48:07

标签: php mysql pagination

我希望将页面上的结果数量限制为20,但在页面底部将是1,2,3,4等。我该怎么做?到目前为止我已经有了这个但是不知道从哪里开始: 代码:

$conn = mysql_connect($dbhost, $dbuser, $dbpass, $database);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db('evocityi_stocks');

$query = "SELECT * FROM FUMUKU"; //
$result = mysql_query($query);

echo "<table>
<tr>
<th>Stock Name</th>
<th>Stock Price</th>
<th>Time</th>
</tr>";


while($row = mysql_fetch_array($result)){   
echo "<tr><td>" . $row['Stock'] . "</td><td>" . $row['Price'] . "<td>" . $row['TimeD'] . "</td></tr>";
}
echo "</table>"; //Close the table in HTML
mysql_close();
?>

1 个答案:

答案 0 :(得分:-2)

<?php 
if (isset(mysql_real_escape_string($_GET["page"]))) 
{
    $page  = mysql_real_escape_string($_GET["page"]); 
}
else  
{ 
$page=1;
 }
$start_from = ($page-1) * 20; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $database);
if(! $conn )
{
 die('Could not connect: ' . mysql_error());
}
mysql_select_db('evocityi_stocks');

$sql = "SELECT * FROM FUMUKU ORDER BY Stock ASC LIMIT $start_from, 20"; 
$result = mysql_query($query);
?> 

echo "<table>
<tr>
<th>Stock Name</th>
<th>Stock Price</th>
<th>Time</th>
</tr>";


while($row = mysql_fetch_array($result)){   
echo "<tr><td>" . $row['Stock'] . "</td><td>" . $row['Price'] . "<td>" . 

$row['TimeD'] . "</td></tr>";
}
echo "</table>"; //Close the table in HTML 
<?php 
$sql = "SELECT COUNT(Stock) FROM FUMUKU"; 
$result = mysql_query($sql); 
$row = mysql_fetch_row($result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 20); 

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='yourpage.php?page=".$i."'>".$i."</a> "; 
}; 
?>