分页不起作用 - PHP

时间:2016-08-30 12:26:34

标签: php pagination

当我在搜索框中放置一个术语时,我的分页不起作用,虽然它给了我一个完美的结果。 例如。如果我输入"Laptops"它会给我记录笔记本电脑,但我的分页不符合流程。我不知道我错过了什么。 这是我的代码:

搜索框:

<form action="" method="GET">  

            Search: <input type="text" name="term" value="<?php echo @$_REQUEST['term']; ?>" /><br />  
            <input type="submit" value="Submit" /> 
        </form>  

显示数据:

<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
            <?php
            include_once '../storescripts/connect_to_mysql.php';
            $num_rec_per_page = 5;
            if (isset($_GET["page"])) {
                $page = $_GET["page"];


            } else {
                $page = 1;
            };
            $start_from = ($page - 1) * $num_rec_per_page;

            $sql = "SELECT * FROM products WHERE status='Active' LIMIT $start_from, $num_rec_per_page";
            ?>
            <thead>
                <tr class="success">
                    <th>Id</th>
                    <th>Product Image</th>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Status</th>
                    <th>Quantity</th>
                    <th>Details</th>
                    <!--<th>Category</th>
                    <th>Subcategory</th>-->
                    <th>Date Added</th>
                    <th colspan="2">Functions</th>
                </tr>
            </thead>


<?php
if (!empty($_REQUEST['term']))
 {

    $term = mysql_real_escape_string($_REQUEST['term']);

    $sql = "SELECT * FROM products WHERE product_name  LIKE '%" . $term . "%' or status LIKE '%" . $term . "' or quantity LIKE '%" . $term . "' or details LIKE '%" . $term . "' or price LIKE '%" . $term . "'  or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) 
{

    while ($row = mysql_fetch_array($r_query)) 
    {
        echo "<tr bgcolor=''>";
        echo "<td width='5%'>" . $row['id'] . "</td>";?>
        <td width="10%"><img  style='border:#666 1px solid;' width='70' src="<?php echo $row["productimage"]; ?>" alt="" /></td>
        <?php echo "<td width='20%'>" . $row['product_name'] . "</td>";
        echo "<td width='5%'>" . $row['price'] . "</td>";
        echo "<td width='5%'>" . $row['status'] . "</td>";
        echo "<td width='5%'>" . $row['quantity'] . "</td>";
        echo "<td width='19%'>" . $row['details'] . "</td>";
       // echo "<td>" . $row['category'] . "</td>";
       // echo "<td>" . $row['subcategory'] . "</td>";
        echo "<td width='10%'>" . $row['date_added'] . "</td>";
        echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";?>
        <td><a href="#" class="delete" onclick="dialogbox(<?php echo $row['id']; ?>)" >Delete</a>
        <?php 
        //echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "' onclick='return show_confirm();' >Delete</a></td><tr>";



        echo "</tr>";
    }
} 
else {
    echo "Nothing should be displayed";
}
?>
        </table>

分页代码:

<?php 
            //Pagination code starts here
            $sql = "SELECT * FROM products"; 
            $rs_result = mysql_query($sql); //run the query
            $total_records = mysql_num_rows($rs_result);  //count number of records
            $total_pages = ceil($total_records / $num_rec_per_page); 

            echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page  

            for ($i=1; $i<=$total_pages; $i++) { 
                        echo "<a href='product_listing.php?page=".$i."'>".$i."</a> "; 
            }; 
            echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page

            ?>

1 个答案:

答案 0 :(得分:1)

您必须使用r_query才能在分页中找不到行。现在您要查找表中的总行数

<?php 
            //Pagination code starts here

            $total_records = mysql_num_rows($r_query);  //count number of records
            $total_pages = ceil($total_records / $num_rec_per_page); 

            echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page  

            for ($i=1; $i<=$total_pages; $i++) { 
                        echo "<a href='product_listing.php?page=".$i."'>".$i."</a> "; 
            }; 
            echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page

            ?>