-1
 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;
    }
}

Passing to the View

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

When i show it on the from end

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

I am getting undefined variable $data

Please someone help

4 Answers 4

3

if you wanted to use $data variable in the view then you would need to pass it as

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

Otherwise with the following the array keys becomes the variable names

$this->load->view('SurveyPage', $data);
Sign up to request clarification or add additional context in comments.

Comments

0

Pass data like this

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

Comments

0

Do it like:

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

Comments

0

As a practice, it is recommended to use $data[] variable as a container of all variables you want to pass to a view. All you just have to do is name the key of each.

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

Then in view :

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

Notice :

foreach ($survey as $row)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.