我有一个用Mysql编写的脚本,它只列出了人们的名字。它在mysql中工作正常,但我一直试图让它更新到mysqli,我得到了大部分。但是,结果应该是分页的。分页工作正常,因为它应该有5个页面,那就是它停止....但所有结果只显示在每个页面上。不知道我在做什么...任何帮助都将非常感谢....我知道它与" mysql_result"
有关这是我在mysqli中的新代码:
<?php
include ('paginate.php'); //include of paginat page
$per_page = 10; // number of results to show per page
$result = $con->query("SELECT EmployeeID,FirstName,LastName FROM employee;");
//If Database Error...Print the error and exit
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
//If No Database Error.....Continue
if ($result)
{
// Return the number of rows in result set
$total_results=mysqli_num_rows($result);
//printf("Result set has %d rows.\n",$total_results);
// Free result set
}
//total pages we going to have
$total_pages = ceil($total_results / $per_page);
//if page is setcheck
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page'];//it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View All Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<a href="#"><img src="../admin/images/logo-thompson-industrial-services.gif" class="text- center"width="259" height="59" /></a>
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
while($row = mysqli_fetch_assoc($result)) {
$id = $row['EmployeeID'];
$fname = $row['FirstName'];
$lname = $row['LastName'];
echo "<tr>";
echo '<td class="col-md-4">' .$fname .'</td>';
echo '<td class="col-md-4">' .$lname .'</td>';
echo "</tr>";
}
}
// close table>
echo "</table>";
?>
</div>
</div>
</div>
</div>
</body>
</html>
这是我在mysql中的原始代码:
<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
error_reporting(E_ALL ^ E_DEPRECATED);
include('config.php'); //include of db config file
include ('paginate.php'); //include of paginat page
$per_page = 10; // number of results to show per page
$result = mysql_query("SELECT * FROM employee");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//-------------if page is setcheck------------------//
$show_page='';
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //it will telles the current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval(isset($_GET['page']));
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Employees</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<style type="text/css">
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="logo">
<a href="#"><img src="../admin/images/logo-thompson-industrial-services.gif" class="text-center"width="259" height="59" /></a>
</div>
</div>
<br />
<div class="row">
<div class="span6 offset3">
<div class="mini-layout">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'FirstName') . '</td>';
echo '<td>' . mysql_result($result, $i, 'LastName') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
</div>
</div>
</div>
</div>
</body>
</html>
我在这里做错了,我无法弄明白......
非常感谢任何帮助......
非常感谢。
答案 0 :(得分:0)
提取查询的方式是问题所在。输入while statement
后,在完成所有结果然后执行
if ($i == $total_results) {
break;
}
所以它永远不会有机会以超过$ total_result的$ i结束。修复方法是在查询中包含LIMIT
和OFFSET
。这意味着您还必须修复分页页面。
所以只需再次运行查询:
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First
Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// run the query again
$sql ="SELECT EmployeeID,FirstName,LastName FROM employee LIMIT $per_page OFFSET $end;
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
$id = $row['EmployeeID'];
$fname = $row['FirstName'];
$lname = $row['LastName'];
echo "<tr>";
echo '<td class="col-md-4">' .$fname .'</td>';
echo '<td class="col-md-4">' .$lname .'</td>';
echo "</tr>";
}
}
echo "</table>";
答案 1 :(得分:0)
我也会删除while语句,并将mysqli_data_seek
与mysqli_fetch_accoc
结合使用。 MySQLi_data_seek 指向我需要的结果。看看:
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>First
Name</th> <th>Last Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
//go to the column you want the results
mysqli_data_seek($result, $i);
$row = mysqli_fetch_assoc($result);
// echo out the contents of the row into a table
echo "<tr>";
echo '<td>' . $row['FirstName'] . '</td>';
echo '<td>' . $row['LastName'] . '</td>';
echo "</tr>";
}
}
echo "</table>";