I can't get a simple foreach loop going here! |
[eluser]patbert[/eluser]
I am going to apologize up front for this. I know this is a simple one but it has me totally stumped. I have seen other posts relating to this on here but I can't seem to apply the solutions to my problem. I have: Code: $this->db->select('*')->from('blog_posts')->order_by('post_date'); Code: <?php foreach($query as $row): ?> I have tried changing $query to $query->result_array() in either the load view line (#3) or the foreach part of the view itself. No go. Some nice person on irc got me a bit farther. They suggested I do: Code: $this->db->select('*')->from('blog_posts')->order_by('post_date'); If instead of using the view, I put Code: if ($query->num_rows() > 0) { So that is where I am right now, I've created a bit of a mess, no? Thank you for any help!
[eluser]chejnik[/eluser]
Hello, I am beginner myself, but this is correct Code: $this->db->select('*')->from('blog_posts')->order_by('post_date'); possibly in view Code: foreach($results as $row): $query->result_array(); returns array of arrays I use often print_r ($some_array) to see how the actual array looks like. Hopefully it helps you little bit
[eluser]chejnik[/eluser]
Hello again this is actually correct Code: foreach($results as $row):
[eluser]Lone[/eluser]
Argh! Do the following for your foreach statements - much neater! Code: foreach($results as $row) {
[eluser]Phil Sturgeon[/eluser]
[quote author="Lone" date="1203099594"]Argh! Do the following for your foreach statements - much neater! Code: foreach($results as $row) { Possibly neater in the sense that there is less characters on the page, but bloody confusing when you have a few nestwed structures. Using the alternative syntax means you have at least some idea what is being closed, instead of just a random }.
[eluser]patbert[/eluser]
Hahaha, wow! I really am a moron. In my foreach loop I just had <?php $row['post_date']; ?> instead of <?php echo $row['post_date']; ?> Doh! No wonder it was looping out blanks! I do have another question since I am new to Code Igniter. What I want to do is have one controller. The index function of this controller (the one I have posted here) simply displays the db data. Then I have made a few other functions in the controller that don't have anything to display, they just perform some action on the db. So when they are done, I want it to just display the db data, so I am calling the index function from inside other functions in the controller. So I have, for example: Code: function index() { Again, I am sure I am missing something fundamental here. Thank you again for your time.
[eluser]Lone[/eluser]
[quote author="thepyromaniac" date="1203105960"] Possibly neater in the sense that there is less characters on the page, but bloody confusing when you have a few nestwed structures. Using the alternative syntax means you have at least some idea what is being closed, instead of just a random }.[/quote] Tab baby tab In views I have been getting into the habit of the following for a foreach: Code: <? foreach ($results as $row) { ?>
[eluser]patbert[/eluser]
Well I got it to do what I wanted, but I am not sure this is the best way to go about these things. . . Code: <?php
[eluser]Lone[/eluser]
Two notes: 1. Get your database code into a Model rather then in your Controller. 2. instead of using GLOBALS checkout the brand spanking new feature in the Session Class called flashdata You would add below to your 'submitpost' function Code: $this->session->set_flashdata('response', 'You just submitted a post I hope that worked LORF'); And the following to 'index' function using '$response' in the view Code: $data['response'] = $this->session->flashdata('response');
[eluser]steelaz[/eluser]
I just started playing with CI yesterday and got the same problem with looping through result, so I dumped $row in my 'view' and it turns out to be object. So instead: Code: foreach($results as $row): Code: foreach($results as $row): |
Welcome Guest, Not a member yet? Register Sign In |