display data from database to dropdown CodeIgniter

后端 未结 6 1420
星月不相逢
星月不相逢 2020-12-03 08:54

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

This is what I have tried:

Model.php

        public funct         


        
相关标签:
6条回答
  • 2020-12-03 09:25

    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();
    }
    
    0 讨论(0)
  • 2020-12-03 09:27

    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>
    
    0 讨论(0)
  • 2020-12-03 09:35

    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'));
    
    0 讨论(0)
  • 2020-12-03 09:41

    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>
    
    0 讨论(0)
  • 2020-12-03 09:44

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

    0 讨论(0)
  • 2020-12-03 09:44

    public function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('trip_model');
    }
    
    public function index(){
        $data['trips']=$this->trip_model->get_all_trips();
        $this->load->view("trip_view",$data);
    }
    
    public function trip_add(){
        if(!empty($_FILES['trip_image']['name'])){
            $config['upload_path'] = 'assests/images/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = 2048;
            $config['file_name'] = $_FILES['trip_image']['name'];
    
            $this->load->library('upload',$config);
            $this->upload->initialize($config);
    
            if($this->upload->do_upload('trip_image')){
                $uploadData = $this->upload->data();
                $trip_image = $uploadData['file_name'];
            }
            else{
                $trip_image = 'Hello..!!';
            }
        }
        else{
            $trip_image = 'Byeee..!!';
        }
    
        $data = array(
            'trip_image' => $trip_image,
            'name' => $this->input->post('name'),
            'location' => $this->input->post('location'),
            'trip_datetime' => $this->input->post('trip_datetime')
        );
    
        $insert = $this->trip_model->trip_add($data);
        echo json_encode(array("status" => TRUE));
    }
    
    function do_upload(){
        $url = "assests/images/";
        $image = basename($_FILES['trip_image']['name']);
        $image = str_replace(' ','|',$image);
        $type = explode(".",$image);
        $type = $type[count($type)-1];
    
        if (in_array($type,array('jpg','jpeg','png','gif'))){
            $tmppath="assests/images/".uniqid(rand()).".".$type;
            if(is_uploaded_file($_FILES["trip_image"]["tmp_name"])){
                move_uploaded_file($_FILES['trip_image']['tmp_name'],$tmppath);
                return $tmppath;
            }
        }
    }
    public function ajax_edit($id){
        $data = $this->trip_model->get_by_id($id);
        echo json_encode($data);
    }
    
    public function trip_update(){
        if(!empty($_FILES['trip_image']['name'])){
            $config['upload_path'] = 'assests/images/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['file_name'] = $_FILES['trip_image']['name'];
    
            $this->load->library('upload',$config);
            $this->upload->initialize($config);
    
            if($this->upload->upload_img('trip_image')){
                $uploadData = $this->upload->data();
                $trip_image = $uploadData['file_name'];
            }
            else{
                $trip_image = 'Hello..!!';
            }
        }
        else{
            $trip_image = 'Byeee..!!';
        }
    
        $data = array(
            'trip_image' => $trip_image,
            'name' => $this->input->post('name'),
            'location' => $this->input->post('location'),
            'trip_datetime' => $this->input->post('trip_datetime')
        );
    
        $this->trip_model->trip_update(array('trip_id' => $this->input->post('trip_id')), $data);
        echo json_encode(array("status" => TRUE));
    }
    
    public function trip_delete($id){
        $this->trip_model->delete_by_id($id);
        echo json_encode(array("status" => TRUE));
    }
    

    }

    0 讨论(0)
提交回复
热议问题