我理解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。我不确定是否只需连接两个表并调用数组来匹配orders的oid和orders_content的oid,然后显示相关的数据。
在视图/Productorder.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 $orders不能只来自订单表,对吗?很抱歉,但是是的,我尝试了一下代码点火器,希望能学得更快。同样,MVC让我有点困惑。
编辑一个问题:如何在浏览器上显示可查看的product_order.php?我修改了模型代码-看上去对吗?
编辑II 1)我想限制订单只限于某一品牌。因此,我想知道是否应该让制造商的桌子也加入吗?例如,制造商id =人员名称,然后只向某一品牌显示销售产品的订单。
2) Product_order.php是不可见的--我的意思是,我试图查看页面,看看它是否有效,但什么也没有出现?
好吧,现在,我按照戴姆西的说法编辑了代码
$data[$orders['orders.oid']] = $orders['orders_content.oid'];这一行是错误:致命错误:不能使用stdClass类型的对象作为数组,为什么不能使用对象?
发布于 2011-06-10 10:34:27
正如@Raza已经解释过的,您需要像这样加入to表(左连接)。
$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;
}
return $data;遵循MVC模式,您现在应该在控制器中检索这个数组($data),它处理传递到正确视图的过程。
因此,在您的控制器中,应该有这样的东西:
编辑控制器:好的,根据您的评论,您想要调用product_order视图。你不能直接这么做。视图是根据URL加载的(通常在控制器中,但不是强制的),因此如果您希望在index.php/product_order访问页面,则需要在控制器文件夹中有一个名为product_order.php的文件。如果您没有指定第二个URI段(就像在您的例子中那样),则需要使用默认函数index()。那么,您的控制器应该是这样的:
class Product_order extends CI_Controller {
function index()
{
$this->load->model('orders_model'); // (I'm assuming you're not autoloading it. Otherwise, just skip the loading of this model);
$data['orders'] = $this->orders_model->get();
$this->load->view('product_order',$data);
}
}如果您将来想添加一个方法,比方说要查看某个特定的产品,您可以执行“index.php/ product / view _product/20”,这样您就可以:
function view_product($id) { /....}。希望MVC对你来说越来越清晰了。//End编辑;
现在可以在视图‘_order.php’中收集数组(在CI中,提供给视图的数组的索引在omonimous变量中转换),就像您正在做的那样。但是请注意,您调用它们时就像调用对象一样,而不是提供关联数组。所以应该是这样的:
<?php foreach($orders as $order) : ?>
<tr>
<td><?php echo $order['product_id']?></td>
<td><?php echo $order['title'];?></td>
<td><?php echo $order['price'];?></td>
<td><?php echo $order['quantity'];?></td>
</tr>
<?php endforeach; ?>如果您使用$q->result()而不是$q->result(),那么您的代码就可以了。
发布于 2011-06-10 10:53:43
看起来你的观点有一个微妙的错误:你有:
<?php foreach($orders as $orders){?> <!-- nb. both variables are plural -->这应该是:
<?php foreach($prders as $order){?> <!-- nb. second variable is singular -->同样:
<td><?php echo $orders->product_id;?></td>现在必须:
<td><?php echo $order->product_id;?></td>您以前看到的是在循环中第一次覆盖$orders数组,这样它就不能正常工作。
发布于 2011-06-10 10:14:43
我认为你需要把这两个表连接起来,然后作为一组得到结果。在模型类中尝试这样做:
$this->db->select('*');
$this->db->from('orders');
$this->db->join('orders_content', 'orders.oid = orders_content.oid');
$query = $this->db->get();https://stackoverflow.com/questions/6304892
复制相似问题