leesider
leesider

Reputation: 109

Codeigniter php MVC

I have a table called 'News' with three columns: 'id', 'title' and 'details'

I have a function ('get_entry') inside a codeigniter model class (called 'News_model')

function get_entry()
    {
        $this->load->database();
        return $this->db->select('id,title,details')->from ('news');
        $data['newsarray'] = $this->db->row_array();      
        return $data['newsarray'];
    }

I am connecting to the db so that is not the problem.I want to return an iterable array from get_entry() by calling the function from a controller file with the followlwing code. I want to push it into another array (called '$data['theNews']') using the code below.

foreach ($this->News_model->get_entry() as $key => $value){
            array_push($data['theNews'],$value->title);
        }

I have been using the code on this (https://www.codeigniter.com/user_guide/general/models.html) as a template (in particular the function 'get_last_ten_entries()' but I think I am close with the code I posted above. I would appreciate any help.

Upvotes: 0

Views: 102

Answers (2)

leesider
leesider

Reputation: 109

Changing the code to this in the function worked:

function get_entry()
    {
        $this->load->database();
        $query =  $this->db->get('news');
        //return $query->result();

        foreach ($query->result() as $row)
        {
            echo "</br>";   
            echo $row->id;
            echo "</br>";       
            echo $row->title;
            echo "</br>";   
            echo $row->details;
            echo "</br>";   
        }

    }

Calling the function like so prints it out:

$news_array = $this->News_model->get_entry();

Upvotes: 1

Kvash
Kvash

Reputation: 163

About your code:

You have two 'return' in your get_entry function:

function get_entry()
{
    $this->load->database();
    // First
    return $this->db->select('id,title,details')->from ('news');
    $data['newsarray'] = $this->db->row_array();      
    // Second
    return $data['newsarray'];
}

Change it to:

function get_entry()
{
    $this->load->database();
    $query = $this->db->select('id,title,details')->from('news');
    $data['newsarray'] = $query->row_array();      
    return $data['newsarray'];
}

It should work now.

Some advices:

Don't use Codeigniter 2 anymore. Version 3 is alive.

If you plan to return whole table columns, i suggest you to use the following code for the query:

$query = $this->db->get('news', 1, 20);

Where 1, 20 is the limit.

Now you can get the result:

return $query->result();

A simple example:

function get_entry()
{
    $this->load->database();
    $query =  $this->db->get('news', 1, 20);
    return $query->result();
}

This method returns the query result as an array of objects that you can print like so in your controller:

$news_array = $this->News_model->get_entry();
foreach ($news_array as $news)
{
        echo $news->id;
}

Look at CI 3 Query Builder query builder for more examples.

One more suggestion, just autoload the database library in application/config/autoload.php if you need it globally.

Upvotes: 1

Related Questions