我明白MVC是做什么的。但不够深入。代码点火器 - 我越想明白,我越陷入困惑
<?php
class Orders_model extends CI_Model {
function __construct(){
parent::__construct();
}
private $table_name='orders';
public function get(){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('orders_content', 'orders.oid = orders_content.oid');
$query = $this->db->get();
$data=array();
foreach($q->result_array() as $orders){
$data[$orders['orders.oid']] = $orders['orders_content.oid'];
}
return $data;
}
}
?>
问题是我有两个表:orders和orders_content。我不知道如果我可以只加入两个表并调用数组来匹配,如果订单的oid和orders_content的oid是相同的,然后显示相关的数据。
在视图/ product_order.php
<table cellpadding="3" cellspacing="0" width="686">
<tr>
<th>Product ID</th>
<th>Title</th>
<th>Price</th>
<th>Qty</th>
</tr>
<?php foreach($orders as $orders){?>
<tr>
<td><?php echo $orders->product_id;?></td>
<td><?php echo $orders->title;?></td>
<td><?php echo $orders->price;?></td>
<td><?php echo $orders->quantity;?></td>
</tr>
<?php }?>
</table>
不知如何显示我有orders.php上视图的命令。因为foreach $命令不能从唯一的订单表正确吗?我很抱歉,但是,我尝试了一下希望快速学习的代码点火器。 MVC再次让我感到困惑。
编辑 一个问题:如何显示product_order.php在浏览器上可见? 我更改了模型代码 - 它看起来是否正确?
EDIT II 1)我正在寻求限制订单只有某些品牌。所以我想知道是否可以建立制造商表格连接?例如,制造商id = manfacturer名称,然后仅向某个品牌显示已售出产品的订单。
2)Product_order.php是不可见的 - 我的意思是我尝试了方法来查看页面,看它是否有效,但没有出现?
3)确定现在,我根据Daimsy
$data[$orders['orders.oid']] = $orders['orders_content.oid'];
这行编辑的代码是一个错误:致命错误:无法使用类型stdClass的的对象作为阵列 为什么不能使用对象?
感谢您快速回答..但我们不需要将两个表格声明为私有? – joe 2011-06-10 10:19:49
不,在codeigniter中,它可以从config文件夹中的database.php文件中找到它(我假设你已经配置了它) – 2011-06-10 10:30:16
不,我还没配置它。从我所看到的,我们必须在某处宣布私人。我会弄清楚。感谢您的帮助。 – joe 2011-06-10 10:32:59