it is not paginating me, humm, dunno why :( |
[eluser]number1chump[/eluser]
Hello well the User guide, which I have thoroughly read several times, says pagination is straightforward. Yet, I dont know why I am not getting to make it. I have managed to display all of the 600 rows, but it is all in one page. I also managed to have the links (page 1, page 2 and upon clicking it just reloads again the 600 rows, so it does not separate them say in pages of 20 row display. I have tried all possible combinations, passing the extracting data from the Model to the Controller, and actually I manage to display the 600 rows without calling the View page. Maybe something in there is the key for failure, it seems as though View did not matter. Here are the files MODEL: Code: <?php CONTROLLER: Code: <?php
[eluser]number1chump[/eluser]
because even though I may load the model from the controller, then it says undefined variable $query or $data, I know very well the concepts of MVC but echoing them in the Model would at least display to me the rows, and I would try to paginate them and once I solved that, then I would try to actually get it all loaded from the Controller and echoed it at the View. In fact, that echoing at the Model is part of an Example of the User Guide
[eluser]Josh Holloway[/eluser]
Firstly, I'd stop what you're doing and setup the following: a model: Code: class Posts_model extends Model { a controller: Code: class Posts extends Controller { a view: Code: foreach ($posts->results() as $post): I'll even give you the SQL script to get this working Code: CREATE TABLE IF NOT EXISTS `posts` ( Right, once done, add a few records to the table posts and see if pagination is working. If so, you can start to substitute bits out to get it working as required.
[eluser]cideveloper[/eluser]
josh_holloway is correct. Pagination seems to be a tough thing for people to figure out. You need to follow and example first and then write your own code. Things to remember 1) $config['total_rows'] is the total rows of results from your database. 2) you need to have the offset and limit set when you are pulling results from the database. 3) Do not echo anything from your model. Quote:In fact, that echoing at the Model is part of an Example of the User Guide. Not in the pagination class user guide page or the model page. 4) be very carefull about the offset and limit. It is counter inuitive as to how you write it when you are writing out the SQL as opposed to Active Record. Active Record: Code: $this->db->limit($limit, $offset); SQL: Code: strSQL = "select * from posts limit " . $offset . ", " . $limit . ";"
[eluser]number1chump[/eluser]
Thank you a lot to both of you. I am going to reflect word by word on the code. Yes, as per $config[‘total_rows’] I know what it was because it is explained in the user guide section of pagination class. Actually in my example that was one issue, I first hard-coded the real number i had, putting 659 (even though there shows 200) I did try to make it dynamic by retrieving the Code: echo 'Total Results: ' . $query ->num_rows(); but it would tell me "undefined variable" because I was not passing it properly so I left it hardcoded at least to see if I could manage the pagination. The code that Josh has written is more elaborate than what the example of the userguide shows, so I will need a couple of hours maybe to fully understand it since I have been with codeigniter for 3 days so far. As per Active Records I am not very fond of it because it would force me to unlearn, unset, unlink the SQL that I know, so I prefer to not use Active Records for building SQL queries, although I will use it for pagination and other functions. thank you very very much Alvaro
[eluser]Josh Holloway[/eluser]
Alvaro -> any issues just post them here I find active record very very good - simplifys a lot. I will try to expand more on my post above later on to simply it and provide you with a bit more explanation of each section. The best way to get total rows is as I've done above: Code: $this->db->count_all_results('posts');
[eluser]number1chump[/eluser]
thank you a lot. I really love Code Igniter because a lot of work has been put on to make it a very clear and clean structure, yet I ll need my time to be able to use it to the fullest. I m looking forward to building my first web on MVC since my last procedural web had pages with around 1500 lines of code where jquery and css and sql and php and ajax was all on the same pages, so separating things will be a bless.
[eluser]Josh Holloway[/eluser]
Might I suggest you look at using HMVC https://bitbucket.org/wiredesignz/codeig.../wiki/Home takes some getting used to but is so much easier to work with
[eluser]number1chump[/eluser]
Been looking now into your example with detail. Nice and insightful to me. I can clearly see how you obtain from the controller the total number of rows having calculated that in the Models, by calling the model AND the function in the Model which makes that calculation which is basically a core function from Active Records. You also use Active Records to set the Limit and Offset, although you dont hardcode the numbers but set them as variables. Then when configuring the parameters for the pagination you are pointing to the controller page AND the function of the controller which contains the pagination functions. Then to obtain the data you call the Model and the function within that Model and also set that the pagination link will be on the third segment of the URI. Then once you have setup the parameters of the pagination you actually call the pagination class and finally haul all that data to the View passing it as the second parameter. Then the view echoes it. My stuff could not work because simply the echoing was being done before the pagination, it was made in the Model, although I did (and so I indicated it at the beginning of my Posting) try all combinations and that included echoing it in the Controller (which again should not be done because that is what the View is for). Like I say, yes some functions of Active Records are handy, but I prefer to stick to native SQL particularly when it comes to complex joins. I am reluctant to replace also the most simple this Code: select * FROM table_name |
Welcome Guest, Not a member yet? Register Sign In |