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!

5
  • Why are you against foreach? Is there any specific, concrete reason? Commented May 15, 2014 at 7:02
  • @DamienPirsy I need an index number. I need the index number for other parts of the code. Commented May 15, 2014 at 7:03
  • @achll Do you mean out of the result set object you want only a particular row? Commented May 15, 2014 at 7:24
  • @ICanHasKittenz I think no. I just want the index of current array result. Commented May 15, 2014 at 7:25
  • @achll Refer each column by index number rather than name? Commented May 15, 2014 at 7:27

2 Answers 2

1

$index is the number you are looking for.

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

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

Comments

0

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

2 Comments

I also read that from here: stackoverflow.com/questions/141108/… but I don't get it how to use $index and $name?
Please explain your answer in brief.

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.