adambedford Posted March 16, 2010 Share Posted March 16, 2010 I'm designing a website and writing the PHP code to list businesses and I'm having a little trouble with displaying the records. On the homepage there is the option to browse by location or category which then adds 'location=' or 'category=' to the URL. The listings page then needs to read this and filter the records accordingly. This is pretty simple and I am comfortable doing this. The problem comes when I want to have more than one url variable. ie. ?location=somewhere&category=something. On the listings page I have a menu down the right hand side that changes content depending on what is supplied in the URL. ie. if the category is supplied, it shows subcategories. and if the location is supplied, it shows categories. I want these links to then change the URL parameters (for example, by adding '&subcategory=blah') and then on the right hand side, where the listings are, to query and return the results based on the various URL parameters supplied. One problem that I can't overcome is the different number of URL parameters that can be supplied and how to process them. So far I've written code so that if there is only 1 $_GET and it equalt 'location' I query the categories table and display that content. And if there is 1 $_GET and it equals 'category' then I query the subcategories table and list the results. I could proceed like this and try and account for every eventuality but this seems like an awful lot of code and an awful lot of repetetive querying How would I go about doing this much more dynamically and then having the results on the right hand side show dynamically based on the varying URL parameters. I don't really want to be spending hours typing out every eventuality because I an convinced there is a much better way of doing this. Would it involve adding the $_GET parameters to an array with foreach()? Just my very (very!) basic speculation. But then how would I decide that 'category' had been set and then display subcategories. and how would I filter the listing results based only on 'category'...and so on... Any help would be REALLY appreciated! Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/195449-filter-results-based-on-varying-number-of-url-parameters/ Share on other sites More sharing options...
adambedford Posted March 16, 2010 Author Share Posted March 16, 2010 anyone? this is still baffling me? I'll just have to write out every possibility otherwise. Link to comment https://forums.phpfreaks.com/topic/195449-filter-results-based-on-varying-number-of-url-parameters/#findComment-1027149 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 this should be pretty easy. <?php foreach($_GET as $a => $b) { filter = $a; value = $b; //do filtering here } ?> If you can post some of your code, I can define it a little better. Link to comment https://forums.phpfreaks.com/topic/195449-filter-results-based-on-varying-number-of-url-parameters/#findComment-1027170 Share on other sites More sharing options...
adambedford Posted March 16, 2010 Author Share Posted March 16, 2010 Thanks for your reply. To be honest, I think that most of the code I have at the moment is pretty useless! All I have is basically what I said I wanted to avoid...doing it all manually by coding for every eventuality. Essentially i have an include for the right hand side that deals with the navigation and another include for the right hand side that deals with the actual business listings. How would I go about implementing the code you suggested? I'm pretty new to PHP so I don't really know what to do with it! Link to comment https://forums.phpfreaks.com/topic/195449-filter-results-based-on-varying-number-of-url-parameters/#findComment-1027196 Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 Hmm.... Well it wouldnt be EASY, persay. And I am not going to write all the code for you... so heres some psuedo code <?php $i = 0; //Account for your vars, and generate the basic MySQL query w/ LIKE statements foreach($_GET as $a => $b) { filter = $a; value = $b; $query[$i] = "SELECT * FROM table WHERE $a LIKE $b"; $i++; } //construct your query peices $array_vals = count($query); $mysqlquery = $query[0]; for($j=1; $j <= $array_vals; $j++) { $query .= "UNION " . $query[$j]; } ?> I wont promise its the most efficient way, but it should more or less work. Link to comment https://forums.phpfreaks.com/topic/195449-filter-results-based-on-varying-number-of-url-parameters/#findComment-1027216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.