如何分页从mysql表接收的信息..
我想在每个页面上显示 4行
我有这段代码:
<?php
include_once "config.php";
$sql_select_product = "SELECT * FROM product ORDER BY date DESC;";
$result_select_product = mysqli_query($connection, $sql_select_product);
if (mysqli_num_rows($result_select_product) > 0) {
while ($row_select_product = mysqli_fetch_assoc($result_select_product)) {
echo "
<div class='col-6 col-sm-3' style='margin-bottom: 20px;'>
<img width='250' height='250' src='on_login_pages/uploads/$row_select_product[img]'/>
<div class='des'>
<div class='price\">$row_select_product[price]</div>
<div class='name\">$row_select_product[name]</div>
<div class=''>$row_select_product[weight]</div>
<div class=''>$row_select_product[work_type]</div>
</div>
</div>";
}
}
$connection->close();
?>
答案 0 :(得分:0)
如果您致电servername/path/script_name.php?page=[page_number]
,脚本将只返回查询的4个元素。
<?php
$page_number = fiter_input(INPUT_GET,'page',FILTER_VALIDATE_INT,1,1);
$rows_per_page = 4;
$offset = ($page_number - 1) * $rows_per_page;
include_once "config.php";
$sql_select_product = sprintf("SELECT * FROM product ORDER BY date DESC LIMIT %d OFFSET %d;",
$rows_per_page,$offset);
$result_select_product = mysqli_query($connection, $sql_select_product);
if (mysqli_num_rows($result_select_product) > 0) {
while ($row_select_product = mysqli_fetch_assoc($result_select_product)) {
echo "
<div class='col-6 col-sm-3' style='margin-bottom: 20px;'>
<img width='250' height='250' src='on_login_pages/uploads/$row_select_product[img]'/>
<div class='des'>
<div class='price\">$row_select_product[price]</div>
<div class='name\">$row_select_product[name]</div>
<div class=''>$row_select_product[weight]</div>
<div class=''>$row_select_product[work_type]</div>
</div>
</div>";
}
}
答案 1 :(得分:0)
在查询中使用limit
。像这样:
select * from table_name order by column_name asc limit $offset,$startfrom;