0

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.

4
  • Your get_entry function retunr 1 row only. If you want to iterate over several rows - rewrite your function so it returns 10 or more results. Commented May 30, 2015 at 14:14
  • Please post what you are getting in $data['newsarray'] Commented May 30, 2015 at 14:18
  • 1
    The built-in code snippets are only for JavaScript/HTML/CSS... you cannot run PHP in your OP. Removed. Thanks. Commented May 30, 2015 at 14:18
  • Here you can find most recent documentation for framework: v3, v2. Commented May 30, 2015 at 15:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Asterisks are not part of the code. Simply use comments to indicate the line. // <-- this line
Can you please post the error? Which code are you using? The first or the last example?
Since I changed the 'get_entry' function to what you posted I am not getting any errors when I run the php page but now when I try to call this function from the controller like below I get nothing back. I want to add each row returned from the function to an array called 'theNews' but for a start I just want to echo out what is coming back so I have the line that adds to the array commented out as you can see. $data['theNews']=array(); foreach ($this->News_model->get_entry() as $key => $value){ echo $key;//array_push($data['theNews'],$value->title); }
Ok, got it, but i can't understand which of the function you're using, is this one? : function get_entry() { $this->load->database(); $query = $this->db->get('news', 1, 20); return $query->result(); }
Yes I am using that code in the function. I am confident that is working ok
|
1

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.