Paginating db data using query only in a Model |
[eluser]Ngulo[/eluser]
hello to all guys, I'm having a lot of problems to do Pagination of data from the db. I would only use my Model to make query db. so i put this function into my Model: Code: function populateByCategory($category) but when i go to the Controller i can't retrieve them so: Code: $category = $this->uri->segment(3); any suggest guys? how can i retrieve data from db now and paginate them? thanks i hope you'll understand my bad enlglish :/
[eluser]Federico Baña[/eluser]
I would return $query->result() in the model first. Code: function populate() Then in the controller you have to split the result array yourself. Code: $photos = $this->photos->populate(); // This returning the $query->result(); instead of $query http://php.net/manual/en/function.array-slice.php
[eluser]Ngulo[/eluser]
Hi Federico, really thanks for time spent with my problem :/ i know i'm a beginner,but i can't retrieve again my db datas i made this: Model: Code: function populateByCategory($category) Code: $category = $this->uri->segment(3); Code: <table class="category-photo"> i made a var_dump($paginator); and result seems to be right: Code: array(1) { ["PhotoList"]=> array(1) { [0]=> object(stdClass)#16 (7) { ["id"]=> string(3) "123" ["title"]=> string(0) "" ["description"]=> string(0) "" ["photo"]=> string(20) "fasd.jpg" ["category"]=> string(4) "2001" ["date"]=> string(10) "28-07-2010" ["datetime"]=> string(8) "16:27:09" } } } also do you know if my base url is so: Code: http://localhost/asd/index.php/photo/category/2001/ thanks again !!!!
[eluser]ELRafael[/eluser]
Ngulo, Try to use $this->db->last_query() to check with the query is ok. Look a piece of my code Model (model_name) Code: function search($start, $end) Code: $per_page = 5; Code: <?php foreach ( $casas->result() as $c ) : ?>
[eluser]Ngulo[/eluser]
Hello ELRafael, maybe I explained evil, because of my bad English. I have an array_slice() that contains all data taken from db. Then if I try to var_dump () (what I posted above) ,i can see this same array data, then the query is right ... Since the data are correct (I've checked) The problem is really that I can not get them out and print them in the view! I suppose
[eluser]ELRafael[/eluser]
Hum.... Inside your view loop, if you try to write something... Code: <?php foreach ( $paginator['PhotoList'] as $row ): ?> I guess you're doing this wrong Code: <img src="<?php echo base_url()?>uploads/img/<?php echo $row['category'].'/'.$row['photo'] ?>"/> Try to use something like that: Code: <img >category; ?>" /> Don't worry about your english!! My english is bad too
[eluser]Ngulo[/eluser]
hi guys,i just fixed my problem....thanks to all of you the problem was in my view i use this to make work my controller and model: Code: <?php foreach ($paginator['PhotoList'] as $row){?> really thanks for suggestions
[eluser]Ngulo[/eluser]
[quote author="Ngulo" date="1281118112"]hi guys,i just fixed my problem....thanks to all of you the problem was in my view i use this to make work my controller and model: Code: <?php foreach ($paginator['PhotoList'] as $row){?> really thanks for suggestions [/quote] i can't right the code inside this forum texteditor: anyway this is--- <?php foreach ($paginator['PhotoList'] as $row){?> <tr> <td><img >category.'/'.$row->photo ?>"/></td> </tr> <?php ;}?> <?php echo $this->pagination->create_links();?>
[eluser]Jondolar[/eluser]
I don't think you really want to return the entire data set every time. You only want to return the records to be displayed, right? Your model could have two functions, one that returns just the count of your query based on your "where" clause. You call that to get the total records. Then, another query that returns the results of the query based on your "where" clause AND your "limit" clause. Although this takes two trips to the database, it doesnt' return 10,000 records just to display 25 and it doesn't require the array_split() function. Alternatively, you could keep the full count of records per category in the category record as a field. Then, if you already queried the category prior to retrieving the photo records, you'd already have the count. Any time an update or delete is done, just update the category count. Good luck with your project. |
Welcome Guest, Not a member yet? Register Sign In |