0

I am trying to get the value of how many dishes are in each order of the logged-in user, by adding up the quantity of dishes group by order.

The normal query I run which is a success by assuming logged in user's userid is 35:

SELECT      o.orderid, location, status, pay, total, 
            SUM(quantity) AS dishnum 
FROM        orders o 
JOIN        ordersdish od 
            ON od.orderid = o.orderid 
WHERE       userid = 35 
GROUP BY    o.orderid;

Result:

enter image description here

Codeigniter code I tried to run:

public function getorderself(){
    $this->db->select_sum('quantity');
    $this->db->select('orders.orderid','location','status','pay','total');
    $this->db->from ('orders');
    $this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
    $this->db->where('userid', $_SESSION["userid"]);
    $this->db->GROUP_BY('orders.orderid');
    $query = $this->db->get ();
    return $query;
}

Thank you. Update: I changed a bit my code, it can return data but only the orderid.

Code i used in view:

<?php
    if($orderdata->num_rows() > 0)
    {
        foreach($orderdata->result() as $row)
        {
?>
            <tr>
                <td><?php echo $row->orderid; ?></td>                        
                <td><?php echo $row->location; ?></td>
                <td><?php echo $row->status; ?></td>
                <td><?php echo $row->pay; ?></td>
                <td><?php echo $row->total; ?></td>
                <td><?php echo $row->quantity; ?><td>
                <td><button type="button" href="#" class="delete_data delButton" orderid="<?php echo $row->orderid; ?>">Delete</button></td>
            </tr>
<?php  

        }
    }
    else
    {
?>
        <tr>
            <td colspan="7" align="center">No Data Founds</td>
        </tr>
<?php   
    }
?>

Code I used in maim.php

public function vieworderself()
{

    $this->load->view('header');
    $this->model->caltotal();
    $data["orderdata"]=$this->model->getorderself();  
    $this->load->view('/orders/vieworderself', $data);
    $this->load->view('footer');
}
Jainil
  • 1,488
  • 1
  • 21
  • 26
calvin724
  • 41
  • 5

1 Answers1

0

Try using table name before every column. Eg. orders.location

public function getorderself(){
    $this->db->select_sum('quantity');
    $this->db->select('orders.orderid','orders.location','orders.status','orders.pay','orders.total');
    $this->db->from ('orders');
    $this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
    $this->db->where('orders.userid', $_SESSION["userid"]);
    $this->db->GROUP_BY('orders.orderid');
    $query = $this->db->get ();
    return $query;
}
Udit
  • 129
  • 2
  • 6