(); $ this-&gt; load-&gt; helper(array(‘form’,‘url’)); $这 - &GT;负载&GT;模型( ‘则fetch_data’); } 公共功能agriculture_exam() { $ stream =‘ 农业 </跨度> “; $ data [’考试 - &gt; result_array(); 返回$ result; } }
我是ci的新人。在控制器即Test.php我定义$ stream =‘ 农业 </跨度> ’并将$ stream变量传递给top_agriculture_exams模型。现在,当
请检查我的以下代码。这会对你有所帮助。
<ul class="list"> <?php if ($exam !== NULL): foreach ($exam as $row): echo "<li><a href='#'>" . $row['exam_name'] . "</a></li>"; endforeach; endif; ?> </ul>
另外cna你请把这个代码放在View中并分享结果。
<?php var_dump($exam); ?>
另请查看下面的型号代码。
<?php class Fetch_data extends CI_Model { function __construct() { parent::__construct(); } public function top_agriculture_exams($stream) { $this->db->select('exam_name'); $this->db->from('all_exams_details'); $this->db->where('exam_name',$stream); $this->db->order_by('exam_name'); $this->db->limit('10'); $query = $this->db->get(); $result = $query->result_array(); return $result; } }
而不是使用元素数组访问它尝试使用对象访问它。
更改Fetch_data.php文件中的代码。
//$result = $query->result_array(); $result = $query->result();
并在header.php中按对象访问它。这是下面的一个例子。
<ul class="list"> <?php foreach($exam as $row): ?> <?= "<li><a href='#'>" . $row->exam_name . "</a></li>" ?> <?php endforeach; </ul>
其中exam_name等于数据库中的列。