php 将数据库中的数据显示到下拉 CodeIgniter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19922143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:14:41  来源:igfitidea点击:

display data from database to dropdown CodeIgniter

phpmysqlcodeigniter

提问by neknek mouh

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

我在将数据从 db 显示到下拉列表时遇到了困难。

This is what I have tried:

这是我尝试过的:

Model.php

模型.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

    <?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

查看.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.

但结果出现在我的页面顶部。它没有出现在下拉列表中。我在这里做错了什么?非常感谢帮助。谢谢。

回答by dnapierata

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

您不应该从您的角度调用您的模型。而是尝试$data['groups']在加载视图之前调用您的模型和设置。

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>

回答by Jamshid Hashimi

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>

回答by baldvegan

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

Codeigniter 已经具有专门的功能,可以最大限度地减少您必须在代码中转储的 html 量:

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

如果你需要让视图拉出下拉列表中的前一个数据,你可以做一个foreach循环从数据库中获取下拉列表的前一个值,即... $description = $item->description; 并在视图中,将 'set_value('description')' 更改为简单的 '$description'。

回答by Ela Buwa

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.

永远不要从视图中调用模型。这是可行的,但是您再次失去了首先使用 MVC 的意义。从控制器调用模型。获取数据并将数据传递到您的视图中。

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 $groupsvariable and echoto your dropdown.

在您看来,只需循环$groups变量和echo下拉列表即可。

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

回答by laurent Nunenthal

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'));

回答by Harsh Lad

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

}

}