CodeIgniter Forums
loading the results of a foreach - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: loading the results of a foreach (/thread-15290.html)



loading the results of a foreach - El Forum - 01-30-2009

[eluser]Unknown[/eluser]
I am new to working with MVC and am struggling with having my result set load properly.

I believe my model should only consist of the query to the database. But currently it does the foreach as well.
Code:
function retrieve_members ()
    {
        $query = $this->db->query('SELECT id, firstName, lastName, memberNum, email FROM member');
if ($query->num_rows() > 0)
            {
       foreach ($query->result() as $row)
               {
          echo $row->firstName;
          echo $row->lastName;
          echo $row->memberNum;
          echo $row->email;
               }
        }
}

The Control currently has

Code:
$this->load->database();
$this->load->model('CMS_model');
$query = $this->CMS_model->retrieve_members();    
$this->load->view('template');

When I move the foreach to the control, I am told "Call to a member function num_rows() on a non-object .... on line 25"

I am obviously accessing the model incorrectly. Where am I going wrong?

Thank you


loading the results of a foreach - El Forum - 01-30-2009

[eluser]Pascal Kriete[/eluser]
You need to return something to the controller. Right now you're just echoing (adding to the browser output).

You could return the result set if you have results:
Code:
function retrieve_members()
{
    $query = $this->db->query('SELECT id, firstName, lastName, memberNum, email FROM member');
    return ($query->num_rows() > 0) ? $query->result() : FALSE;
}

I prefer just returning the $query object, but let's use the above example - the controller could be:
Code:
$data['members'] = $this->CMS_model->retrieve_members();    
$this->load->view('template', $data);

And then loop through $members in the view and show the details (or do whatever you need to with the data, I obviously don't know where this is going).

Hope that helps.
Welcome to CodeIgniter.


loading the results of a foreach - El Forum - 01-30-2009

[eluser]Unknown[/eluser]
Thank you PascalKriete for the quick response. That worked great. I just wasn't aware of the return. Thanks for the warm welcome to the CodeIgniter community.

For future reference:
MODEL
Code:
function retrieve_members ()
    {
        $query = $this->db->query('SELECT id, firstName, lastName, memberNum, email FROM member');
        return ($query->num_rows() >0) ? $query->result() : FALSE;
    }
CONTROLLER
Code:
function index()
    {    
        $this->load->database();
        $this->load->model('CMS_model');    
        $data['include'] = 'control_panel';
        $data['members'] =$this->CMS_model->retrieve_members();            
        $this->load->view('template', $data);
    }
VIEW
Code:
echo "<table>";
echo "<tr><td>First Name</td><td>Last Name</td><td>Membership Number</td><td>Email</td></tr>";
   foreach ($members as $row)
               {
          echo "<tr><td>";
          echo $row->firstName;
          echo "</td><td>";
          echo $row->lastName;
          echo "</td><td>";
          echo $row->memberNum;
          echo "</td><td>";
          echo $row->email;
          echo "</td></tr>";
               }
echo "</table>";