Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2011, 05:51 AM   PM User | #1
ohmitmedia
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
ohmitmedia is an unknown quantity at this point
Question How to let user re-sort POST search results passed from previous page

I'm creating a search engine that allows users to search various variables in a mysql database and sort them. I would like the user to be able to RE-SORT the search results they see by any one of the variables, but I'm having trouble getting their search terms passed onto the "next", re-sorted page.

PAGE 1: POST search form, sends results to "page2.php?field=sortfield&order=asc"

PAGE 2: Displays search results, sorted by "sortfield" in "asc" or "desc". Page 2 contains links with href="?field=differentsortfield" but upon clicking the link, I receive a mysql error that there are no valid variables passed.

Here is my sql query.....
PHP Code:
<?     $sql "SELECT * FROM rentlist WHERE active = 1  AND type LIKE '%"$searchtype ."%' AND location LIKE '%"$searchlocation ."%' AND bed LIKE '%"$searchbedrooms ."%' AND pets LIKE '%"$searchpetok ."%' AND price BETWEEN $searchpricelow AND $searchpricehigh ORDER BY $field $order"$result mysql_query($sql);     ?>
Here are the sort links I'm trying to use....
Code:
<div id="sort"> <a href="#" onClick="window.print()"><img class="left" src="images/print-icon.gif" width="22" height="22" alt="Print This Page"></a>SORT BY:&nbsp; &nbsp;<a href="?field=type">Type</a> | <a href="?field=location">Location</a> | <a href="?field=bed">#Bedrooms</a> | <a href="?field=price">Price</a> &nbsp; &nbsp; &nbsp;<a href="?field=<?=$field?>&order=asc">Asc</a> | <a href="?field=<?=$field?>&order=desc">Desc</a></div><!--close sort-->
I have tried changing my search form to GET, but then I get no valid results at all.

I'm still pretty new with PHP. Any help would be greatly appreciated.
ohmitmedia is offline   Reply With Quote
Old 10-10-2011, 12:23 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Can you display the whole script?
I think we'll be able to see it better.
mlseim is online now   Reply With Quote
Old 10-10-2011, 04:11 PM   PM User | #3
ohmitmedia
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
ohmitmedia is an unknown quantity at this point
certainly.

Code:
<? 	$sql = "SELECT * FROM rentlist WHERE active = 1  AND type LIKE '%". $searchtype ."%' AND location LIKE '%". $searchlocation ."%' AND bed LIKE '%". $searchbedrooms ."%' AND pets LIKE '%". $searchpetok ."%' AND price BETWEEN $searchpricelow AND $searchpricehigh ORDER BY $field $order"; $result = mysql_query($sql); 	?>
<?    function shorten_string($string, $wordsreturned)
    {
    $retval = $string;    //    Just in case of a problem
     
    $array = explode(" ", $string);
	
    if (count($array)<=$wordsreturned)
    /*  Already short enough, return the whole thing
    */
    {
    return $retval;
    }
    else
    /*  Need to chop of some words
    */
    {
    array_splice($array, $wordsreturned);
    $retval = implode(" ", $array)." ...";
    }
    return $retval;
    }
?>


  <div id="search">
   		<form id="searchterms" name="form" method="post" action="RentalSearch.php?field=location&order=desc">
                <select class="left" name="searchtype" id="searchtype">
                  <option selected value="">Any Type</option>
                  <option value="House">House</option>
                  <option value="Apartment">Apartment</option>
                  <option value="Commercial">Commercial</option>
                  </select>
                <select class="left" name="searchlocation" id="searchlocation">
                  <option selected value="">Any Location</option>
                  <option value="Tehachapi">Tehachapi</option>
                  <option value="Golden Hills">Golden Hills</option>
                  <option value="Bear Valley">Bear Valley</option>
                  <option value="Keene">Keene</option>
                  <option value="Hart Flat">Hart Flat</option>
                  <option value="California City">California City</option>
                  </select> 
                <select class="left" name="searchbedrooms" id="searchbedrooms" >
                  <option selected value="">Any # Bedrooms</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4+</option>
                  </select> 
                 <select class="left" name="searchpetok" id="searchpetok" >
                  <option selected value="">Pets N/A</option>
                  <option value="1">Pets OK</option>
                  <option value="0">NO Pets</option>
                  </select>
               
                 &nbsp; &nbsp; Price:<input name="searchpricelow" type="text" id="searchpricelow" size="8" maxlength="8" value="0"> to 
                <input name="searchpricehigh" type="text" id="searchpricehigh" size="8" maxlength="8" value="10000">
                
                &nbsp; &nbsp;
                <input type="submit" name="Submit" value="Search">
  </form>
   </div><!--end search-->
   
   <div id="disclaimer">
   <p class="fine">ONLY DRIVE BY:  DO NOT APPROACH. DO NOT KNOCK OR PEEK IN WINDOWS.    IF THERE IS SOMETHING YOU WOULD LIKE TO SEE, CALL OR STOP BY OUR OFFICE TO MAKE AN APPOINTMENT.  APPOINTMENTS SHOULD BE MADE ONE TO TWO DAYS IN ADVANCE IF POSSIBLE.  SATURDAY APPOINTMENTS  ARE AVAILABLE.</p>
</div><!--close disclaimer-->
   

   
	<div id="sort"> <a href="#" onClick="window.print()"><img class="left" src="images/print-icon.gif" width="22" height="22" alt="Print This Page"></a>SORT BY:&nbsp; &nbsp;<a href="?field=location">Location</a> | <a href="?field=bed">#Bedrooms</a> | <a href="?field=price">Price</a> &nbsp; &nbsp; &nbsp;<a href="?field=<?=$field?>&order=asc">Asc</a> | <a href="?field=<?=$field?>&order=desc">Desc</a></div><!--close sort-->

  <? $i=1; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>	
   
   <!--Lightbox Popup-->
     <div id="<? echo $i ?>" class="white_content">
	 <?	 if ($row['photo1'] != "") { echo "<img id='boximg' src='rentalphotos/".$row['address']."/".$row['photo1']."' alt='Rental Photo' border='0' align='absmiddle'></a>";} else { echo "<img id='boximg' src='images/camerashy.png' alt='Rental Photo'> ";} ?>
      <?	 if ($row['photo2'] != "") { echo "<img id='boximg2' src='rentalphotos/".$row['address']."/".$row['photo2']."' alt='' border='0' align='absmiddle'>";} else { echo "";} ?>
      <?	 if ($row['photo3'] != "") { echo "<img id='boximg3' src='rentalphotos/".$row['address']."/".$row['photo3']."' alt='' border='0' align='absmiddle'><p></p>";} else { echo "";} ?>
      <?	 if ($row['photo4'] != "") { echo "<img id='boximg4' src='rentalphotos/".$row['address']."/".$row['photo4']."' alt='' border='0' align='absmiddle'><p></p>";} else { echo "";} ?>
         <div id="boxprice">$<? echo $row['price']; ?>/mo	
     </div><!--close price-->
    <div id="boxaddress"><? echo $row['address']; ?>,<p>Tehachapi</p>
    </div><!--close address-->
    <div id="boxtype"><? echo $row['type']; ?></div><!--close type-->
    <div id="boxbedbath">
	<? if ($row['bed'] != "0") {  echo "".$row['bed']." Bed,";} ?> 
	<? if ($row['bath'] != "0") { echo "".$row['bath']." Bath";} ?></div><!--close bedbath-->
    <div id="boxpets"><? if ($row['type'] == "COMMERCIAL") {echo ""; } else if ($row['pets'] == "1") { echo "PETS OK"; } else { echo "NO PETS"; } ?></div>
    <div id="boxphone">(661) 822-RENT</div>
    <div id="boxdownloadapp"><a href="/pdfs/RentalApplication.pdf">Download Application</a></div><!--close downloadapp-->
    <div id="boxdescription"><? echo $row['description'] ?>
    </div><!--close description-->
    <div id="boxhide"><a href="#" onclick="printDiv('<? echo $i ?>')"><img class="left" src="images/print-icon.gif" width="18" height="18" alt="Print This Page"></a>&nbsp; &nbsp; <a href = "javascript:void(0)" onclick = "document.getElementById('<? echo $i ?>').style.display='none';document.getElementById('fade').style.display='none'">Hide</a>
    </div><!--Close Boxhide--> 	
   </div><div id="fade" class="black_overlay"></div>
   <!--Close Lightbox Popup-->
   
	<div id="listing">
    	<a href = "javascript:void(0)" onclick = "document.getElementById('<? echo $i ?>').style.display='block';document.getElementById('fade').style.display='block'"><? if ($row['photo1'] != "") { echo "<img src='rentalphotos/".$row['address']."/".$row['photo1']."' alt='Rental Photo' border='0' align='absmiddle'></a>";} else { echo "<img src='images/camerashy.png' alt='Rental Photo'> ";} ?></a>
        
      <div id="price">$<? echo $row['price']; ?>/mo</div><!--close price-->
        <div id="address"><a href = "javascript:void(0)" onclick = "document.getElementById('<? echo $i ?>').style.display='block';document.getElementById('fade').style.display='block'"><? echo $row['address']; ?></a>,<p><? echo $row['location']; ?></p></div><!--close address-->
      <div id="type"><? echo $row['type']; ?></div><!--close type-->
        <div id="bedbath"><? if ($row['bed'] != "0") {  echo "".$row['bed']." Bed,";} ?> 
	<? if ($row['bath'] != "0") { echo "".$row['bath']." Bath";} ?></div><!--close bedbath-->
        <div id="pets"><? if ($row['type'] == "COMMERCIAL") {echo ""; } else if ($row['pets'] == "1") { echo "PETS OK"; } else { echo "NO PETS"; } ?></div>
       
      <div id="downloadapp"><a href="/pdfs/RentalApplication.pdf">Download Application</a></div><!--close downloadapp-->
      <div id="description">
			<? echo shorten_string($row['description'],25); ?>
 			 &nbsp;<a href = "javascript:void(0)" onclick = "document.getElementById('<? echo $i ?>').style.display='block';document.getElementById('fade').style.display='block'">click for more details.</a></div><!--close description-->
  
    </div><!--end listing-->
    <?  $i++;	} ?>
    
    
   <div id="disclaimer"><p class="fine red">
  IN THE EVENT OF TYPOGRAPHICAL ERROR IN RENT OR SECURITY AMOUNT, THE RENT OR SECURITY IS THE TRUE AMOUNT NOT THE ERROR PRICE.  TENANTS IN HOUSES ARE RESPONSIBLE FOR THE WATER AND SEWER BILL.  4 SEASONS REALTY PAYS THIS AND TENANTS REIMBURSE 4 SEASONS.  THIS SAVES YOU PAYING A HIGH WATER DEPOSIT.   MOST SEPTIC SYSTEMS WILL ONLY SUPPORT A FAMILY OF 4 TO 6 IN A HOUSE.  MOST HOUSES ARE ON ONE YEAR LEASES,  UNLESS STATED OTHERWISE.  IF ALLOWED: DOGS ARE AN EXTRA MINIMUM $300+ DEPOSIT AND USUALLY OUTDOOR ONLY, GENTLE BREED, MAXIMUM 30 LBS AND NO BARKING DOGS.  DEPOSIT AND 1ST MONTHS RENT MUST BE PAID IN CASHIER’S CHECK OR MONEY ORDER ONLY!!!
</p></div><!--close discplaimer-->

Last edited by Fou-Lu; 10-10-2011 at 05:21 PM.. Reason: Changed from icode to code
ohmitmedia is offline   Reply With Quote
Old 10-10-2011, 06:15 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Is the script you're showing us "index.php", or is it "RentalSearch.php"?

I see you have a script called "RentalSearch.php" ... that processes your form,
yet the links at the bottom for sorting seem to be referencing "index.php"?

The solution to your issue is to use PHP SESSION array to store all of the form
values AND the sort preferences. Once they fill-out the form, those values will
be retained no matter where they go. If they want to change one of them, or
change the sorting, you change the PHP SESSION variable(s).

I'm just not sure if currently you have more than one script?
Because I'm confused about how you are handling the URL variables.


.
mlseim is online now   Reply With Quote
Old 10-10-2011, 06:52 PM   PM User | #5
ohmitmedia
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
ohmitmedia is an unknown quantity at this point
The file shown is RentalSearch.php. The sort links are just referencing the current page, but changing the variables. They stay on RentalSearch.php but change the sort variables in the URL.

Thanks, I will try to implement PHP SESSION again. I did try last night, and couldn't get it to work. But.....that was at midnight and I wasn't all there
ohmitmedia is offline   Reply With Quote
Old 10-10-2011, 07:34 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Do all the SESSION (setting variables) in a different script.
Perhaps you might call it "setting.php". You process your form there,
set the SESSION variables and redirect back. That whole transaction
is invisible to the user, but it handles the header stuff you need for
setting SESSION (or cookies).

Your RentalSearch.php script only reads SESSION variables and displays either
the form or the results, or both for the query (which uses those SESSION variables).
mlseim is online now   Reply With Quote
Users who have thanked mlseim for this post:
ohmitmedia (10-10-2011)
Old 10-10-2011, 08:08 PM   PM User | #7
ohmitmedia
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
ohmitmedia is an unknown quantity at this point
thanks. not sure what I'm doing wrong. this is what i wrote to set the form results into the session variables....

PHP Code:
<?php session_start(); 

$_SESSION['searchtype'] =  $_POST['searchtype'];
$_SESSION['searchlocation'] = $_POST['searchlocation'];
$_SESSION['searchbedrooms'] = $_POST['searchbedrooms'];
$_SESSION['searchpetok'] = $_POST['searchpetok'];
$_SESSION['searchpricelow'] = $_POST['searchpricelow'];
$_SESSION['searchpricehigh'] = $_POST['searchpricehigh'];
?>
When I put this into the RentalSearch.php and then echo $_SESSION['searchtype'] etc, it displays the proper search terms and I get search results. But, upon clicking the sort links it resets the variables and I don't get any results. This makes sense to me - I just put it in RentalSearch.php to echo and test that it worked, which it did.

I moved the script to SearchProcess.php and added a redirect at the bottom

PHP Code:
header'Location: RentalSearch.php?field=price&order=desc' ) ; 
Now when I search, it runs the script and redirects well, but I get no search results, and when I echo the session variables, they come up blank.

I am echoing, just to test.....

PHP Code:
<? echo $_SESSION['searchtype']; ?>
<? 
echo $_SESSION['searchlocation']; ?>
Have any idea what I'm missing?
ohmitmedia is offline   Reply With Quote
Old 10-10-2011, 08:56 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
PHP Code:
<?php session_start(); 

if(
$_GET['field']){
$_SESSION['field']=$_GET['field'];
}
if(
$_GET['order']){
$_SESSION['order']=$_GET['order'];
}
$_SESSION['searchtype'] =  $_POST['searchtype'];
$_SESSION['searchlocation'] = $_POST['searchlocation'];
$_SESSION['searchbedrooms'] = $_POST['searchbedrooms'];
$_SESSION['searchpetok'] = $_POST['searchpetok'];
$_SESSION['searchpricelow'] = $_POST['searchpricelow'];
$_SESSION['searchpricehigh'] = $_POST['searchpricehigh'];
?>
Now you have those two new SESSIONS to use ...
field and order.


.
mlseim is online now   Reply With Quote
Old 10-10-2011, 09:52 PM   PM User | #9
ohmitmedia
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
ohmitmedia is an unknown quantity at this point
Thanks - that wasn't it, but I did figure it out. I just needed to add session_start() to every page that I want to use those variables.

Thanks for the help!
ohmitmedia is offline   Reply With Quote
Old 10-10-2011, 10:10 PM   PM User | #10
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Oh yes ... sorry I forgot to mention that.
Glad you got it working, and I hope this has helped you learn more PHP.
mlseim is online now   Reply With Quote
Reply

Bookmarks

Tags
form, mysql, php, sort

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:45 AM.


Advertisement
Log in to turn off these ads.