分页不起作用-PHP

杰明

当我在搜索框中输入一个字词时,尽管它能给我一个完美的结果,但我的分页无法正常工作。对于例如。如果输入,"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

            ?>
约法卜

您必须使用它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

            ?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章