表数据在编码器中没有显示


Table data not showing in codeigniter?

CONTROLLER FUNCTION

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

试试这个首先加载这个

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

no foreach需要更多的信息检查这个http://ellislab.com/codeigniter/user-guide/libraries/table.html

试试这个:

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