Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display data from database to dropdown CodeIgniter

I'm having difficulty with display data from the db to dropdown.

This is what I have tried:

Model.php

        public function __construct()
        {
            parent::__construct();
        }

        function getAllGroups()
        {
            /*
            $query = $this->db->get('location');

            foreach ($query->result() as $row)
            {
                echo $row->description;
            }*/

            $query = $this->db->query('SELECT description FROM location');

            foreach ($query->result() as $row)
            {
                echo $row->description;
            }

            //echo 'Total Results: ' . $query->num_rows();
        }

Controller.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Delivery_controller extends CI_Controller{
        public function __construct()
        {
            parent::__construct();
            $this->load->model('delivery_model');

        }
        public function index()
        {

            $data['title']= 'Warehouse - Delivery';
            $this->load->view('include/header',$data);
            $this->load->view('include/navbar',$data);
            $this->load->view('delivery_view', $data);
            $this->load->view('include/sidebar',$data);
            $this->load->view('include/footer',$data);
        $data['groups'] = $this->delivery_model->getAllGroups();
        }


    }

View.php

           <select class="form-control">
                <?php 
                        $data = $this->delivery_model->getAllGroups();
                foreach($description as $each)
                { ?><option value="<?php echo $each['description']; ?>"><?php echo $each['description']; ?></option>';
                <?php }
                ?>
                </select>

But the results appear on top of my page. It's not appearing on the dropdown list. What am I doing wrong in here? Help is pretty much appreciated. Thanks.

like image 747
neknek mouh Avatar asked Nov 12 '13 06:11

neknek mouh


People also ask

How fetch data from database in dropdown in CodeIgniter?

To populate form dropdown in CodeIgniter we are going to use one of the form helper function i.e. form_dropdown() from helper file that will create drop-down field. Also, one can use it to create multi select by passing an array of multiple items.


4 Answers

You should not be calling your model from your view. Instead try calling you model and setting $data['groups'] before you load your views.

Also do not echo the row results in your model unless you want it displayed on your page.

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('delivery_model');

    }
    public function index()
    {

        $data['title']= 'Warehouse - Delivery';
        $data['groups'] = $this->delivery_model->getAllGroups();
        $this->load->view('include/header',$data);
        $this->load->view('include/navbar',$data);
        $this->load->view('delivery_view', $data);
        $this->load->view('include/sidebar',$data);
        $this->load->view('include/footer',$data);

    }


}

Model:

    public function __construct()
    {
        parent::__construct();
    }

    function getAllGroups()
    {
        /*
        $query = $this->db->get('location');

        foreach ($query->result() as $row)
        {
            echo $row->description;
        }*/

        $query = $this->db->query('SELECT description FROM location');


        return $query->result();

        //echo 'Total Results: ' . $query->num_rows();
    }

View:

       <select class="form-control">
            <?php 

            foreach($groups as $row)
            { 
              echo '<option value="'.$row->description.'">'.$row->description.'</option>';
            }
            ?>
            </select>
like image 61
dnapierata Avatar answered Oct 22 '22 13:10

dnapierata


This is what you should do:

Model:

public function __construct()
{
    parent::__construct();
}

function getAllGroups()
{
    $query = $this->db->query('SELECT description FROM location');
    return $this->db->query($query)->result();
}

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('delivery_model');
    }
    public function index()
    {
        $data['title']= 'Warehouse - Delivery';
        $data['groups'] = $this->delivery_model->getAllGroups();
        //I take here a sample view, you can put more view pages here
        $this->load->view('include/header',$data);
    }
}

View:

<select class="form-control">
    <?php foreach($groups as $each){ ?>
        <option value="<?php echo $each->description; ?>"><?php echo $each->description; ?></option>';
    <?php } ?>
</select>
like image 38
Jamshid Hashimi Avatar answered Oct 22 '22 13:10

Jamshid Hashimi


Codeigniter already has specialized functions that minimize the amount of html that you have to dump in your code:

Model

public function description_pulldown(){
    $this->db->from('location');
    $query = $this->db->get();
    foreach($query->result() as $row ){
        //this sets the key to equal the value so that
        //the pulldown array lists the same for each
        $array[$row->description] = $row->description;
    }
    return $array;
}

Controller

public function index(){
    $data['description_list'] = $this->delivery_model->description_pulldown();
    //load all of your view data
    $this->load->view('delivery_view', $data);
}

View

echo form_label("Description");
echo form_dropdown('description', $description_list, set_value('description'), $description_list);

If you need to have the view pull up the previous data in the dropdown list, you can do a foreach loop to obtain the previous value of the dropdown from the database ie... $description = $item->description; and in the view, change the 'set_value('description')' to simply '$description.'

like image 30
baldvegan Avatar answered Oct 22 '22 14:10

baldvegan


Never call a model from a view. It is doable but the again you lose the point of using an MVC in the first place. Call the model from your controller. Get the data and pass the data in to your view.

Use like below.

public function index(){
    $data['title']= 'Warehouse - Delivery';
    $data['groups'] = $this->delivery_model->getAllGroups();
    $this->load->view('include/header',$data);
    $this->load->view('include/navbar',$data);
    $this->load->view('delivery_view', $data);
    $this->load->view('include/sidebar',$data);
    $this->load->view('include/footer',$data);
}

In your view, simply loop around the $groups variable and echo to your dropdown.

<select class="form-control">
<?php 
$i = 0;
while($i < count($groups)){
  $val= $groups[$i]['value'];
  $des = $groups[$i]['description'];
  echo "<option value='$i'>$des</option>";
}
</select>

And your model's function should be,

function getAllGroups(){
   $query = $this->db->get('location');
    return $query->result_array();
}
like image 31
Ela Buwa Avatar answered Oct 22 '22 14:10

Ela Buwa