0

I have a query that goes like this:

function getnames(){
        $sql = "select * from names where status = '1'";

        $query = $this->db->query($sql);

        if($query->num_rows()>0){
            return $query->result();
        }
        else{
            return null;
        }
    }

This function is already loaded in the new_model.php. I am using CodeIgniter.

Then in the controller, I use a function that returns the query result. I already have this in the controller:

    function getnames(){
    return $this->new_model->getnames();
} 

What I want to do is that instead of using foreach() loop in getting the array result, I want to use something else that will let me use an index number. How can I do this? Please help. Thanks!

achll
  • 153
  • 1
  • 4
  • 12

2 Answers2

1

$index is the number you are looking for.

$name is the value of the $names[$index]

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

How about using foreach ($names as $index => $name) ?

Marian
  • 1,154
  • 2
  • 15
  • 25