How can i make model query based on images |
Hi thanks for some respon in past now i have been working on my cms and need help
my problem is here tell by images here http://prntscr.com/85zesi if in php native i can do that by save each id in every function but in model codeigniter i'm still figure it out ! and here is my model Code: //detail trend I don't know how to get data in the images, if any some suggest feel free to comment here, thanks
In CI it is much easier.
PHP Code: $query=$this->db->query("SELECT * FROM table ORDER BY id DESC LIMIT 4"); PHP Code: $main_article = $query->row(0); To display the next article at the top your right column: PHP Code: $topright_article = $query->row(1); PHP Code: for ($x=3; $x<5; $x++) { If you want to do this in a model, make your model return the $query object.
Oh great thanks for comment, hem it's easy things after read the comments, thanks bro, how can i +1 or solved this thread
edit How the last wuery show 2 last from 4 data itself. don't know how to figure it out so isu this twice here is my controller Code: $data['leftarticle'] = $this->model_front->lefttrend(); then here is view i use twice Code: <!---articles 1 main-child---> and this show another view Code: <!---articles 1 main-child---> It give me redudancy, how can i make right query based on your suggest in the last query, thanks
This just requires a little prep-work in the controller to get the data into the form that makes it easiest to work with in the view:
PHP Code: $leftarticle = $this->model_front->lefttrend(); Then, in your view, you can loop through the array instead of creating multiple views: PHP Code: <?php foreach ($rows as $row) : ?> Or, if you don't want to use a foreach() inside your view, you can put a foreach() loop inside your controller and load the same view for each row: PHP Code: // $data['rows'] = $rows; Then 'your_view' would just be something like this: PHP Code: <div class="col-lg-12 child-articles-trend">
Thanks mwhitney but has bugs in here
Code: //$query=$this->db->query("SELECT * FROM `articles` WHERE art_jenis='TREND' ORDER BY art_id DESC limit 4"); when data more than 4 or even just 4 it's oke no problem with view but i'm trying to delete 1 so data 4-1=3 it show twice here is images http://prntscr.com/86p3ga then i'm trying to delete again to be 2 data (3-1=2) in table it also show it, it suppose to be only show 2 row without third row, i mean 2 row show twice too. then tested remove data and remain 1 data also show twice, what do i wrong here ? Please Correct me where i should give if, Thanks
Use $query->num_rows() to determine how many records have been found in the database.
(08-19-2015, 09:47 PM)Wouter60 Wrote: Use $query->num_rows() to determine how many records have been found in the database. Did you mean put it in model like this Code: $query=$this->db->query("SELECT * FROM `articles` WHERE art_jenis='TREND' ORDER BY art_id DESC limit 4")->num_rows; then how in view to use if data only 1 or 2 to make my data not redudancy, thanks wouter60 for comments PHP Code: $query = $this->db->query("SELECT * FROM `articles` WHERE art_jenis = 'TREND' ORDER BY art_id DESC limit 1,2"); I've changed the limit portion of the query here to return 2 items, skipping the first item (if I wanted the first 2 items, I could use "limit 0,2" or "limit 2"). Then, instead of using the row_array() function, you can use result_array(): PHP Code: $rows = $query->result_array(); Personally, I would have my model return $query->result_array() or $query->result() rather than returning $query, but that's up to you. If you're getting duplicates when doing it this way, you should probably check your data. (08-20-2015, 07:33 AM)mwhitney Wrote: Sorry late respons cause i'm doing responsive design by the way why you change query becom limit 2 mwhitney all i want is take 4 data then my problem now is show the third and also 4 in other hand i have been tested it delete one when data still 4 it give me duplicate data which should only show data with row 3 then i delete again till data 2 it also duplicate, will you give me example how to show it with right ways Thanks for respond ya (08-21-2015, 03:16 AM)freddy Wrote: by the way why you change query becom limit 2 mwhitney all i want is take 4 data then my problem now is show the third and also 4 in other hand i have been tested it delete one when data still 4 it give me duplicate data which should only show data with row 3 then i delete again till data 2 it also duplicate, will you give me example how to show it with right ways All I did was change the query to reflect what you were doing in the code by taking row_array(2) and row_array(3). I think that we're running into a few different problems here simply because we're working off small snippets of code instead of the relevant controller, model, and view(s) and also dealing with a bit of a language barrier. To go back to the original function you supplied: PHP Code: function lefttrend($art_slug) This could be modified as follows (using query builder because it makes more sense to me than manually editing a query string, especially with MySQL's syntax for this): PHP Code: function lefttrend($art_slug, $limit = 0, $offset = 0) Then you can get whatever range of values you need from the query by passing in the relevant $limit and $offset, or you can retrieve the full query results by leaving them out. So, you could get the first one by calling PHP Code: $first = lefttrend($art_slug, 1) PHP Code: $twoThree = lefttrend($art_slug, 2, 1); |
Welcome Guest, Not a member yet? Register Sign In |