如何通过在codeigniter中加入来从SQL查询中删除表中的冗余数据

时间:2015-02-27 01:13:46

标签: mysql database codeigniter join

CONTROLLER:

class adminpanel extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('login_database');
    }

    public function index() {
        $data['h'] = $this->login_database->load_orders();  
        $this->load->view('admin_page', $data);
    }

}

MODEL:

public function load_orders(){
    $this->db->select('*');    
    $this->db->from('orders');
    $this->db->join('order_detail', 'orders.serial = order_detail.orderid');
    $this->db->join('customers', 'orders.customerid = customers.serial');
    $this->db->order_by('date','DESC');
    $query = $this->db->get();
    $result = $query->result();
        return $result;     
    }
}

查看:

<table border="1" cellpadding="5px" cellspacing="1px" bgcolor="#FFFFFF">  
      <tbody>  
         <tr>  
            <td>Date</td>
            <td>Delivery Time</td> 
            <td>Meal Name</td>
            <td>Quantity</td>  
            <td>Customer Name</td>
            <td>Phone</td>
            <td>Location</td> 
            <td>Address</td>
            <td>Payment Method</td>
         </tr>  
         <?php  foreach ($h as $row)  { ?>
            <tr>  
               <td><?php echo $row->date;?></td>
               <td><?php echo $row->time;?></td>
               <td><?php echo $row->prodname;?></td>   
               <td><?php echo $row->quantity;?></td>
               <td><?php echo $row->name;?></td>
               <td><?php echo $row->phone;?></td>
               <td><?php echo $row->location;?></td>
               <td><?php echo $row->address;?></td>
               <td><?php echo $row->payment;?></td>
            </tr>  
         <?php } ?>  
      </tbody>  
   </table>  

返回的表有如此多的冗余数据,例如一位顾客订购了许多不同的饭菜。它为他所订购的饭菜一遍又一遍地显示相同的客户名称。我如何消除这种情况并在表格中显示?

请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试像这样修改你的查询,我使用左连接和右连接..

$this->db->select('*');    
$this->db->from('orders');
$this->db->join('order_detail', 'orders.serial = order_detail.orderid', 'left');
$this->db->join('customers', 'orders.customerid = customers.serial', 'right');
$this->db->order_by('date','DESC');
$query = $this->db->get();
$result = $query->result();
return $result;