0

So I inherited an old site from another developer, and I'm not really a programmer so I'm having some trouble. I've put the code into a Fiddle: https://jsfiddle.net/s6coraf5/

Basically there are different categories of real estate properties and when you click on different pages it's supposed to filter them and only display the ones specific to whatever page you're on. The problem is that no matter what page you're on, it's just displaying everything. I've narrowed down some specific code but can't figure out why it isn't applying it right.

In the php there's:

$select_title = "Unknown";      
    if ($select_type == "all") { $select_title = "All Listings"; }  
    if ($select_type == "office") { $select_title = "Office"; }     
    if ($select_type == "industrial") { $select_title = "Industrial"; }     
    if ($select_type == "retail") { $select_title = "Retail"; }     
    if ($select_type == "shoppingcenter") { $select_title = "Shopping Center"; }    
    if ($select_type == "land") { $select_title = "Land"; } 
    if ($select_type == "agricultural") { $select_title = "Ranch / Farm"; } 
    if ($select_type == "investment") { $select_title = "Investment"; }     
    if ($select_type == "lodging") { $select_title = "Lodging"; }   
    if ($select_type == "sportsentertainment") { $select_title = "Sports /      Entertainment"; }

In the HTML there are various places where those $select_type's are supposed to be applied:

a href="properties.php?select_type=<?php echo $select_type;?>&select_city=<?php echo $select_city;?>&priceForm=<?= $lowPrice;?>,<?= $highPrice; ?>&sqft=<?= $lowSize;?>,<?= $highSize; ?>&sort_type=city, asking_price desc"><font size=4><b>Location,</b></a>

it's only applying the "all" one though on every page. Again, the fiddle has the full php and html which is probably more helpful. I realize it's ugly and bad but maybe someone can see something obvious that I can't.

Thanks in advance for any help anyone can provide.

ViPeRx007
  • 13
  • 4
  • Please take a second look at the fiddle you posted. It's kind of a mess. – kittykittybangbang Aug 07 '15 at 19:40
  • 1
    That looks to me like PHP code, not JavaScript - which is why it is rendered poorly in JSFiddle. – Maverick976 Aug 07 '15 at 19:40
  • Yea, I kinda said I'm not a programmer. I didn't write any of the code either. I was just tasked with figuring out why it isn't filtering properly. I'm actually a front-end designer. Maybe I should just find someone else to help out. It's a bit over my head. I was just hoping someone here might be able to see something obvious. – ViPeRx007 Aug 07 '15 at 19:45

2 Answers2

1

Based on the PHP code in the fiddle (Which really shouldn't be there since the fiddle is for Javascript), it seems like the problem is that you never use the select_type given in the URL.

Take a look at this line. This is the first time $select_type is used.

if (!isset($select_type)) $select_type = "all";

Thus, $select_type will always be all. Instead you should either change it to:

if (!isset($select_type)) $select_type = $_GET['select_type'];

Or just add this line before it:

$select_type = $_GET['select_type'];
Zsw
  • 3,920
  • 4
  • 29
  • 43
  • Awesome, this fixed it! I know now that the Fiddle wasn't the best place to post the code. I don't know of a similar site for PHP though. Is there one? Anyway, thanks so much! I appreciate it. – ViPeRx007 Aug 07 '15 at 20:06
  • @ViPeRx007 Here are some suggestions I found http://stackoverflow.com/questions/3869226/is-there-a-place-online-that-i-can-test-my-php-code http://stackoverflow.com/questions/4616159/is-there-a-php-sandbox-something-like-jsfiddle-is-to-js – Zsw Aug 07 '15 at 20:10
0

Your ssql query in your jsfiddle seems like it might be the culprit. I'll put it here to make it easier:

select properties.property_id,selected_subtypes.property_type,properties.listing_type,properties.city,properties.asking_price,memberships.name,properties.membership_id,properties.building_size,memberships.website,properties.sold_price
                     from selected_subtypes,properties,memberships where (selected_subtypes.property_id = properties.property_id) 
                      and (properties.membership_id = memberships.membership_id)
              and (memberships.status = 'Active') and (properties.sold_information = ' ' or properties.sold_information = 'Undisclosed')
                      and ((selected_subtypes.property_category ='".$select_type."' or '".$select_type."'='all')
                      or (selected_subtypes.property_type ='".$select_type."'))
                      and (properties.city = '".$select_city."' or '".$select_city."'='all') 
                      and (properties.asking_price BETWEEN ".$lowPrice." and ".$highPrice.")
                      and (properties.building_size BETWEEN ".$lowSize." and ".$highSize.") 
                      ".$date_sql."
                      order by ".$sort_type

The query appears to be, in each line, be selecting $select_type OR 'all

This boolean approach will always bring back either of those, so it would bring back "all" every time.

If you want to bring back only the selected type, you'd need to eliminate the "all" within the OR in these rows.

The easiest way to handle this would be to set the value $select_type to be equal to "all" if that is what is selected, or else, the specific type. One way he way I do "all" queries is to set the value to be "1=1" which will always be true.

In other words, modify the query like so (for each of the selected types) to show this (I changed the OR to AND in this case)

AND selected_subtypes.property_type ='".$select_type."'

and then in the php modify the code to be something like this:

if (!isset($select_type)) {
   $select_type = "1=1"
} 
else {
   $select_type = $_GET['select_type'];
}

Another thing to be aware of

This particular code is somewhat vulnerable to SQL injection, so you might want to modify the way that you query the database. I strongly suggest you look into prepared statements, either using mysqli or PDO

nomistic
  • 2,902
  • 4
  • 20
  • 36