下面我已经附上了表格结构,请仔细阅读。
我想获得序列号,因为我的第一列后跟id和name。序列号应该继续分页而不是从1开始。 提前谢谢..
表结构是
create table departments (
id INT(20) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL);
<html>
<head>
<?php
$db_connection = new mysqli("localhost","root","","emp_app");
if($db_connection->connect_error)
die("connection failed".$db_connection->connect_error);
?>
</head>
<body>
<table>
<tr>
<th>Serial no </th>
<th>id</th>
<th>name</th>
</tr>
<?php
$sql_query = "select * from departments";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
?>
</table>
</body>
答案 0 :(得分:0)
您可以附加网址以添加一个get变量,该变量将用作您的分页编号,即youurl.com/?pagination=1或2或3
然后在你的代码中添加
$pagenumber = $_GET["pagination"];
$offset = $pagenumber * 5; //The number 5 is how many results per page if you wanted 10 results per page this would be 10
你在哪里
$sql_query = "select * from departments";
您可以更改为
$sql_query = "select * from departments" LIMIT 5 OFFSET '$offset';
基本上是说;只给我从分页行开始的5个结果(这是5个结果x第2页或第3页
答案 1 :(得分:0)
这是一个很好的例子,如何在php pagination in php中添加分页 引用链接使用已弃用的mysql_query,您需要用mysqli查询替换
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$rs_result = $db_connection->query ($sql); //run the query
$serial=1;
while ($row = $result->fetch_assoc) {
//code here
echo $serial;
$serial++;
};
$sql = "SELECT * FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>
答案 2 :(得分:0)
<html>
<head>
<?php
$db_connection = new mysqli("localhost","root","","emp_app");
if($db_connection->connect_error)
die("connection failed".$db_connection->connect_error);
$num_rec_per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * $num_rec_per_page;
?>
</head>
<body>
<table>
<tr>
<th>Serial no </th>
<th>id</th>
<th>name</th>
</tr>
<?php
$sql_query = "SELECT * FROM departments LIMIT $start_from, $num_rec_per_page";
$result = $db_connection->query($sql_query);
if($result->num_rows > 0){
while($rows = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$rows["id"]."<td>";
echo "<td>".$rows["name"]."<td>";
echo "<tr>";
}
}
$sql = "SELECT * FROM departments"; //select query for total records
$rs_result = $db_connection->query($sql); //run the query
$total_records =$rs_result->num_rows; //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='index.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index.php?page=$total_pages'>".'>|'."</a> ";
?>
</table>
</body>