0

When getting a database result in Codeigniter, the result_array method returns the row data, but the array key is not the row ID. What is going wrong?

Model:

<?php
class Admin_user extends CI_Model
{
    public function list_rows()
    {
        $query = array();
        $query = $this->db->get('content');
        return $query->result_array();
    }
}

View:

<?php 
foreach ($get_all_content as $key => $values)
{
    $title = $values['title'];
    echo "<th>" . $key . "</th>";
}

Controller:

$data['get_all_content'] = $this->admin_user->list_rows(); 

print_r:

Array
(
    [0] => Array
        (
            [id] => 1
            [menu_id] => 12
            [title] => Register Domain name for free
            [sub_title] => This is the sub_title 
            [content] => this is the content description
            [description] => 
            [section] => 
        )
)
12
  • Do you print_r($data['get_all_content'];? And where did you get your print_r()? Commented Sep 10, 2015 at 22:52
  • Yes it returns, I did it at view Array ( [0] => Array ( [id] => 1 [menu_id] => 12 [title] => Register Domain name for free [sub_title] => This is the sub_title [content] => this is the content description [description] => [section] => ) ) Commented Sep 10, 2015 at 22:56
  • But when you do this $title = $values['title']; you are getting the title? Commented Sep 10, 2015 at 22:56
  • Yes! am able to get the title values easily Commented Sep 10, 2015 at 22:57
  • The key of your data is 0. Commented Sep 10, 2015 at 22:59

2 Answers 2

1

Try this:

 $yourdata = [0 => 
      ['id' => 1, 'menu_id' => 12, 'title' => 'Register Domain name for free','sub_title' => 'This is the subtitle', 'content' => 'this content', 'description' => 'description', 'section' => 'sect']
 ];

    foreach($yourdata as $key => $val)
    {
        foreach($val as $key2 => $newVal)
        {
            echo '<pre>';
            var_dump($key2, $newVal);
              //or echo it echo $key2 and echo $newVal
        }
    }

resulted output:

  string(2) "id"
    int(1)

  string(7) "menu_id"
    int(12)

  string(5) "title"
  string(29) "Register Domain name for free"

  string(9) "sub_title"
  string(20) "This is the subtitle"

  string(7) "content"
  string(12) "this content"

  string(11) "description"
  string(11) "description"

  string(7) "section"
  string(4) "sect"
Sign up to request clarification or add additional context in comments.

1 Comment

it worked :-) thanks for your code example that works, why how does $key in the first loop didn't not worked?
0

This is an old question, but I'm answering now for the benefit of new users.

The short answer is that when you retrieve the results of a database query with result_array, you're getting an array of arrays. Each row is it's own array, and the key for that row is NOT the ID column. It is just a sequential number, starting at zero.

For example, if I have a table like this:

+----+-----------+------------+
| id | username  | updated    |
+----+-----------+------------+
| 16 | nhageman  | 2019-10-29 |
| 20 | hnoland   | 2014-02-10 |
| 31 | lpostel   | 2020-04-08 |
| 36 | lemmerich | 2016-03-08 |
| 77 | rhegwood  | 2017-02-20 |
+----+-----------+------------+

...and run an Active Record query like this:

$results = $this->db
  ->get('users')
  ->result_array();

The $results array will look like this:

array (size=10)
  0 => 
    array (size=3)
      'id' => string '16' (length=2)
      'username' => string 'nhageman' (length=8)
      'updated' => string '2019-10-29' (length=19)
  1 => 
    array (size=3)
      'id' => string '20' (length=2)
      'username' => string 'hnoland' (length=7)
      'updated' => string '2014-02-10' (length=19)
  2 => 
    array (size=3)
      'id' => string '31' (length=2)
      'username' => string 'lpostel' (length=7)
      'updated' => string '2020-04-08' (length=19)
  3 => 
    array (size=3)
      'id' => string '36' (length=2)
      'username' => string 'lemmerich' (length=9)
      'updated' => string '2016-03-08' (length=19)
  4 => 
    array (size=3)
      'id' => string '77' (length=2)
      'username' => string 'rhegwood' (length=8)
      'updated' => string '2017-02-20' (length=19)

Here's the point:

Notice that the array key for each row is 0 through 4, NOT the row ID.

So you will not get what you expect when you iterate through your array as you did in your question:

// how to do it wrong
foreach ($results as $key => $row) {
    echo $key . ':';
    echo $row['username'] . '<br>';
}

Wrong results:

0: nhageman
1: hnoland
2: lpostel
3: lemmerich
4: rhegwood

You can see that the $key is just the automatic sequential key assigned to each row. In most cases when dealing with an Active Record result, you can just ignore the key, because the row ID you're looking for is contained in the $row array:

// how to do it right
foreach ($results as $row) {
    echo $row['id'] . ':';
    echo $row['username'] . '<br>';
}

Correct results:

16: nhageman
20: hnoland
31: lpostel
36: lemmerich
77: rhegwood

Hope this clarifies it for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.