/    Sign up×
Community /Pin to ProfileBookmark

Pagination problem for where cluase

Hello,
I am facing one problem of pagination.
Actually I did pagination successfully,but problem is getting by where clause
I used SELECT query with WHERE clause.Using input field.
Now the problem is that “while choosing the page of that pagination I have to again choose that input fields which used in the where clause.
Here is my piece of code :
Collapse | Copy Code

<?php
$i = 0;

if(!empty($_POST[‘select2’]))
{
foreach ($_POST[‘select2’] as $selectedOption)
{
$options[$i++] = $selectedOption;
}
}

if (isset($_GET[‘pageno’])) {
$pageno = $_
GET[‘pageno’];
} else {
$pageno = 1;
}
$sql =”select DISTINCT date,mobno,city,state,type,telecaller,time FROM import”;
$query = mysql_query($sql);
$query_data = mysql_num_rows($query);

$numrows = $query_data;
$rows_per_page = 10;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
}
if ($pageno < 1) {
$pageno = 1;
}
$limit = ‘LIMIT ‘ .($pageno – 1) * $rows_per_page .’,’ .$rows_per_page;
$sql = “select date,mobno,city,state,type,telecaller FROM import WHERE time IN(“;
$num = count($options);
for ($i=0; $i<$num-1; $i++)
{
$sql .= “‘”.$options[$i].”‘, “;
}
$sql .= “‘”.$options[$i].”‘)”;
$sql .= “GROUP BY mobno,telecaller ORDER BY date DESC $limit”;
// OR date1=’$_POST[date]’
//echo $sql . “<br>”;
$query = mysql_query($sql);
echo”<div id=’pagination’>”;
if ($pageno == 1) {
echo ” FIRST PREV &nbsp &nbsp &nbsp “;
} else {
echo ” <a href='{$_
SERVER[‘PHP_SELF’]}?pageno=1′>FIRST</a> &nbsp &nbsp &nbsp “;
$prevpage = $pageno-1;
echo ” <a href='{$_SERVER[‘PHP_SELF’]}?pageno=$prevpage’>PREV</a> &nbsp &nbsp &nbsp “;
}
echo”</div>”;
echo”<div id=’pagination1′>”;
echo ” ( Page <b>$pageno</b> of $lastpage ) &nbsp &nbsp &nbsp”;
echo”</div>”;
echo”<div id=’pagination2′>”;
if ($pageno == $lastpage) {
echo ” NEXT LAST &nbsp &nbsp &nbsp”;
} else {
$nextpage = $pageno+1;
echo ” <a href='{$_
SERVER[‘PHP_SELF’]}?pageno=$nextpage’>NEXT</a> &nbsp &nbsp &nbsp”;
echo ” <a href='{$_SERVER[‘PHP_SELF’]}?pageno=$lastpage’>LAST</a> “;
}
echo”</div>”;

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@mjdamatoSep 21.2012 — I'm seeing some things that just don't look right in that code. For example the query to get the total records is not using the WHERE parameter to get the correct record total based upon the filter criteria. But the solution to your problem is simple. If the user passes data to be used to filter the records then you need to same those parameters and use them on subsequent page loads. Here is some sample code you can try:

[code=php]<?php

session_start();

$records_per_page = 10;

function parseOptions($opt)
{
$opt = mysql_real_escape_string($opt);
return "'{$opt}'";
}

if(!empty($_POST['select2']))
{
$options = array_map('parseOptions', $_POST['select2']);
$_SESSION['options'] = $options;
}
elseif(isset($_SESSION['options']))
{
$options = $_SESSION['options'];
}

//Create WHERE clause
$WHERE = (isset($options)) ? "WHERE time IN (" . implode(', ', $options) . ")" : '';

//Get total record count
$query ="SELECT COUNT(*)
FROM import
{$WHERE}";
$result = mysql_query($query);
$total_records = mysql_result($result, 0);

//Calculate total pages
$total_pages = ceil($total_records / $records_per_page);

//Set page number
$pageno = (isset($_GET['pageno'])) ? intval($_GET['pageno']) : 1;
$pageno = min(max($pageno, 1), $total_pages);

//Run query to get records for selected page
$LIMITSTART = ($pageno - 1) * $records_per_page;
$query = "SELECT date, mobno, city, state, type, telecaller
FROM import
{$WHERE}
GROUP BY mobno,telecaller
ORDER BY date DESC
LIMIT $LIMITSTART, $records_per_page";
$result = mysql_query($query);

//Create page navigation
echo"<div id='pagination'>";
if ($pageno == 1)
{
echo " FIRST PREV &nbsp &nbsp &nbsp ";
}
else
{
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> &nbsp &nbsp &nbsp ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> &nbsp &nbsp &nbsp ";
}
echo"</div>";

echo"<div id='pagination1'>";
echo " ( Page <b>$pageno</b> of $total_pages ) &nbsp &nbsp &nbsp";
echo"</div>";

echo"<div id='pagination2'>";
if ($pageno == $total_pages)
{
echo " NEXT LAST &nbsp &nbsp &nbsp";
}
else
{
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> &nbsp &nbsp &nbsp";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$total_pages'>LAST</a> ";
}
echo"</div>"; [/code]
Copy linkTweet thisAlerts:
@neha_jaltareauthorSep 24.2012 — huh,it works thanks.. but can you please explain me the above coding??
×

Success!

Help @neha_jaltare spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...