Forum Moderators: coopster

Message Too Old, No Replies

Passing Values In URL

More than or equal to, Less than or equal to

         

oceanwave

6:23 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



Hi,

I have a form that posts to another page. There are two statements on the next page:

$query_count = "SELECT count(*) FROM home WHERE state ='$state' AND town ='$town' AND br >='$br' AND ba >='$ba' AND price <='$price' AND date_expired > curdate()";

$query = "SELECT * FROM home WHERE state ='$state' AND town ='$town' AND br >='$br' AND ba >='$ba' AND price <='$price' AND date_expired > curdate() ORDER BY price DESC LIMIT $limitvalue, $limit";

I need to know how to code the pagination links on this page to include the values for br, ba, and price:
echo("<a href=\"search.php?page=$pageprev&state=$state&town=$town\">PREV</a>&nbsp;&nbsp;&nbsp;");

When my original query_count and query lines only referred to state and town, my pagination line worked fine. Now I don't know how to change this code since I now have "more than or equal to" and "less than or equal to" values (br, ba, price) to pass into the URL. How is this done?

mojomartini

8:33 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



If I'm reading this correctly, I think you are trying to count the records produced by the second query in order to make the pagination come out right. If that's what you are looking for, I think you can do the following after the second query.

$result = mysql_fetch_array($query);
$row_count = count($result);

Then you could pass $row_count to whatever pagination scenario you need.

I'm fairly new at php so if anyone has a better way, I'm be curious to know.

oceanwave

9:06 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



Hi Mojomartini,

Thanks for responding. Yes, the line after the second query is
$result_count = mysql_query($query_count);
The results are echoed into a table limited by the number of results I have set. If there are more than the set amount, the pagination links continue to offer additional page links.

When I only used "state" and "town" in my queries and links (the variables were carried over inside the URL page links), the pagination part worked fine. Now that I am adding br, ba, and price, I only get results on the first page, and the next page says there are no additional matches, even though there are.

The reason "state" and "town" worked is because there was only an equal to variable that needed to be passed. I need to find out how to add the br, ba, and price variables when "more than or equal to" and "less than or equal to" have to be passed inside a URL (for the pagination to work).

mojomartini

9:43 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



I guess I don't see why you need to pass those values over in the URL. Your query only produces a set of rows that match the criteria that is limited by your comparison operators (<,>,=).

If you are using 'action="GET' in the form and then assigning a variable to the $_GET[] array in the php page then you should be passing all that you need. For each table field (column name) that you are passing, you should assign them in php with $variable_name = $_GET[];

Since br,ba and price are all columns returned by the query you should be able to pass them with you form post.

Maybe this will help. On your page with the form, try using hidden input form fields for the columns you wish to pass.

i.e.
<input name="br" type="hidden" value="blah">
<input name="ba" type="hidden" value="blah">

Any 'name' you have defined in those hidden form fields will get passed as part of the url. If those variables have special characters, you might have to use urlencode() to pass it along.

I hope this helps some. I'm still learning myself but I've done what you are trying to do successfully before.

oceanwave

10:17 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



Hi Mojomartini,

Sure doesn't sound like you are a beginner! The form page uses a select drop down to select the minimum number for br, another select drop down for minimum number for ba, and another select drop down for maximum price. These values are being passed to the next page where the queries, table, and links reside. It is the pagination links portion of this page that I am having the difficulty with:

if($page!= 1){
$pageprev = $page-1;
echo("<a href=\"search.php?page=$pageprev&state=$state&town=$town\">PREV</a>&nbsp;&nbsp;&nbsp;");
}else{
echo("PREV &nbsp;&nbsp;&nbsp;");
}
$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"search.php?page=$i&state=$state&town=$town\">$i</a> ");
}
}
if(($totalrows % $limit)!= 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"search.php?page=$i&state=$state&town=$town\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page+1;
echo("&nbsp;&nbsp;&nbsp; <a href=\"search.php?page=$pagenext&state=$state&town=$town\">NEXT</a>");
}else{
echo(" &nbsp;&nbsp;&nbsp;NEXT");
}
mysql_free_result($result);

This worked when I only had state and town in my queries. How do I include br, ba, and price?

mojomartini

11:07 pm on Nov 13, 2007 (gmt 0)

10+ Year Member



Yeah..I'm still new..lol. Only been fooling w/ it a month but I enjoy learning new things.

I'm not sure what the issue is here. The only thing I can offer now is another pagination script I found the other day. It works fine for me.

----------------------
<?php

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}

$query_count = "SELECT count(*) FROM table";
$result_count = mysql_query($query_count) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result_count);
$numrows = $query_data[0];

$rows_per_page = 3; //change this to the number of rows on each page
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno < 1) {
$pageno = 1;
} elseif ($pageno > $lastpage) {
$pageno = $lastpage;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

//Insert your Query Here //
// .
$query = "select blah blah blah ...";
$result=mysql_query($query);

// .

if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo " <a href='index.php?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='index.php?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='index.php?pageno=$nextpage'>NEXT</a> ";
echo " <a href='index.php?pageno=$lastpage'>LAST</a> ";
}
?>

---------------------------
Let me know if that works for you. It seems to provide the same functionality as yours but maybe it will work better.

oceanwave

1:10 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



Thanks Mojomartini! Got it to work.

mojomartini

1:14 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



With my script or yours? If it was yours, I'd be curious as to what the solution was.

oceanwave

5:31 pm on Nov 14, 2007 (gmt 0)

10+ Year Member



Hi Mojomartini,

Actually, I little bit of both scripts. I couldn't figure out why the "more than/less than" values wouldn't pass, even though I thought all you had to do was set &br=$br etc. into the URL. The first mistake I made was that the price variable was not being passed correctly, because I set the database for that field as VARCHAR rather than INT. I thought the price variable was being passed when I checked it using smaller numbers, but then I noticed that if the number was more than 1,000,000 the script fell apart. I was blaming it on the links, but it was actually my database error. I found a post on this forum that referred to the VARCHAR mistake, and sure enough, I had made the same one. Who would have thought to check that? Some of the code in your script was much cleaner than mine, so I combined a little bit of both scripts, fixed my database, added &br=$br&ba=$ba&price=$price to the URL's in my script, and now everything works.

Thanks so much for your help and your script example! You definitely are not a beginner!