Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Giant CI noob here - I can't get the data I get from the database to work with my View?
#1

[eluser]Unknown[/eluser]
Okay, so here's the code in my model:
Code:
public function getRecentProjects()
    {
        $this->load->database();
        $q = $this->db->get('projects', 5);
                return $q;
}

Now, my controller passes $q directly onto my view, how would I then access the data from there in my view?

Here's the relevant code for my controller:
Code:
$q = $this->projects->getRecentProjects();
        $this->load->view('inc/header');
        $this->load->view('main', $q);

Sorry for any spelling mistakes, etc, I've been up for a while trying to work it out.

Cheers
#2

[eluser]Chris Williams[/eluser]
If you apply print_r($q) to your variable you get some sense of what is accessible.

Returned records from a query would be found in $q->result()

Code:
foreach( $q->result() as $r )
{
  echo $r->field;
}
#3

[eluser]TWP Marketing[/eluser]
[quote author="timtamboy63" date="1309212089"]Okay, so here's the code in my model:
Code:
public function getRecentProjects()
    {
        $this->load->database();
        $q = $this->db->get('projects', 5);
                return $q;
}

Now, my controller passes $q directly onto my view, how would I then access the data from there in my view?

Here's the relevant code for my controller:
Code:
$q = $this->projects->getRecentProjects();
        $this->load->view('inc/header');
        $this->load->view('main', $q);

Sorry for any spelling mistakes, etc, I've been up for a while trying to work it out.

Cheers[/quote]

Please note that
Code:
$this->db->get('projects,5);

will return an OBJECT and uses the Active Record Class, read about that in the User Guide. I assume you want 5 records from the projects file? The get() function accepts two parameter here (limit and offset, see the User Guide).

When you pass this to your view 'main', using $this->load->view('main',$data), the ARRAY named $data will be exploded and only the array elements will be visible in the view.
Therefore, you need to extract your data from the query OBJECT and create an array ($data) to be passed to the view.
Reference the User Guide: http://ellislab.com/codeigniter/user-gui...sults.html

Code:
$q = $this->projects->getRecentProjects(); // get the query OBJECT

if ($q->num_rows() > 0) { // check that it was not an empty result

  $data = array(); // create an array

  foreach( $q->result() as $project)

   $data['projects'][] = $project; // load the data array with project objects
  }

  $this->load->view('inc/header');

  $this->load->view('main', $data); //pass the data array to the view
}
In your view, walk through the projects (which are objects themselves). Remember, the loader function has exploded the data array.
Code:
...
foreach( $projects as $proj ){

echo $proj->name;
echo $proj->title;
}
#4

[eluser]Unknown[/eluser]
That makes a lot of sense, thanks.

I tried
Code:
foreach( $q->result() as $project)

   $data[] = $project; // load the data array with project objects
  }

But I couldn't access the $project object from my view, for obvious reasons.

I'll give it a go now, cheers
#5

[eluser]TWP Marketing[/eluser]
[quote author="timtamboy63" date="1309248251"]That makes a lot of sense, thanks.

I tried
Code:
foreach( $q->result() as $project)

   $data[] = $project; // load the data array with project objects
  }

But I couldn't access the $project object from my view, for obvious reasons.

I'll give it a go now, cheers[/quote]

You would need to add an array name within the data array:
Code:
foreach( $q->result() as $project)

   $data['projects'][] = $project; // load the data array with project objects
  }
Then reference the exploded array as objects: $projects




Theme © iAndrew 2016 - Forum software by © MyBB