![]() |
need help,,, reverse foreach or reverse the query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: need help,,, reverse foreach or reverse the query (/showthread.php?tid=3859) Pages:
1
2
|
need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]H8train[/eluser] is there a way to reverse this maybe to start from the bottom instead of the top: <?php foreach($query->result() as $row): ?> or to reverse the query: $data['query'] = $this->db->get('entries'); basically each row has an incrementing id and I want to post the highest id number first, as in to post the newest id. need help. Thanks Rich need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]Alex007[/eluser] [quote author="H8train" date="1193368234" $data['query'] = $this->db->get('entries'); [/quote] use the ORDER BY .. DESC clause: Code: $this->db->select("Field1, Field2..."); need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]H8train[/eluser] OK I tried this and it didn't work: Code: $this->db->select("id , title , body , date"); this gets me this error: Fatal error: Call to a member function on a non-object in application/views/blog_view.php on line 190 which that line is my foreach: <?php foreach($query->result() as $row): ?> need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]John_Betong[/eluser] In your View try: Code: <?php foreach($query as $row): ?> need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]H8train[/eluser] ok that at least got me some output but no content. my view looks like this: Code: <h3><?=$row->title?></h3> its not grabbing the actual content and my anchor link doesnt contain the post ID anymore so the comments won't work correctly. ok heres all that I have that works properly: from My blog.php controller Code: function index() from my blog_view.php view file: Code: <p> and my output is flowing like this: Code: Post 1 date 2005 Code: Post 3 date 2007 I just want the newly posted data to be added at the top and not the bottom. if anyone can help me to achieve this it would be greatly appreciated. I am new to PHP and would think this is an easy fix to a seasoned PHP coder. Thank you for your replies. Rich need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]Derek Allard[/eluser] Your view looks fine. Definately this is a case of just an orderby. Try reverting back to the working code (only in the wrong order). Post your controller (don't worrry about your view). Then add the orderby as suggestioned. Try your code. Anything? Post that also for us. need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]H8train[/eluser] ok heres my controller that works: Code: <?php I tried it like this with no luck Code: function index() I also tried a few variants of the orderby with no luck. Thanks Rich need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]John_Betong[/eluser] Hi H8train, Ok for the moment forget about your View file and concentrate on your data. Try this: Code: function index() need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]H8train[/eluser] I now get this error Parse error: syntax error, unexpected ':' in /blahblah/application/controllers/blog.php on line 28 I tried changing":" to";" (well I say I tried it but I changed it in the php developer app that I'm using and the app gave me warnings that the code was wrong)with no luck, im just having a hard time learning this but I think im getting better. So anyway how can I fix this? Thank you for your replies. Rich woops forgot to say whats on that line: Code: foreach($query->result_array(): need help,,, reverse foreach or reverse the query - El Forum - 10-25-2007 [eluser]Rick Jolly[/eluser] You are using "result_array" instead of "result". Result_array returns an array of data so you need to use array syntax - something like this: Code: <?php foreach($result as $row): ?> |