表格数据没有显示在codeigniter中?

时间:2014-03-27 12:19:27

标签: php 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>

我无法访问数组变量请帮助某人我需要一个模型可以在没有它的情况下工作吗?

4 个答案:

答案 0 :(得分:2)

应该是

return $table; //good

而不是

return; $table; //bad

;之后您不需要return,除非您不想返回任何内容。

在您的通话功能中,也可以这样做:

$questionstable['dataquestions'] = $this->displayallquestionsandoptions();
$this->load->view('questions', $questionstable);

您在视图中指的是$dataquestions,但该变量不存在。

此外,在您的foreach中,您最后错过了:

应该是:

foreach($dataquestions as $questionsrow):

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分:0)

我认为您忘记编写一些代码:在公共函数displayallquestionsandoptions() {}方法中使用您的行更改以下行

$table = $query->result_array();

并且在您调用另一个功能线时几乎没有变化......

$data['questionstable'] = $this->displayallquestionsandoptions();
$this->load->view('questions', $data);

答案 3 :(得分:0)

试试这个 首先加载

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

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