8

我在将数据库中的数据显示到下拉列表时遇到了困难。

这是我尝试过的:

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

控制器.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();
        }


    }

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

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

4

7 回答 7

20

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

也不要在模型中回显行结果,除非您希望它显示在页面上。

控制器:

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

    }


}

模型:

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

看法:

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

            foreach($groups as $row)
            { 
              echo '<option value="'.$row->description.'">'.$row->description.'</option>';
            }
            ?>
            </select>
于 2013-11-12T06:16:19.597 回答
8

这是你应该做的:

型号

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

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

控制器

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

查看

<select class="form-control">
    <?php foreach($groups as $each){ ?>
        <option value="<?php echo $each->description; ?>"><?php echo $each->description; ?></option>';
    <?php } ?>
</select>
于 2013-11-12T06:27:29.740 回答
4

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

模型

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

控制器

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

看法

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

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

于 2017-11-02T23:04:34.463 回答
2

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

像下面这样使用。

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

在您看来,只需围绕$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>

你的模型的功能应该是,

function getAllGroups(){
   $query = $this->db->get('location');
    return $query->result_array();
}
于 2013-11-12T06:24:07.587 回答
0

我认为更好的是,在您看来,使用:

在您的模型上,将所有数据放入数组中:

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

在控制器中:

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

鉴于:

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

echo form_dropdown('description', $description22, set_value('description'));
于 2016-04-30T15:29:25.413 回答
0

这是 Codeigniter 4 的答案。

控制器

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

}

模型

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

看法

<select>
    <?php
      foreach ($groups as $row) {
      echo '<option value="' . $row["description"] . '">' .$row["description"] . '</option>';
      }?>
</select>   
于 2021-07-17T14:11:51.717 回答
-1

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

}

于 2018-10-04T08:46:35.860 回答