我是一个非常新的程序员,所以如果您发现我的问题很愚蠢,请原谅我。
我正在使用Codeigniter,并且在Projects模型中有一种方法get_projects()
从项目表中读取数据。这是代码。
public function get_projects ($userid = '')
{
if (empty($userid))
{
return FALSE;
}
$return_data = array();
$this->db->select('id, startdate, projectname, expenddate, amount, advance, dues');
$this->db->where('userid', $userid);
$query = $this->db->get('projects');
if($query->row())
$return_data = $query->row();
return $return_data;
}
我有一个控制器函数get_projects()
,在其中我试图调用此模型方法并将数据传递给View文件。这是代码。
public function get_projects()
{
$userid = $this->session->userdata('user_id');
$projects = $this->projects->get_projects($userid);
$data = array(
'projectname' => '',
'startdate' => ''
);
$this->load->view('dashboard', $data);
}
我有一个名为dashboard.php
的视图文件,想在那里显示。我正在更新我的问题以显示视图文件。
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<section>
<div class="row topspace">
<div class="col-6 offset-3">
<h4 class="text-center">Dashboard Testing</h4><hr>
</div>
</div>
<div class="row">
<?php foreach ($result as $row) { ?>
<div class="col-12">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title"><?php echo $row->projectname; ?></h5>
<h6 class="card-subtitle mb-2 text-muted"><?php echo $row->startdate; ?></h6>
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
</div>
</div>
</div>
<?php } ?>
</div>
</section>
我该怎么做?从控制器向视图文件发送数据的正确方法是什么,以及在视图文件中访问该数据的正确方法是什么? 请帮我弄清楚我最近5个小时的尝试。提前谢谢。
答案 0 :(得分:1)
您必须先加载模型:
$mymodel = $this->load->model('namemodel')
然后访问您的模型功能:
$result = $mymodel->get_projects(useridgohere)
然后您的$result
变量就会出现。
要阅读,您可以将变量数据放入结果中
$data = array(
'thenameofvariableinview' => $result,
);
$this->load->view('dashboard', $data);
然后在视图中可以使用以下方法进行调用:
<?php
echo $thenameofvariableinview;
?>
答案 1 :(得分:1)
首先,请确保要获取多少行。
row()
用于一个记录,result()
用于多个记录
模型
public function get_projects ($userid = '')
{
if (empty($userid)){
return FALSE;
}
$this->db->select('id, startdate, projectname, expenddate, amount, advance, dues');
$this->db->where('userid', $userid);
$query = $this->db->get('projects');
//Another way is to count rows and then use row() or result()
//$query->num_rows() will tell you how many rows you are getting
if($query->num_rows() == 1){
return $query->row();
}else{
return $query->result();
}
}
控制器
public function get_projects(){
$userid = $this->session->userdata('user_id');
$projects = $this->projects->get_projects($userid);
//Here pass `$projects` data to array and give it to view
$data = array(
'projectname' => $projects,
'startdate' => 'your-date'
);
$this->load->view('dashboard', $data);
}
现在在视图中显示数据。如果行多于一个,请使用foreach()
循环,否则不循环
答案 2 :(得分:0)
型号:
public function get_projects ($userid = '')
{
if (!empty($userid))
{
$this->db->select('id, startdate, projectname, expenddate, amount, advance, dues');
$this->db->where('userid', $userid);
$query = $this->db->get('projects');
if($query->num_rows() > 0){
return $query->row();
//return $query->result();//according to your requirment like loop
}else{
return array();//empty array
}
}else{
return array();//empty array
}
}
控制器:
public function get_projects(){
$data = array()
$this->load->model('Project_model');//assums not loaded in autoload
$userid = $this->session->userdata('user_id');
$results = $this->Project_model->get_projects($userid);//always add _model to avoid confusion
//$results->startdate = '';//you can do this too
$data['result'] = $results;
$data['startdate'] = '';//dont forget to add date
$this->load->view('dashboard', $data);
}
查看:
<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<section>
<div class="row topspace">
<div class="col-6 offset-3">
<h4 class="text-center">Dashboard Testing</h4><hr>
</div>
</div>
<div class="row">
<?php if (!empty($result) { ?>
<?php foreach ($result as $row) { ?>
<div class="col-12">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title"><?php echo $row->projectname; ?></h5>
<h6 class="card-subtitle mb-2 text-muted"><?php echo $startdate; ?></h6>
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
</div>
</div>
</div>
<?php } ?>
<?php }else{ ?>
<div class="col-12">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">No data found.</h5>
</div>
</div>
</div>
<?php } ?>
</div>
</section>