Map data from database to CodeIgniter drop-down list

I'm having difficulty displaying data from db to a drop down list.

Here is what I 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 does not appear in the drop-down list. What am I doing wrong here? Help is much appreciated. Thanks.

+6
source share
5 answers

You should not call your model from your view. Instead, try invoking the model and setting $data['groups'] before loading your views.

Also, do not repeat the results of the string in your model if you do not want it to appear 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> 
+14
source

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> 
+5
source

Never call a model from a view. This is doable, but you again lose the point of using MVC. Call the model from your controller. Get the data and transfer the data to your presentation.

Use as shown 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 swipe around the $groups and echo variable to the drop down list.

 <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 should be

 function getAllGroups(){ $query = $this->db->get('location'); return $query->result_array(); } 
+2
source

Codeigniter already has specialized features that minimize the amount of html that you should 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 want the view to draw the previous data in the drop-down list, you can do a foreach loop to get the previous value of the drop-down list from the database, that is ... $ description = $ item-> description; and in the view, change "set_value (" description ") to just" description ".

+2
source

Better I think, in your opinion, use:

In your model, all your data is obtained in an array with:

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

In the controller:

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

In sight:

 for($i=0;$i<sizeof($description);$i++) { $description2[$description[$i]['description']]=$marque[$i]['description']; } echo form_dropdown('description', $description22, set_value('description')); 
0
source

Source: https://habr.com/ru/post/957881/


All Articles