Jess McKenzie
Jess McKenzie

Reputation: 8385

array of data in a drop down menu

I am trying to get the area table of my database to display in a drop down menu but I am having trouble because it is an array.

In my controller I have: $data['city'] = $this->location->fetchCity();

but in my view when I go <?php echo $city; ?> I get this Array why?

Model:

public function fetchCity(){
    $this->db->select('area');
    $this->db->from('suburbs');

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

    if($query->num_rows() > 0)
    {
        $row = $query->result();
    }

Update:

    <select>
        <?php foreach ($city as $key => $row): ?>

    <option value="<?php echo $row['area'];?>"><?php echo $row['area'];?></option>

        <?php endforeach; ?>
</select>

Upvotes: 1

Views: 632

Answers (1)

okay_google
okay_google

Reputation: 349

<?php echo $city[$key]; ?>

OR

<?php echo $city['area']; ?>

Since your function call returns an Array, this is how I suggest you print it out.


foreach ($city as $key=>$row)
{ 
   echo $row->area; 
}

Since your result is an Array of Objects, I'm sure this will get things right.

Upvotes: 1

Related Questions