当我加载控制器时,我的主页上出现此错误,但是执行搜索后,该错误将消失并且搜索结果将显示在其中。
这是我的查看页面:
<div id="toggle_tst">
<div class="scroll">
<?php
// List up all results.
echo "<table id='table'>";
echo "<tbody>";
echo "<thead>";
echo "<tr><th>NAME</th><th>Pernor No</th><th>DESIGNATION ID</th>
</tr>";
echo "</thead>";
foreach ($results as $row)
{
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['pernor_no'];
echo "</td><td>";
echo $row['designation_id'];
echo "</td></tr>";
}
if (isset($_POST['data_rows']))
{
//if the page has been submitted, append the rows
echo $_POST['data_rows'];
}
echo "</tbody>";
echo "</table>";
?>
</div>
</div>
控制器:
public function execute_search()
{
// Retrieve the posted search term.
$search_term = $this->input->post('search');
$data['query'] = $this->JcMeetingExpense_model->viewother();
$data['query1'] = $this->JcMeetingExpense_model->viewcatprice();
// Use a model to retrieve the results.
$data['results'] = $this->JcMeetingExpense_model-
>get_results($search_term);
// Pass the results to the view.
$this->load->view('JcMeetingExpense/jc_meeting_expense',$data);
}
答案 0 :(得分:3)
在执行循环之前使用isset。
如果在代码中使用变量和数组,则isset是最佳实践。
为您的解决方案:使用以下代码
if(isset($results)){
// all code inside it...
}
答案 1 :(得分:3)
答案 2 :(得分:1)
原因是您尚未设置变量results
。如此简单的解决方案是在foreach循环之前添加一个isset()
。
if(isset($results)){
foreach ($results as $row){
.
.
.
.
}
}