Welcome Guest, Not a member yet? Register   Sign In
I can't get a simple foreach loop going here!
#1

[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');
$query = $this->db->get();
$this->load->view('blogindex', $query);
in my controller. . .
Code:
<?php foreach($query as $row): ?>
    <div class="news_title">&lt;?php $row['post_date']; ?&gt;&nbsp;&lt;?php $row['post_title']; ?&gt;</div>
    <p class="news_text">&lt;?php $row['post_data']; ?&gt;</p>
&lt;?php endforeach; ?&gt;
in my view.

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');
$query = $this->db->get();
$data['results'] = $query->result_array();
$this->load->view('blogindex', $data);
and then &lt;?php foreach($results as $row): ?&gt; in my view. That does not return any errors! But it does not return any of the results, either! It seems to loop twice, but doesn't fill anything in.

If instead of using the view, I put
Code:
if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                echo $row->post_id;
                echo $row->post_title;
                echo $row->post_data;
                echo $row->post_date;
                echo $row->post_fa_user_id;
                echo $row->post_hidden;
            }
        }
in my controller, the query does return results, so I know the query is good and there are results to be displayed.


So that is where I am right now, I've created a bit of a mess, no?

Thank you for any help!
#2

[eluser]chejnik[/eluser]
Hello, I am beginner myself, but

this is correct
Code:
$this->db->select('*')->from('blog_posts')->order_by('post_date');
$query = $this->db->get();
$data['results'] = $query->result_array();
$this->load->view('blogindex', $data);

possibly in view
Code:
foreach($results as $row):
echo $results['title'];
endforeach;

$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
#3

[eluser]chejnik[/eluser]
Hello again
this is actually correct

Code:
foreach($results as $row):
echo $row['title'];
endforeach;
#4

[eluser]Lone[/eluser]
Argh! Do the following for your foreach statements - much neater!

Code:
foreach($results as $row) {
  echo $row['title'];
}
#5

[eluser]Phil Sturgeon[/eluser]
[quote author="Lone" date="1203099594"]Argh! Do the following for your foreach statements - much neater!

Code:
foreach($results as $row) {
  echo $row['title'];
}
[/quote]

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 }.
#6

[eluser]patbert[/eluser]
Hahaha, wow! I really am a moron.

In my foreach loop I just had

&lt;?php $row['post_date']; ?&gt;

instead of

&lt;?php echo $row['post_date']; ?&gt;

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() {
        if (isset($response)) {
            echo $response;
        }

        $this->db->select('*')->from('blog_posts')->order_by('post_date');
        $query = $this->db->get();
        $data['results'] = $query->result_array();

        $this->load->view('blogindex', $data);
    }

    function addpost() {
        $this->freakauth_light->check('superadmin', true);
        $this->load->helper('form');

        $this->load->view('addpost');

    }

    function submitpost() {
        $this->freakauth_light->check('superadmin', true);

        $_POST['post_date'] = date("Y-m-d H:i:s");

        $this->db->insert('blog_posts', $_POST);

        $response = "You just submitted a post I hope that worked lol";

        $this->index();
    }
So you see that in the submitpost() function I am setting $response, which I am then trying to display when I call $this->index(). The problem is that the $isset($function) if statement always fails because for some reason when it isn't there.

Again, I am sure I am missing something fundamental here.

Thank you again for your time.
#7

[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 Tongue

In views I have been getting into the habit of the following for a foreach:

Code:
&lt;? foreach ($results as $row) { ?&gt;
Blah blah blah freakn blah
&lt;? } // end results output ?&gt;
#8

[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:
&lt;?php
class Blog extends Controller {

    function index() {
        if (isset($GLOBALS['response'])) {
            echo $GLOBALS['response'];
        }

        $this->db->select('*')->from('blog_posts')->order_by('post_date');
        $query = $this->db->get();
        $data['results'] = $query->result_array();

        $this->load->view('blogindex', $data);
    }

    function addpost() {
        $this->freakauth_light->check('superadmin', true);
        $this->load->helper('form');

        $this->load->view('addpost');
    }

    function submitpost() {
        $this->freakauth_light->check('superadmin', true);

        $data = array();
        $data['post_fa_user_id'] = $this->input->post('post_fa_user_id');
        $data['post_title'] = $this->input->post('post_title');
        $data['post_data'] = $this->input->post('post_data');
        $data['post_date'] = date("Y-m-d H:i:s");

        $this->db->insert('blog_posts', $data);

        $GLOBALS['response'] = "You just submitted a post I hope that worked LORF";

        $this->index();
    }

}
?&gt;
Is doing this with $GLOBALS[] a bad idea? Is there a better way?
#9

[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 Wink

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');
#10

[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):
echo $row['title'];
endforeach;
try this:
Code:
foreach($results as $row):
echo $row->title;
endforeach;
Worked for me.




Theme © iAndrew 2016 - Forum software by © MyBB