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
  •  | 
  •  

문제

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!

도움이 되었습니까?

해결책

$index is the number you are looking for.

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top