JayminsFakeAccount JayminsFakeAccount - 3 months ago 15
PHP Question

Pagination not working-PHP

When I put a term in search-box, my pagination is not working although it gives me a perfect result.
For an eg. If I type

"Laptops"
it gives me records having laptops,but my pagination is not going with the flow. I don't know where I have missed something.
Here is my code:

Search-Box:

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

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


To Display data:

<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>


Pagination Code:

<?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

?>

Answer

You have to use r_query to find no of rows in pagination. Now you are finding the total number of rows in the table

<?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

            ?>