8

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.

4

6 Answers 6

20

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>
Sign up to request clarification or add additional context in comments.

1 Comment

when you pass $data to your view each key is an accessible variable in your view. Ex: $data['groups'] in your controller corresponds to $groups in your view.
8

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>

Comments

4

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.'

Comments

2

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();
}

Comments

0

Better I think, in your view use:

On your model get all your data in an array with:

public function get_all_description()
{
    $query = $this->db->get('description');
    return $query->result_array();
}

In controller:

$data['description']=$this->model->get_all_description();

In view:

for($i=0;$i<sizeof($description);$i++)
{
    $description2[$description[$i]['description']]=$marque[$i]['description'];
}

echo form_dropdown('description', $description22, set_value('description'));

Comments

0

This is Codeigniter 4 answer.

Controller

public function index()
{
    $delModel = new delivery_model();
    $groups=$delModel->getAllGroups();
    $data = [

        'title' => 'Warehouse - Delivery',
        'groups' => $groups,

    ];
        return view('include/header',$data);
        return view('include/navbar',$data);
        return view('delivery_view', $data);
        return view('include/sidebar',$data);
        return view('include/footer',$data);

}

Model

public function getAllGroups()
{
    $db = \Config\Database::connect();
    $query = $db->query("SELECT description FROM location;");
    return $query->getResultArray();
}

View

<select>
    <?php
      foreach ($groups as $row) {
      echo '<option value="' . $row["description"] . '">' .$row["description"] . '</option>';
      }?>
</select>   

Comments

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.