![]() |
A Rather Challenging Pagination Problem - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: A Rather Challenging Pagination Problem (/showthread.php?tid=25920) Pages:
1
2
|
A Rather Challenging Pagination Problem - El Forum - 12-30-2009 [eluser]djreed[/eluser] Background on my application: I am querying business listings from a database based on a user-submitted zip code via POST. This is not like typical location-based search however. This search is for a particular type of business - rental businesses. As such, each business listing in the database has a central zip code and a delivery radius indicating their coverage area (where they will deliver rentals to). When a user searches for businesses that cover their area with a zip code, my application queries all listings from the database, loops through them calculating the coverage area for that listing (based on the equation of a circle), then determines if the user falls within a particular business's coverage area (using latitude and longitude coordinates). I'm using the very helpful Zip code library discussed here.. The problem: Still with me? That is actually not the challenging part. However, I do invite others to come up with a better solution because it seems impractical to loop through all listings this way - but I have yet to come up with a better solution. Still... this is not my main question. The real problem is how the hell I am supposed paginate the results. This is what my View file currently looks like: Code: //Print any validation errors In a nutshell, the above code loops through all listings in the database and prints results that cover the Lat/Long of inputted zip code. I invite input/commentary on the entire implementation - however, the real challenge here is how I can implement Pagination with this. Everything I come up with seems overly complicated and I am beginning to feel like I should just drop it altogether. Help! -Danny A Rather Challenging Pagination Problem - El Forum - 12-30-2009 [eluser]Chad Fulton[/eluser] The key to this will be to move the logic which picks out which listings are in the target area to the controller. At that point, you can use the pagination library included in CodeIgniter to filter the results even further (i.e. on page one, only want $listings[1] - $listings[10], and on page two, only want $listings[11] - $listings[20], etc). Also, it's often easier to work with arrays that are indexed at 0 instead of 1, but that's totally up to you. A Rather Challenging Pagination Problem - El Forum - 12-31-2009 [eluser]wowdezign[/eluser] When I was reading through your post I had the same initial thought that Chad mentioned. I realize that your view only has if...else and foreach, type logic (which is what everything I've read on the MVC pattern says is good). However, It just seems like an awful lot of decision-making going on in the view. Perhaps if you made a Zipcode_Area library that knew how to query the database, do the figures and return an array of Objects that you could loop over in your view. Maybe it could have member variables (a.k.a. instance variables, or class properties) like: Code: class Zipcode_Area{ Hope I understood correctly. It's not "a commentary", and there are probably other ways to do it. But it seems like this way would allow you to use the same code later on if you wanted to advertise this type of programming to potential customers. A Rather Challenging Pagination Problem - El Forum - 12-31-2009 [eluser]djreed[/eluser] Thank you to both of you. I appreciate the help. I have definitely wanted to move the logic to another part of my application. Both of you suggestions make a lot of sense. I'm aware the View file should not be making these decisions. That is something I should fix up for sure. I have a more basic PHP question: Is there a way to loop through results queried from the database and then add relevant results to a multidimensional array somehow? In other words: Code: $i=1; I feel like this could be a helpful solution to pagination. A Rather Challenging Pagination Problem - El Forum - 12-31-2009 [eluser]BrianDHall[/eluser] As to your last question, replace $results_array[$i]=$row; and $i++; with just $results_array[] = $row. If you use $array[] and then assign a variable, PHP automatically adds whatever you want as a new element on the end of $array. So you can do: Code: $array[] = 'blue'; And off you go. A Rather Challenging Pagination Problem - El Forum - 12-31-2009 [eluser]djreed[/eluser] Ah! Shoulda known this. Thank you! However, can I attach two arrays such as a database result to another array in this way? In other words, $result is already an array - $result['id'], $result['businessname'], $result['url'] are all contained within $result. So in this case what happens when: Code: $array[]=$result And how would I access the results? A Rather Challenging Pagination Problem - El Forum - 12-31-2009 [eluser]wowdezign[/eluser] Code: $array['a_key_of_your_choosing']=$result_a; A Rather Challenging Pagination Problem - El Forum - 01-01-2010 [eluser]Fabdrol[/eluser] Okay, I've got a few ideas. First of all, I think it would be a good idea to separate logic from display. In your controller, calculate the list of companies your zip code is in range of (is that correct english? ![]() Store them in a big, multi-dimensional, uber-cool array. (or whatever, just store them) Pass that stored array, and so on, to a view file which handles the display of the results. And by doing that, please write valid code and please get rid of those ugly <center> tags! ![]() To win some performance, you could maybe calculate some of those range variables when creating the entry in the database, so you don't have to do that in the foreach loop anymore. (in general: move every piece of static information the script processes to your insertion code, so it's just stored in the database. Then you don't need to calculate it every single time) After your big, uber-cool array with companies has been passed to the view file, you can take care of the pagination there. (to be honest, I never understood the pagination library to its full extend and prefer to write that myself!) Some sample code, to get you started: Code: <?php A Rather Challenging Pagination Problem - El Forum - 01-02-2010 [eluser]djreed[/eluser] Yeah - this helps! Thanks so much everyone. I very rarely use this forum (or any forum), but I have to say, seeing it done from different perspectives helps me learn a lot - so thank you very much. As I was working this out, I ran into one other problem (surprise!). In this scenario, the database query is being executed based on a $_POST['zip'] which is entered by a user on the prior page. Assuming the user enters a valid zip, they will be introduced to the first page of results. If they click to go to the next page of results, the $_POST data will not pass along with it? So, how would you pass $_POST['zip'] along to each page of the pagination? Plus, this just doesn't seem like good practice in general because if the user presses the browser back button you would have a mess. One possible solution would be to use a URI. How would one implement this? A Rather Challenging Pagination Problem - El Forum - 01-03-2010 [eluser]djreed[/eluser] What if I created a session variable that stored the zip code query and passed it along to each page? I've always avoided the realm of session variables just because it is not a world I understand too well - and it seems like an overly complicated solution to a simple problem. Would there be a risk to browser compatibility if I used this solution? Or is this a reasonable option? |