开发者

Code Igniter - the more I try to understand, the more I get confused

开发者 https://www.devze.com 2023-03-12 03:57 出处:网络
I understand what MVC does. but not understand deep enough. <?php class Orders_model extends CI_Model {

I understand what MVC does. but not understand deep enough.

 <?php
class Orders_model extends CI_Model {

         function  __construct(){

                parent::__construct();
        }
        private $table_name='orders';

         public function get(){


         开发者_StackOverflow中文版       $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;
        }


}

?>

the problem is that I have two tables: orders and orders_content. I am not sure if I can just join two tables and call the array to match if orders' oid and orders_content's oid is same and then show the data that are related.

In views/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>

I wonder how to display the orders on view which I have orders.php. because foreach $orders cant be from only orders table right? I am sorry but yes, I took a plunge into code igniter hoping to learn fast. Again MVC confuses me a little.

EDIT One question: how to show the product_order.php to be viewable on browser? I changed model code - does it look right?

EDIT II 1) I m looking to limit orders to only certain brand. So I am wondering if it is advisable to have manufacturer table join as well? For eg, manufacturer id = manfacturer name and then display orders of sold products only to certain brand.

2) Product_order.php is not viewable - I mean I tried ways to view the page to see if it works, but nothing comes up?

3) ok now, i edited the code according to Daimsy

    $data[$orders['orders.oid']] = $orders['orders_content.oid'];

This line is a error: Fatal error: Cannot use object of type stdClass as array Why can't use object?


It looks like your view has a subtle mistake: You have:

<?php foreach($orders as $orders){?> <!-- nb. both variables are plural -->

which should be:

<?php foreach($prders as $order){?> <!-- nb. second variable is singular -->

Similarly:

<td><?php echo $orders->product_id;?></td>

will now have to be:

<td><?php echo $order->product_id;?></td>

What you had before was overwriting the $orders array the first time around the loop so it wouldn't function properly.


As already explained by @Raza Gill, you need to join the to tables (LEFT JOIN) like that

$this->db->select('*');
$this->db->from('orders');
$this->db->join('orders_content', 'orders.oid = orders_content.oid');
$query = $this->db->get();

In your model, you need than to collect the data to be passed to the view. You're not doing that right, cause you're making an assignemnt which has little sense (you're putting there a join clause?)

It should be like this:

$data =array();
foreach($q->result_array() as $orders)
{
   $data[] = $orders;
}

return $data;

Following the MVC pattern, you should now retrieve this array ($data) in your CONTROLLER, which handles then the passing to the right view.

So, in your controller, you should have something like this:

Edited controller: Ok, based on your comment you want to call that product_order view. You cannot do that directly. Views are loaded (usually in controllers, but not mandatory) according to the URL, so if you want the page to be accessed at index.php/product_order you need to have a file called product_order.php in your controllers folder. If you don't specify the 2nd URI segment (like in your case), you need to use the default function index(). Your controller would look like this then:

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);
     }

}

In case you, in future, want to add a method, say to view a specific product, you can do `index.php/product_order/view_product/20' for example, so you'll have:

  • product_order : --> the controller (product_order.php)
  • view_product : --> the method inside the controller AND
  • 20 : --> the ID of your selected product, that has to be passed to the method above. Like function view_product($id) { /....}. Hope MVC is getting clearer for you.

//End edit;

You can now, in your view 'product_order.php', collect the array (in CI, the indexes of the array provided to the view are transformed in the omonimous variable) with a foreach, like you're doing. But beware, you're calling them as if were objects, while instead you provided an associative array. SO it should be like this:

 <?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; ?>

You're code would have been fine if you'd used $q->result() instead of $q->result_array()


I think you need to join the two tables and get the result as one set. Try to do this in your model class:

$this->db->select('*');
$this->db->from('orders');
$this->db->join('orders_content', 'orders.oid = orders_content.oid');
$query = $this->db->get();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号