Sunday, 18 August 2013

filtering data gets messy on php

filtering data gets messy on php

I have a database which stores payment orders. I have paged the sql
records (10 per page). I have created a filter *(timi) which is supposed
to filter the data. For specific values (of filter timi)records are 10+ .
And here is the bug. For 10+ records i get the first page and its all
fine. But when i click to the second page (or Next link) I get the 2nd
page of all the data and not from the filtered query.
<?php
include 'core/init.php';
$rec_limit = 10;
?>
<article>
ÖÉËÔÑÁ:
<form method= "POST" action = "tameio.php">
ÔÉÌÇ :
<input type="text" value="" name="timi">
<input type= "submit" name= "filters" value = " ÁÍÁÆÇÔÇÓÇ " >
</form>
<?php
if(isset($_POST['filters']))
{
$timi = $_POST['timi'];
}
?>
<?php
$query = "SELECT COUNT(*) FROM id_of_orders WHERE 1=1";
if(!empty($timi))
$query .= " AND id_of_orders.price='$timi'";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_NUM);
$counter = $row[0];
$offset = $_GET['offset'];
if (empty($offset)) {
$offset=0;
}
$query = "SELECT * FROM id_of_orders WHERE 1=1";
if(!empty($timi))
$query .= " AND price='$timi'";
$query .= " LIMIT $offset, $rec_limit" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo $row['id_order'] . ": -> " .$row['time'] . " " .
$row['price'] . '<br>';
}
if ($offset>0) { // bypass PREV link if offset is 0
$prevoffset=$offset - 10;
echo "<a href=\"tameio.php?offset=$prevoffset\">PREV</a> &nbsp; \n";
}
// calculate number of pages needing links
$pages=intval($counter/$rec_limit);
// $pages now contains int of pages needed unless there is a remainder
from division
if ($counter%$rec_limit) {
// has remainder so add one page
$pages++;
}
for ($i=1;$i<=$pages;$i++) { // loop thru
$newoffset=$rec_limit*($i-1);
echo "<a href=\"tameio.php?offset=$newoffset\">$i</a> &nbsp; \n";
}
if (!((($offset+$rec_limit)/$rec_limit)==$pages) && $pages!=1){
// not last page so give NEXT link
$newoffset=$offset+$rec_limit;
echo "<a href=\"tameio.php?offset=$newoffset\">NEXT</a><p>\n";
}
?>

No comments:

Post a Comment