这是我" products.php"的输出。页
我已经尝试了几个分页代码并对其进行了调整,但我无法做到这一点...... 我真的需要一些帮助,一个简单的教程或一个工作代码分页。 例如,每页中有4个产品。
我知道它不是一些代码,但我不知道还有什么地方可以寻求帮助......
<?php
// to prevent undefined index notice
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
if($action=='add'){
echo "<div>" . $name . " was added to your cart.</div>";
}
if($action=='exists'){
echo "<div>" . $name . " already exists in your cart.</div>";
}
require "libs/DbConnect.php";
$query = "SELECT id, name, price FROM products";
$stmt = $con->prepare( $query );
$stmt->execute();
$num = $stmt->rowCount();
if($num>0){
echo "<table border='0'>";//start table
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price (USD)</th>";
echo "<th>Action</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td class='textAlignRight'>{$price}</td>";
echo "<td class='textAlignCenter'>";
echo "<a href='addToCart.php?id={$id}&name={$name}' class='customButton'>";
echo "<img src='images/add-to-cart.png' class='imagem2'title='Add To Cart' />";
echo "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
// no products in the database
else{
echo "No products found.";
}
?>
答案 0 :(得分:0)
我会在您的网址中添加一个页面偏移量作为查询字符串。从那里,您可以从开始时$ _GET当前页面偏移量,并在您的SQL查询中创建一个偏移量,以绕过应该在您已经传递的页面上显示的产品。此外,将结果限制为您希望每页显示的帖子数。
例如,如果您在第2页,并且每页发布4个产品,则您的SQL查询应限制为4个返回的结果,并跳过表中的前4个结果。
答案 1 :(得分:0)
如果您感兴趣,我可以使用 MySQLi 来分页。随意试试这个:
假设您的 DbConnect.php 是这样的:
<?php
/* ESTABLISH YOUR CONNECTION */
$con=mysqli_connect("YourHost","Username","Password","Database"); /* REPLACE NECESSARY DATA */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
您的分页页面:
<?php
/* I DID NOT ALTER THIS PART OF YOUR ACTION, JUST PASTED IT HERE... */
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
if($action=='add'){
echo "<div>" . $name . " was added to your cart.</div>";
}
if($action=='exists'){
echo "<div>" . $name . " already exists in your cart.</div>";
}
/* ...UNTIL HERE */
include('libs/DbConnect.php'); /* INCLUDE YOUR CONNECTION */
$result=mysqli_query($con,"SELECT id,name,price FROM products ORDER BY id"); /* EXECUTE YOUR QUERY */
$count=mysqli_num_rows($result);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 4; /* SET 4 ROWS PER PAGE */
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$result=mysqli_query($con,"SELECT id,name,price FROM products ORDER BY id LIMIT $offset,$rowsperpage"); /* EXECUTE QUERY WITH LIMIT */
echo "<table border='0'><tr><th class='textAlignLeft'>Product Name</th><th>Price</th><th>Action</th></tr>";
while($row=mysqli_fetch_array($result)){ /* FETCH ARRAY BASED FROM THE QUERY */
$name=mysqli_real_escape_string($con,$row['name']); /* ESCAPE STRINGS AND PUT THEM IN A VARIABLE */
$price=mysqli_real_escape_string($con,$row['price']); /* ESCAPE STRINGS AND PUT THEM IN A VARIABLE */
echo "<tr><td>".$name."</td>
<td class='textAlignRight'>".$price."</td>
<td class='textAlignCenter'>
<a href='addToCart.php?id={$id}&name={$name}' class='customButton'>
<img src='images/add-to-cart.png' class='imagem2' title='Add To Cart' />
</a>
</td></tr>";
} /* END OF WHILE LOOP */
if($count==0){ /* IF NO PRODUCTS FOUND */
echo "<tr><td></td><td>No products found.</td><td></td></tr>";
}
else { /* START OF PAGINATION LINK */
echo '<tr height="30px;" valign="bottom"><td></td><td>';
echo "<table style='border-collapse:separate; border-spacing:3px;'><tr>";
/****** build the pagination links ******/
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo "<td style='width:70px; background-color:fff; border:solid #08c 1px; font-size:14px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage' style='background-color:fff;'>Previous</a> </td>";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #ccc 2px;' align='center'> <font color='#ccc'><b>$x</b></font> </td>";
} else {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x' style='background-color:fff;'>$x</a> </td>";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo "<td style='width:70px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage' style='background-color:fff;'>Next</a> </td>";
} // end if
/****** end build pagination links ******/
echo "</tr></table></td></tr>";
} /* END OF ELSE IF COUNT 0 */
echo '</table>';
?>