PHP getting sql result by using for loop (or index number) and not foreach in controller codeigniter

StackOverflow https://stackoverflow.com/questions/23671342

  •  23-07-2023
  •  | 
  •  

Pergunta

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!

Foi útil?

Solução

$index is the number you are looking for.

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top