Welcome Guest, Not a member yet? Register   Sign In
I need help :)
#1

[eluser]Unknown[/eluser]
hello sorry for bothering you guys. I'm still new to codeigniter and PHP/MySQL I just want to ask if what I did wrong why I can't make this code work.

I did these codes but it seems not working on my end any error I made?

I keep receiving this error message
Quote:A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/users.php

Line Number: 23


Code:
Model:

function post_all(){
      $qry = $this->db->select('*')
             ->from('users')
             ->join('posts','posts.auth_id=users.id')
             ->get();
      if($qry->num_rows > 0){
          return $qry->result_array();          
      }
      return FALSE;
    }
Code:
Controller:

function Index(){        
        if($this->session->userdata('logged_in')){
            $data['posts'] = $this->post->post_all();
            $this->load->view('users/index',$data);
        }else{
            redirect('users/login');
        }
    }
Code:
view:

<?php foreach($posts as $p):?>
    <?php echo $p['post'];?>
<?php endforeach;?>
#2

[eluser]michalsn[/eluser]
Model:
Code:
if($qry->num_rows() > 0)
{
    return $qry;          
}
View:
Code:
<?php foreach($posts->result_array() as $p):?>
    <?php echo $p['post'];?>
<?php endforeach;?>

http://ellislab.com/codeigniter/user-gui...sults.html
#3

[eluser]cideveloper[/eluser]
First thing I see is that $qry->num_rows > 0 should be $qry->num_rows() > 0

Do not forget the brackets.

Secondly if your model returns FALSE which I think it is doing every time due to the above error $posts in your view will be the FALSE which will be an "Invalid argument supplied for foreach()"
#4

[eluser]Unknown[/eluser]
guys thank you ... now I now why it is happening.. thank you so much Smile
+rep for the help guys..




Theme © iAndrew 2016 - Forum software by © MyBB