March 26, 200917 yr i have a list of items on my site which are displayed in a long list, i would like to be able to filter these result but not sure how. i want to filter them by location. each listing has a location but i would like to make a search or filter for a location. at the moment the listing just lists everything ie <?php foreach ($listings as $listing){ $imgal = "./listings/showImage.php?pic=".$listing['image']."&size=150"; echo "<tr><td valign=top width=152><a href=\"java script:popUp('./listings/index.php?page=include_showlisting&lid=".$listing['lid']."')\"><img src='$imgal' alt='no image uploaded' border=0></a></td>" ."<td valign=top>" ."<h2><a href=\"java script:popUp('./listings/index.php?page=include_showlisting&lid=".$listing['lid']."')\">".$listing['Title']."</a></h2><br>Location (town):".$listing['City']."<br><span class=\"t2\">\$ ".$listing['Price']."</span><br>".$listing['description'] ."</td></tr>"; echo "<tr><td colspan=2><table width='100%' class='t4' cellpadding=0 cellspacing=0 height=2><tr height=2><td height=2></td></tr></table></td></tr>"; } ?> basically the above code get the information or all the information from the listings and then displays it, the repeats its self depanding on the number of listings. but i would like to filter them so people can search for a recovery person near them ie search via city anyhelp??
March 26, 200917 yr Author basically i want to only display the results of a pay ie daiply only results where .$listing['City']. = London or .$listing['City']. = Manchester or .$listing['City']. = $town where $town is chosen from a dropdown
March 26, 200917 yr You would add the extra filter in the SQL query so only the results you want are sent to the browser.
March 26, 200917 yr If you post the full page I can show you how to do it. I think you're looking at it from the wrong angle. The way you do it now is to fetch all the results from the DB, now you could put an if inside your loop to only display results from a specific city or whatever but your script would fetch everything from the DB again and only display ones that met the criteria. It is much less resource intensive just to add the extra criteria to your query so that it only returns and loops through the relevant data.
March 26, 200917 yr Author their is no DB just stored in flat files which are created. my DB is used by my forum. just so you know. here is the full page minus the headers fotters html which displays the full listings. <?php // get an array full of appropriate data $listings = array(); $count = 0; foreach (glob("./listings/*") as $filename) { if (is_dir($filename)&&($filename!="./listings/.")&&($filename!="./listings/..")){ $lid = substr($filename, 11); include "./inc/fill_fields.inc.php"; $listing["lid"]=$lid; //get other stuff, too. include "./inc/fill_extras.inc.php"; $listing["image"]=$mainimage; $description = substr($description, 0, 40)."..."; $listing["description"]=$description; $listings["$count"] = $listing; $count++; } } ?> <script LANGUAGE="JavaScript"> function popUp(URL) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=55 0,height=500,left = 302,top = 134');"); } </script> <p>test text</p> <table width=100% cellspacing=0 cellpadding=4> <?php foreach ($listings as $listing){ $imgal = "./listing/showImage.php?pic=".$listing['image']."&size=150"; echo "<tr><td valign=top width=152><a href=\"java script:popUp('./listing/index.php?page=include_showlisting&lid=".$listing['lid']."')\"><img src='$imgurl' alt='no image uploaded' border=0></a></td>" ."<td valign=top>" ."<h2><a href=\"java script:popUp('./listing/index.php?page=include_showlisting&lid=".$listing['lid']."')\">".$listing['Title']."</a></h2><br>Location (town):".$listing['City']."<br><span class=\"t2\">£ ".$listing['Price']."</span><br>".$listing['description'] ."</td></tr>"; echo "<tr><td colspan=2><table width='100%' class='t4' cellpadding=0 cellspacing=0 height=2><tr height=2><td height=2></td></tr></table></td></tr>"; } ?> </table>
March 26, 200917 yr Ah, that makes it slightly more complicated. I'm not too used to using flat files (so someone might be able to correct me) but maybe the other method I mentioned might be your best bet. So inside your loop we would add: foreach ($listings as $listing){ if($listing['City'] == "London"){ $imgurl = "./vsreal/showImage.php?pic=".$listing['image']."&size=150"; echo "<tr><td valign=top width=152><a href=\"java script:popUp('./listing/index.php?page=include_showlisting&lid=".$listing['lid']."')\"><img src='$imgurl' alt='no image uploaded' border=0></a></td>" ."<td valign=top>" ."<h2><a href=\"java script:popUp('./listing/index.php?page=include_showlisting&lid=".$listing['lid']."')\">".$listing['Title']."</a></h2><br>Location (town):".$listing['City']."<br><span class=\"t2\">£ ".$listing['Price']."</span><br>".$listing['description'] ."</td></tr>"; echo "<tr><td colspan=2><table width='100%' class='t4' cellpadding=0 cellspacing=0 height=2><tr height=2><td height=2></td></tr></table></td></tr>"; } } That would only display entries where the city is "London". You will have to create a form that will tell the script what they are filtering on and what the filter is.
March 26, 200917 yr Author legend!!!! simple and effective great help and works a treat, just need to create the dropdown now
March 30, 200917 yr Author just to add to this. how would i go about displaying a message when their are no results for the search?? at the moment the screen just show blank as their is nothing to display but how wuold i make a message to say, nothing in yor area please try again at a later date or widen your seach area. or something like that
April 2, 200917 yr The $listing is an array so you can check the size of it before running the foreach loop. if(count($listing) != 0) { // run it } else { //echo the 'Nothing in your area' message }
April 2, 200917 yr Author The $listing is an array so you can check the size of it before running the foreach loop. if(count($listing) != 0) { // run it } else { //echo the 'Nothing in your area' message } cheers for that but the only problem is that if i have 10 listings in total this method above repeats the echo 10 times. or how ever many time i have a listing.
April 2, 200917 yr Author also when the listings are filtered it says no listing for all the listing not displayed.
Create an account or sign in to comment