【发布时间】:2014-07-23 09:11:45
【问题描述】:
在使用 codeigniter 时,我整理了一件我也可以从视图页面调用模型函数的东西。
举个例子
这是我的示例模型
<?php
class autoload_model extends CI_Model{
function __construct() {
parent::__construct();
}
/*---------data fetching-----------*/
function get_data_from_table($table,$data,$cond)
{
$this->db->select($data);
$this->db->where($cond);
$result= $this->db->get($table);
return $result;
}
/*---------ends-----------*/
}
?>
现在在我的视图页面中我已经写了这个
<table cellspacing="1" cellpadding="0">
<tr>
<td>Product Title</td>
<td><Product Price</td>
</tr>
<?php
$product_list = $this->autoload_model->get_data_from_table("td_product","*",
"product_id > 0")->result_array();
if(count($product_list)>0)
{
foreach($product_list as $pl)
{?>
<tr>
<td><?php echo $pl['product_title'];?></td>
<td><?php echo $pl['product_price'];?></td>
</tr>
<?php
}
}
else
{?>
<tr>
<td colspan="2">No data Found</td>
</tr>
<?php
}
}?>
</table>
一切都很好,只是我想知道以这种方式使用它是否好用?
NOTE:
自动加载模型在 config/autoload.php 文件中自动加载
【问题讨论】:
-
这是可能的,但你不应该这样做你可以把它放在控制器中并将数组传递给视图
标签: codeigniter