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
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);
Pass data like this
$data['data'] = $this->displayallsurveys();
$this->load->view('SurveyPage', $data);
Do it like:
$data['data'] = $this->displayallsurveys();
$this->load->view('SurveyPage', $data);
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)