Php 我的页面中的简单分页

Php 我的页面中的简单分页,php,html,mysql,pagination,Php,Html,Mysql,Pagination,这是我的“products.php”页面的输出 我已经尝试了几个分页代码,并对其进行了修改,但我没有做到这一点。。。 我真的需要一些简单的教程或分页的工作代码的帮助。 例如,每页有4种产品 我知道这不是一点代码,但我不知道在哪里可以寻求帮助 <?php // to prevent undefined index notice $action = isset($_GET['action']) ? $_GET['action'] : ""; $name = isset($_GET['name

这是我的“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.";
}

?>

我会在url中添加页面偏移量作为查询字符串。从那里,您可以$\u从一开始就获得当前页面偏移量,并在sql查询中创建一个偏移量,以绕过应该出现在已传递页面上的产品。此外,将结果限制为每页希望看到的帖子数


例如,如果您在第2页,并且每页发布4个产品,则sql查询应限制为返回4个结果,并跳过表中的前4个结果。

如果您感兴趣,我可以使用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>';

?>

是啊,我的php知识太差了。我主要编辑代码,或者只编写一点程序。你不能提供一个简单的págination working.code吗?我有点不赞成使用mysqli或mysql???@user3689530有什么区别吗?区别在于mysql已经被弃用了。