how does one code the the previos and next buttons, when the users have almost 10check boxes to choose from and 5text fields to choose from? How do you parse your entire query from one page to the other?
kunal
| SitePoint Sponsor |
how does one code the the previos and next buttons, when the users have almost 10check boxes to choose from and 5text fields to choose from? How do you parse your entire query from one page to the other?
kunal
i dunno...
I had the same problem, but I found a few months ago this on this board in this forum:
I'm sorry for the autor, I don't know his name anymore, but he did post this in a previous thread!PHP Code:<?
$limit=10;
if (empty($offset)){ $offset=0; }
$result = mysql_query("SELECT * FROM $table[posts] WHERE threadid='$threadid' ORDER BY post_time");
$numrows = mysql_result($result, 0, 0);
//get results
$result=mysql_query("select id,name,phone ".
"from TABLE where YOUR CONDITIONAL HERE ".
"order by WHATEVER ".
"limit $offset,$limit");
//now you can display the results returned
while ($data=mysql_fetch_array($result)) {
//include code to display results as you see fit
}
//calculate number of pages needing links
$pages=intval($numrows/$limit);
//detemine if we need to add a page for a last page with less than the full number of rows
if ($numrows%$limit) {
//If there was a remainder we add a row
$pages++;
}
//set up variable equal to the record number of the first displayed on current page
$first_record = $offset + 1;
//start displaying the Next/Previous links
echo "Now viewing: $first_record -";
//Check to see if it the last page
if (!((($offset)/$limit)+1==$pages)) {
//not last page so give so last record on page is $offset + $limit
$last_record = $offset + $limit;
echo " $last_record of $numrows items ";
} else {
//is the last page so last record on page is $numrows
echo " $numrows of $numrows item(s) ";
}
//next we need to do the links to other results
if ($offset != 0) { //bypass PREV link if offset is 0
$prevoffset=$offset-$limit;
echo "<a href=\"$PHP_SELF?offset=$prevoffset\">Prev</a> \n";
}
//if there is only 1 page we need no numbered links.
if ( $pages != 1 ) {
for ($i=1;$i<=$pages;$i++) { //loop thru when more than one page
$newoffset=$limit*($i-1);
//echo the number of the currently viewed page without a hyperlink
if ( ((($offset)/$limit)==($i-1)) ) {
echo "$i \n";
} else {
echo "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
}
}
//check to see if last page
if (!((($offset)/$limit)+1==$pages) && $pages!=1) {
//not last page so give NEXT link
$newoffset=$offset+$limit;
echo "<a href=\"$PHP_SELF?offset=$newoffset\">Next</a><p>\n";
}
?>




This piece of code was from freddy I think
PHP Code:if(!isset($offset)) $offset = 0;
$recordsperpage = 10;
$result = mysql_query("SELECT COUNT(*) as totalnum from items");
$row = mysql_fetch_array($result);
$totalrecords = $row["totalnum"];
$result = mysql_query("SELECT * from items order by date DESC LIMIT $offset, $recordsperpage");
while ($row = mysql_fetch_array($result))
{
print $row["title"];
print "\n <br>";
}
if ($totalrecords > $offset)
{
$newoffset = $offset + $recordsperpage;
printf('<a href="%s?offset=%s">more >></a>', $PHP_SELF, $newoffset);
}
if ($offset >= $recordsperpage)
{
$newoffset = $offset - $recordsperpage;
printf('<a href="%s?offset=%s">back <<</a>', $PHP_SELF, $newoffset);
}
hi,.... thanx for the all code..but this does it only for one where condition.. what if there were more??
i dunno...
How do you mean??
You can add more than one WHERE condition...
PHP Code:<?
$query = mysql_query("SELECT * FROM a_table WHERE condition1='1' AND condition2='2' OR condition3='3'");
?>
Well, you can do that for the first page... but how will you parse those variables to the next page???
i dunno...




I know quite see what the problem is, as long as the query give a set of results, the offset will work, say you have multiple WHERE clauses, the query will be:
And everytime the next link is click, it'll query MySQL and fetch the next ten results, perhaps you could further explain the problem?PHP Code:$query = mysql_query("SELECT * FROM a_table WHERE condition1='1' AND condition2='2' OR condition3='3' LIMIT $offset, $recordsperpage");
ok.. let me explain myself a little better....
$test = "Looking for stuff";
$using = "And";
$date = "3days old";
the above is the information, that user submitted using a form...
you run your query and get the first 10results... now, when you want to get the next 10 results.. you ask the user to click on a button, "next"... when he clicks on this button, the original mysql query button gets reset and needs to be re-written, correct? and for this re-writing, we need the orginal information that was entered by the user?
my question, how did you parse the orginal information to the next page? using the variables in the next link, is not an option considering the amout of data they can enter in there query string.
kunal
i dunno...
you'll just have to run the query every page and send the page info in the url for instance searchresults.php?page=3
then you can work out what results to show via LIMIT as above...
the results should always be the same if you use the same query to thedb, assuming you don't add/delete stuff from the db, but having to re-query the db should solve your probs...
beachball





Wihtout reading hte previous posts in this thread..here's how you parse variables thorugh a link.
$test = "Looking for stuff";
$using = "And";
$date = "3days old";
http://www.domain.com/script.php?test=Looking%20for%20stuff&using=And&date=3days%20old
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein
lynlimz, there is a restriction on the amount of text you can parse in the url.
i dunno...
Yeah, I see what you mean
I don't see another solution than use the querystring in your next / previous links.
This should do the trick.PHP Code:<?
$qs = $QUERY_STRING;
echo "<a href=\"?$qs&offset=10\">Next</a>";
?>
Maybe there are other solutions (sessions or cookies), but you shouldn't use that, it's not recommended for a simple search... Even Yahoo takes the searchquery (p=yahoo) in the url:
Code:<a href="http://search.yahoo.com/search?p=yahoo&hc=20&hs=2215&b=21&h=s">Next 20 Site Matches</a>





Yeah. I wasn't expecting you to parse an entire paragrah or something. just the basic like action name...offset number etc.Originally posted by kunal
lynlimz, there is a restriction on the amount of text you can parse in the url.
Another solution would be to build a form submit button, and ultilising the <input type="hidden"...> types...though it'll be ugly. cna't hink of any other way besides these 2 though.
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein
hmm.. im thinking cookies..
but the submision but is a good idea.. one doesnt have to use the button perse right? they can use some text to submit too?
i dunno...





All you need to pass is the variable names and their values, only the ones that are used in the query, it shouldn't even come close to the 255 char limit on GET strings. What is the basis of your query, you certianly aren't using 255 char long strings in yoru query are you?
Cookies, bad idea, when all you want to do is pass some vars to each page. Search this forum there are tons of examples on hwo to achieve this.
Please don't PM me with questions.
Use the forums, that is what they are here for.





just a note...about security.
you shouldn't parse variables for your mysql_query like..
http://domain.com/script.php?where=hello
mysql_query("SELECT * FROM tablename WHERE='$where'");
bad practise. as people can code directly into your mysql_query.
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein
Originally posted by lynlimz
just a note...about security.
you shouldn't parse variables for your mysql_query like..
http://domain.com/script.php?where=hello
mysql_query("SELECT * FROM tablename WHERE='$where'");
bad practise. as people can code directly into your mysql_query.
yup... which is also one of the reasons, why I dont wanna parse the text using the link... hmmm... i think ill just use a session variable..
i dunno...





yes..but then again, it isn't a neccessity to use a session variable. parsing a few simple variables via the query string is all too normal.
what you would do is like have some error checking for the variables..
for example, i have a script which have differnet access controls.
while i gather the access level form the query string /or the input tyoe hidden field, back in the scirpt i also verify the access status. just some security measures.
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
-- Albert Einstein





Just passing variables in the url string alone is no security hazard. In fact passing things like offset and and small things like for instance City=Chicago isn't either. Furthermore like lynlimz said, you should be checking GET variables anyway, if you are not then you are asking for trouble.
Doing something like page=index.php
<?
include($page);
?>
is a security hazard but passing around stupid criteria for a query is not. If someone can maliciously embed some php code in a GET variable then have it screw with your database, then your script is poorly coded from the start. You should not need sessions for this type of feature.
Please don't PM me with questions.
Use the forums, that is what they are here for.
Bookmarks