我想知道如何在PHP中进行分页 我可以帮你一个忙吗?
此代码没有错误。 但我不知道$ current_page做什么
<?php
$con = mysqli_connect("localhost", "root", "", "dbdb") or die("error");
$page_size = 10;
$page_list_size = 10;
if (isset($_POST['search'])) {
$valueTosearch = $_POST['valueTosearch'];
$query = "SELECT * FROM `shipment` WHERE CONCAT(`Ship_Date`,`Model_No`,`Serial_No`)LIKE'%" . $valueTosearch . "%' ORDER BY Ship_Date DESC LIMIT $page_size";
$search_result = filterTable($query);
} else {
$query = "SELECT * FROM `shipment` ORDER BY Ship_Date DESC LIMIT $page_size";
$search_result = filterTable($query);
}
function filterTable($query) {
$con = mysqli_connect("localhost", "root", "", "dbdb");
$filter_Result = mysqli_query($con, $query);
return $filter_Result;
}
echo "<center><h1>info</h1></center> <br><br>";
$ret = mysqli_query($con, $query);
if ($ret) {
echo mysqli_num_rows($ret), " <br><br>";
$count = mysqli_num_rows($ret);
} else {
echo "error :" . mysqli_error($con);
exit();
}
$query = "SELECT count(*) FROM shipment";
$result_count = mysqli_query($con, $query);
$result_row = mysqli_fetch_array($result_count);
$total_row = $result_row[0];
if ($total_row <= 0)
$total_row = 0;
$total_page = ceil($total_row / $page_size);
$current_page = ceil();
?>
我想知道如何在PHP中进行分页 我可以帮你一个忙吗?
此代码没有错误。 但我不知道$ current_page做什么
答案 0 :(得分:1)
我认为你在mysql查询上有一个错误,限制为$ page_size你只会获得一些结果= $ page_size,但是你总是会得到$ page-size 1st元素,如果你想查看结果你需要添加偏移量。
这样第一页将是带有偏移量0的$ page_size元素,带有$ page_size偏移量的第二页$ page_size元素,并且通常第n页将是$ page_size元素,带有(n-1)倍$ page_size偏移量。
在最后一行中你缺少一个参数:
$ current_page = ceil();应该是$ current_page = ceil($ lowest_element_in_result_set / $ page_size);
答案 1 :(得分:1)
$ current_page应该是用户想要查看的页面编号,这将是用户的输入。 这是我的实施。 希望这会对你有所帮助。
$pagenum = $_GET['pagenum'];//get the page number user want to view
$con = mysqli_connect("localhost", "root", "9272", "new_platform_7");
$query = 'select * from table1';
$filter_Result = mysqli_query($con, $query);//execute query without limit to get total number of rows
$rows = mysqli_num_rows($filter_Result);//total number of rows
if (!(isset($pagenum)))
{
$pagenum = 1;//if user hasn't give page number set page to page number 1
}
$page_rows = 30;//the page size (number of rows in each page)
$last = ceil($rows/$page_rows);//total number of pages //use ceil to avoid fractions
if ($pagenum < 1){ //if user give negative page number. set page number to 1
$pagenum = 1; //this will happen if user clicked the Previous page link when current page is 1
}elseif ($pagenum > $last) //if page number is greater than calculated number of pages . set page number to last page
{
$pagenum = $last; //this will happen if user clicked the next page link when current page is the last page
}
//($pagenum - 1) * $page_rows calculates where to start the lmit
$query.= ' limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;//limit result
echo $query;
$filter_Result = mysqli_query($con, $query);//execute query with limits
//pagination links
echo '<a href="?pagenum=1&">First Page</a>';
echo '<a href="?pagenum='.($pagenum-1).'&">Previous Page</a>';
echo '<a href="?pagenum='.($pagenum+1).'&">Next Page</a>';
echo '<a href="?pagenum='.$last.'&">Last Page</a>';
答案 2 :(得分:0)
尝试简单的方法。
<?php
$link = mysqli_connect("localhost","roo","","dbname");
if(!$link):
echo "something wrong with the database connection";
endif;
//check to see if the page (value) is available
if(isset($_GET['pageNo']):
$page = $_GET['pageNo'];
else:
$page = "";
endif;
if($page == "" || $page == 1):
$start_to_count_rowNo = 0;
else:
$start_to_count_rowNo = ($page * &item_displayed_per_page) - &item_displayed_per_page;
endif;
//here choose how many items do you want to display per page
&item_displayed_per_page = 10;
//Now select the data to display them
$query = "SELECT * FROM table_name LIMIT $start_to_count_rowNo, $item_displayed_per_page";
$readData = mysqli_query($link, $query);
//obtaining total number of data(rows) in a database
$sql = mysqli_query($link,"SELECT * FROM table_name");
$total_row = mysqli_num_rows($sql);
$pageNo = ceil($total_row / &item_displayed_per_page);
//using bootstrap add class pager in unordered list to style the list
?>
<ul class="pager">
<?php
//display out the page # like 1,2,3 ....
for($i = 0; $i <= $pageNo; $i++){
echo "</li><a href='pagename.php?pageNo={$i}'>{$i}</a></li>";
}
?>
</ul>