Welcome Guest, Not a member yet? Register   Sign In
Custom active record for dropdown
#1

[eluser]eggzy[/eluser]
I have a model function for getting categories:

Code:
public function get_cats() {

        $this->db->order_by('title', 'asc');
        $query = $this->db->get('labels');

        if($query->num_rows() > 0) return $query->result();
        else return NULL;
    }

and in controller:

Code:
$categories = $this->posts_m->get_cats();

            foreach ($categories as $category) {
                $options[$category->id] = $category->title;
            }

            $data = array('dropdown' => $options);

and this populates dropdown for choosing category:

Code:
<?php echo form_dropdown('cats', $dropdown, ''); ?>

But when there are no categories in DB it shows errors.
It would be nice to show just empty dropdown.
#2

[eluser]ηυмвєяσηє[/eluser]
Code:
public function get_cats() {

        $this->db->order_by('title', 'asc');
        $query = $this->db->get('labels');

        if($query->num_rows() > 0) return $query->result();
        else return array();  // change this.
    }
#3

[eluser]eggzy[/eluser]
Already tried that I get:
Message: Undefined variable: options - in controller
Message: Invalid argument supplied for foreach() - form helper
#4

[eluser]ηυмвєяσηє[/eluser]
Code:
$categories = $this->posts_m->get_cats();
$options = array();

  if (is_array($categories)) {
    foreach ($categories as $category) {
      $options[$category->id] = $category->title;
    }
  }
$data = array('dropdown' => $options);
#5

[eluser]eggzy[/eluser]
Thanks, that works




Theme © iAndrew 2016 - Forum software by © MyBB