Welcome Guest, Not a member yet? Register   Sign In
loading the results of a foreach
#1

[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
#2

[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.
#3

[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>";




Theme © iAndrew 2016 - Forum software by © MyBB