模型
//返回一个包含不同数组的数组,其中包含从数据库中获取的键ID,名称和缩略图
function m_get_thumbnails($category){
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food');
if($query->num_rows()>0)
{
$count =0;
$arr = array();
foreach ($query->result() as $row)
{
$arr[$count++] = array(
'id'=>$row->id,
'name'=>$row->name,
'thumbnail'=>$row->thumbnail
);
}
return $arr;
}
}
控制器中的功能
之后我将结果放在一个数组中并将数组传递给我的视图。
function index_food()
{
$data['cafe'] = $this->mlocus->m_get_thumbnails('cafe');
$this->load->view('food.php',$data);
}
查看
<script>alert(<?php $cafe[1]; ?>);</script>
//this alert is coming blank.......
答案 0 :(得分:0)
在模型
中简化您的功能function m_get_thumbnails($category)
{
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food')->result_array();
return $query;
}
在您的视图中
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Thumbnail</td>
</tr>
<?php for($i=0;$i<count($cafe);$i++){?>
<tr>
<td> <?php echo $cafe[$i]['id']?> </td>
<td> <?php echo $cafe[$i]['name']?> </td>
<td> <?php echo $cafe[$i]['thumbnail']?> </td> <!-- Use <img> if you have a url -->
</tr>
<?php }?>
</table>
答案 1 :(得分:0)
控制器:
function index_food()
{
$data['cafe'] = $this->mlocus->m_get_thumbnails('cafe');
$this->load->view('food',$data);
}
在模型中
function m_get_thumbnails($category)
{
$this->db->select('id,name,thumbnail');
$this->db->where('category',$category);
$query=$this->db->get('food');
$result = $query->result_array();
return $result;
}
在您的视图中
<table>
<tr>
<td>ID</td>
<td>Name</td>
<td>Thumbnail</td>
</tr>
<?php
if(!empty($cafe)) {
foreach($cafe as $c){
?>
<tr>
<td> <?php echo $c['id']?> </td>
<td> <?php echo $c['name']?> </td>
<td><img src="<?php echo $c['thumbnail']?>" /></td>
</tr>
<?php } } else { ?>
<tr><td colspan="3">No record found.</td></tr>
<?php } ?>
</table>