[英]Table data not showing in codeigniter?
控制器功能
public function displayallquestionsandoptions() {
$this->load->database();
$this->load->library('table');
$query = $this->db->query("SELECT QuestionId,Name,SurveyId,CreatedOn from question");
$table = $this->table->generate($query);
return $table; //Added this
}
调用另一个函数:
$questionstable = $this->displayallquestionsandoptions();
$this->load->view('questions', $questionstable);
查看代码:
<table>
<thead>
<th>Question Id</th>
<th>Question Name</th>
<th>Survey Id</th>
<th>Created On</th>
</thead>
<tbody>
<?php foreach ($questionstable as $questionrow) ?>
<tr>
<td><?php $questionrow -> QuestionId;?></td>
<td><?php $questionrow ->Name;?></td>
<td><?php $questionrow ->SurveyId;?></td>
<td><?php $questionrow ->CreatedOn;?></td>
</tr>
<?phpendforeach;?>
</tbody>
</table>
我无法访问数组变量请帮助某人我需要一个模型我可以在没有它的情况下工作吗?
它应该是
return $table; //good
并不是
return; $table; //bad
你不需要;
return
后,除非你想要什么都不返回。
在您的调用函数中,也执行此操作:
$questionstable['dataquestions'] = $this->displayallquestionsandoptions();
$this->load->view('questions', $questionstable);
您在视图中引用了$dataquestions
,但该变量不存在。
而且,在你的foreach
,你错过了:
最后。
它应该是:
foreach($dataquestions as $questionsrow):
试试这个:
在Controller中返回查询对象
public function displayallquestionsandoptions() {
$this->load->database();
$this->load->library('table');
$query = $this->db->query("SELECT QuestionId,Name,SurveyId,CreatedOn from question");
return $query; //Added this
}
在其他功能,尝试喜欢
$data['questionstable'] = $this->displayallquestionsandoptions();
$this->load->view('questions', $data);
并在视图中获取详细信息
<?php foreach ($questionstable->result() as $questionrow) ?>
<tr>
<td><?php echo $questionrow -> QuestionId;?></td>
<td><?php echo $questionrow ->Name;?></td>
<td><?php echo $questionrow ->SurveyId;?></td>
<td><?php echo $questionrow ->CreatedOn;?></td>
</tr>
<?php endforeach;?>
我想你忘了编写一些代码:在公共函数displayallquestionsandoptions() {}
方法中用你的行更改下面的行
$table = $query->result_array();
在你的另一个功能线上调用...
$data['questionstable'] = $this->displayallquestionsandoptions();
$this->load->view('questions', $data);
先尝试这个加载它
$this->load->library('table');
public function displayallquestionsandoptions() {
$this->load->database();
$this->load->library('table');
$query = $this->db->query("SELECT QuestionId,Name,SurveyId,CreatedOn from question");
return $query->result_array();
}
$questionstable = $this->displayallquestionsandoptions();
$this->load->view('questions', $questionstable);
在视图中使用这个
echo $this->table->generate($questionstable);
如果没有更多信息,请访问http://ellislab.com/codeigniter/user-guide/libraries/table.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.