简体   繁体   中英

Codeigniter MYSQL join return as single record?

I'm trying to get my head around displaying a nice “My orders” page in an app - the page list's each order in a box, with all the items in that order next to it. It's done by joining three tables (Orders, Items, Products), The items table has a product_id and an order_id etc. A chap on here helped me with the original code, which is below for the model function:

function get_all_orders() {
    $this->db->select('o.*,p.*,i.*,o.order_id AS order_id,p.product_id AS product_id, i.item_id as item_id');
    $this->db->from('Orders o');
    $this->db->join('Items i','o.order_id = i.order_id','left');
    $this->db->join('Products p','p.product_id = i.product_id','left');
    $this->db->order_by('o.order_id', 'desc');   
    $query = $this->db->get();
    return $query->result();                  

}

This returns each row as a separate result, so then i'm using something like this in the view to separate the orders…

$currentParent = false;
foreach ($orders as $o) {
    if ($currentParent != $o->order_id) {
        echo '<h1>ORDER NUMBER ' . $o->order_id . '</h1>';
        $currentParent = $o->order_id;
    }
    echo '<p>' . htmlentities($o->product_name) . '</p>';
}

But now i'm running into problems with trying to use the Codeigniter Pagination function, which needs to know the total number of records (orders) returned so it can calculate the links, but one Order box could be 5 records (1 order record then 4 items) so it doesn't work.

I'm thinking it would be super easy (but bad) to just call the items query from within the view… so i don't want to do that. I'm thinking there must be a way to get the SQL result into a more suitable array structure where each order contains a sub-array with the items… but i've no idea how to do that in MYSQL or how to return it in the correct way in the view.

Bit stuck really - anybody been here/done that before and fancy showing me the error of my ways?

You can use GROUP_CONCAT() which will give you the comma separated all the products related to one particular order but using this before

Be ware of that fact it has a default limit of 1024 characters to concat but it can be increased which is defined in manual

function get_all_orders() {
    $this->db->select('o.*,o.order_id AS order_id,
    GROUP_CONCAT(p.product_name ORDER BY p.id) products,
    GROUP_CONCAT(p.id ORDER BY p.id) product_ids
   ',FALSE);
    $this->db->from('Orders o');
    $this->db->join('Items i','o.order_id = i.order_id','left');
    $this->db->join('Products p','p.product_id = i.product_id','left');
    $this->db->group_by('o.order_id');   
    $this->db->order_by('o.order_id', 'desc');   
    $query = $this->db->get();
    return $query->result();
}

Then from above query you can get you products by using explode function,from above query you can get the desired count of all orders in order to use the CI's pagination class,now in your view you data presentation code would be something like this

foreach ($orders as $o) {        
    echo '<h1>ORDER NUMBER ' . $o->order_id . '</h1>';
    $products=explode(',',$o->products);
    if(!empty($products)){
      foreach ($products as $p) {
            echo '<p>' . htmlentities($p) . '</p>';
      }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM