传递数组以在PHP中查看和显示数据

时间:2014-03-25 09:06:55

标签: php codeigniter

 private function displayallsurveys() {
    $this->load->database();
    $sql = "SELECT * from survey";
    $query = $this->db->query($sql);
    $data = array();
    if ($query->num_rows() > 0) {

       $data = $query->result_array();

       return $data;


    } else {
        return $data;
    }
}

传递给视图

    $data = $this->displayallsurveys();
    $this->load->view('SurveyPage', $data);

当我在结尾处展示时

<ul class="nav nav-sidebar">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Add new Survey</a></li>
       <?php  foreach ($data as $row) {
            $SurveyTitle = $row['SurveyTitle'];
            $SurveyId = $row['SurveyId'];


                  echo'<li id=' . $SurveyId . '><a href=' . $SurveyTitle . '>' . $SurveyTitle . '</a><li>';
       }

          ?>

      </ul>

我得到未定义的变量$ data

请有人帮忙

4 个答案:

答案 0 :(得分:3)

如果你想在视图中使用$ data变量,那么你需要将它作为

传递
$this->load->view('SurveyPage', array('data' => $data));

否则,使用以下数组键成为变量名称

$this->load->view('SurveyPage', $data);

答案 1 :(得分:0)

传递这样的数据

$data['data'] = $this->displayallsurveys();
$this->load->view('SurveyPage', $data);

答案 2 :(得分:0)

这样做:

$data['data'] = $this->displayallsurveys();
$this->load->view('SurveyPage', $data);

答案 3 :(得分:0)

作为一种做法,建议使用 $ data [] 变量作为要传递给视图的所有变量的容器。您只需要命名每个

例:
$ data ['survey'] = $ this-&gt; displayallsurveys();
$ this-&gt; load-&gt; view('SurveyPage',$ data);

然后在视图中:

<ul class="nav nav-sidebar">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Add new Survey</a></li>
       <?php  foreach ($survey as $row) {
            $SurveyTitle = $row['SurveyTitle'];
            $SurveyId = $row['SurveyId'];


                  echo'<li id=' . $SurveyId . '><a href=' . $SurveyTitle . '>' . $SurveyTitle . '</a><li>';
       }

          ?>

      </ul>

注意:

foreach ($survey as $row)