页面上一页/下一页PHP MYSQL

时间:2013-07-05 22:31:39

标签: php mysql

我正在使用php / mysql编写一个简单的脚本来记录,我保存了记录,并且它运行良好。

我的问题是我只需要显示4条记录,并有一个寻呼机转到下一页/上一页。

有人可以给我一个代码吗?

这是我的代码:

$result = mysqli_query($con,"SELECT * FROM products Where username='$_SESSION[username]' LIMIT 0, 4");


while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td width='25%'>". $row['serial'] . "</td>";
  echo "<td><img width='65' height='65' src='upload/".$row['image']."'></td>";
  echo "<td width='25%'>". $row['arrive'] . "</td>";
  echo "<td width='25%'>". $row['size']; "</td>";  
  echo "</tr>";
}
echo "</table>";

3 个答案:

答案 0 :(得分:1)

你必须计算起点,然后在你的SQL查询中使用它 看看这段代码

// pagination
$total_results = mysqli_num_rows(mysqli_query($con,"SELECT `username` FROM products Where username='$_SESSION[username]' "));

if(!isset($page_number))
  $page_number = (int)$_GET['page'] <= 0 ? 1 : (int)$_GET['page']; // grab the page number

$perpage = 4; // number of elements perpage


if($page_number > ceil($total_results/$perpage))
  $page_number = ceil($total_results/$perpage);


$start = ($page_number - 1) * $perpage;

$result = mysqli_query($con,"SELECT * FROM products Where username='$_SESSION[username]' LIMIT $start, $perpage");

另外,您可以看到总页数为ceil($total_results/$perpage),因此您可以使用$page_number来决定何时显示下一个/上一个

答案 1 :(得分:0)

将Limit子句中的0设置为变量并增加4

//$_POST['limit']=0,4,8,12....
$start = $_POST['limit'];
$result = mysqli_query($con,"SELECT * FROM products Where username='$_SESSION[username]' LIMIT $start, 4");

答案 2 :(得分:0)

<?php
if (!isset($_GET['page'])))
{
    $page = 1;
}
else
{
    $page = (int)$_GET['page'];
}

$result = mysqli_query($con,"SELECT * FROM products Where username='$_SESSION[username]' LIMIT " . ($page - 1) * 4 . ", 4");

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td width='25%'>". $row['serial'] . "</td>";
    echo "<td><img width='65' height='65' src='upload/".$row['image']."'></td>";
    echo "<td width='25%'>". $row['arrive'] . "</td>";
    echo "<td width='25%'>". $row['size']; "</td>";  
    echo "</tr>";
}
echo "</table>";

echo '<br/>';
echo '<a href="http://website.com/yourPage.php?page=2">Next page</a>';
?>