1

Iam new to codeIgniter; my problem is I have database with table cartegories_meta_data

meta_cartegory_id | meta_cartegory_name 
------------------|--------------------
 395              |  Soft Drinks 
 11111            |  Hot Drinks

I have a model Add_items

public function getcartegory()
{
    $query = $this->db->query("SELECT * FROM `cartegories_meta_data`");
    return $query->result();
}

Controller items

public function additems()
{
    $this->load->helper('url');
    $this->load->model('Add_items');
    $data['result'] = $this->Add_items->getcartegory();
    $this->load->view('menu');
    $this->load->view('additems', $data);
    $this->load->view('footer1');
}

And on my view I load array result in for each loop

<?php 
    foreach ($result as $row) {
        $id = $row->meta_cartegory_id;
        $name = $row->meta_cartegory_name;
    } 
?>

Now what I want is to create a drop down list... which will show all cartegory names and their id as value

<?php 
    $options = array($id => $name );
    $more =' class ="form-control"';
    echo form_dropdown('item_cartegory', $options, 11111, $more); 
?>

The current code give me

<option value="11111" selected="selected">Hot Drinks</option>

But what I want is to get something like this

<option value="395">Soft Drinks</option>
<option value="11111" selected="selected">Hot Drinks</option>

Can you help me on how i can deal with the for each loop to get what i want or is there another way

2
  • Non-programming error: it's "category" not "cartegory" Commented Sep 9, 2014 at 8:10
  • Thanks, I have seen that it's because there is another table called category_meta_data Commented Sep 9, 2014 at 8:22

1 Answer 1

1

Just put the option inside loop like this.

<select>

<?php
  $flag=0;
  foreach ($result as $row) { ?>

  if(flag==0)
  {
     //code to select the first item
       <option value="<?php echo $row->meta_cartegory_id; selected="selected"?>" ><?php echo $row->meta_cartegory_name; ?></option>
   flag=1;
   }
   else
   {
     //remove the selected here
       <option value="<?php echo $row->meta_cartegory_id;?>" ><?php echo $row->meta_cartegory_name; ?></option>
    }
  } ?>

</select>

Let me know if there is an error

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

2 Comments

Thanks for reply... that code works , but what if the data in table categories_meta_data are more than 2 rows ( meaning these data are added frequently and I can not predict how many they will be) so I need a loop which will loop and show all values automatically
this code can output one or more rows because it is in loop. What's your current problem now?

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.