Adema_It_Man
Adema_It_Man

Reputation: 39

codeigniter - pass data to view

Ok I have a simple question I am using codeigniter frame work to create a simple blog. When I set up just a controller and a view I can print my database information (blog roll) to my view just fine. When I use the model controller view method i fail.

Here is what I would like to implement in to a method controller view setup.

my original view that works:

<?php
//is there an array from your search form?
if($_GET){

$books = $this->db->get('blog');//query the blog table in the database
if($books->num_rows() < 1)//are there clients to show?
{
    echo 'There are no blog post'; //error message if no post
}

else
{
    foreach(result() as $row)//loop through the blog
    {
        echo $row->title."<br>";//display each titles info
    }
}
}
?>

This is what i have set for my New model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog_Model extends CI_Model {

function get($args = null)
{
    $query = $this->db->get('blog');
    return $query->result();
    foreach(result() as $row)//loop through the books

}

function insert($data)
{

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

function update($data,$id)
{
    $this->db->where("id",$id);

    $this->db->update('blog', $data);
}
 function delete($id)
{
    $this->db->where("id",$id);


    $this->db->delete('blog');
}
}

this is my new controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller {
public function index()
{
    $this->load->model('blog_model', 'blog');
    $data['blogs'] = $this->blog->get();
    $this->load->view('blog',$data);

}


}

I am not sure what to do for my new view? I just want to echo the blog roll to the view

Upvotes: 0

Views: 478

Answers (2)

s1x
s1x

Reputation: 574

Well you´re doing a foreach in the model after the return function.

So what you return is just this:

$query->result()

You may should do the foreach in the view, this would be better placed as a model just should just return and not process information. Best place would be in this case the controller or may view - depending on how strict you are.

I did not work with CodeIgniter for a while so may try this: Controller:

class Blog extends CI_Controller
{
    public function index()
    {
        $this->load->model('blog_model', 'blog');
        $data['blogs'] = $this->blog_model->get()->result();
        $this->load->view('blog',$data);

    }


}

The View

Here goes some text...
<?php
    foreach($blogs as $post)
    {
       echo $post['someData'];
       echo $post['someData2'];
    }
?>
After all this code...

May you want to lookup this (CodeIgniter Doc).

Hope this helps. Try to

Upvotes: 1

BigKingMilo
BigKingMilo

Reputation: 1

Controller:

$data['blogs'] = $this->blog_model->get();

Even though you load the model in order to call its function you must pass its name.

Model:

Must always have result() or row() when query are applied.

Hope this helps in your endeavors

Upvotes: 0

Related Questions