我从db到dropdown的显示数据有困难。
这就是我的尝试:
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>
但结果显示在我的页面顶部。它没有出现在下拉列表中。我在这里做错了什么?非常感谢帮助。感谢。
答案 0 :(得分:18)
您不应该从您的视图中调用您的模型。相反,请在加载视图之前尝试调用模型并设置$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>
答案 1 :(得分: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>
答案 2 :(得分:3)
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-&gt; description ;并在视图中,更改&#39; set_value(&#39;说明&#39;)&#39;简单地说&#39; $描述。&#39;
答案 3 :(得分: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();
}
答案 4 :(得分: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'));
答案 5 :(得分: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>
答案 6 :(得分:-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));
}
}