简体   繁体   中英

How to remove redundant data in table from SQL query with join in codeigniter

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

VIEW:

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

The table being returned has so much redundant data eg one customer has ordered many different meals. it shows same customer name over and over for his meals which he has ordered. How do i eliminate this and show in the table??

Please help.

try modifying your query like this, I used left join and right join..

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

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