เนื้อหาต่อไปนี้เป็นแนวทางการประยุกต์การแสดงข้อมูล โดยทำการแทรกส่วนการแสดง
ผลรวมข้อมูลของแต่ละชุดข้อมูล ในที่นี้จะอ้างอิงจากวันที่ สามารถนำไปประยุกต์เป็นกรณี
อื่นๆ ได้
คำสั่ง sql ตารางสำหรับทดสอบ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | -- -- Table structure for table `tbl_sale` -- CREATE TABLE IF NOT EXISTS `tbl_sale` ( `sale_id` int (11) NOT NULL , `sale_date` date NOT NULL , `sale_name` varchar (50) NOT NULL , `sale_product` varchar (50) NOT NULL , `sale_price` int (11) NOT NULL , `sale_com` int (11) NOT NULL ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; -- -- Dumping data for table `tbl_sale` -- INSERT INTO `tbl_sale` (`sale_id`, `sale_date`, `sale_name`, `sale_product`, `sale_price`, `sale_com`) VALUES (1, '2015-07-01' , 'Seller A' , 'Product A' , 300, 60), (2, '2015-07-01' , 'Seller A' , 'Product B' , 450, 90), (3, '2015-07-01' , 'Seller B' , 'Product B' , 450, 90), (4, '2015-07-02' , 'Seller A' , 'Product C' , 550, 110), (5, '2015-07-02' , 'Seller B' , 'Product E' , 550, 110), (6, '2015-07-02' , 'Seller B' , 'Product F' , 700, 140), (7, '2015-07-03' , 'Seller A' , 'Product G' , 400, 80); -- -- Indexes for dumped tables -- -- -- Indexes for table `tbl_sale` -- ALTER TABLE `tbl_sale` ADD PRIMARY KEY (`sale_id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tbl_sale` -- ALTER TABLE `tbl_sale` MODIFY `sale_id` int (11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8; |
ตารางสำหรับทดสอบ
Date | Product | Price | Com. |
---|---|---|---|
2015-07-01 | Product A | 300 | 60 |
2015-07-01 | Product B | 450 | 90 |
2015-07-01 | Product B | 450 | 90 |
2015-07-02 | Product C | 550 | 110 |
2015-07-02 | Product E | 550 | 110 |
2015-07-02 | Product F | 700 | 140 |
2015-07-03 | Product G | 400 | 80 |
จากตัวอย่างตาราง จะเป็นตาราง tbl_sale แสดงรายการขายของ สินค้า แสดง
ชื่อสินค้า ราคา และ คอมมิชชั่น 2% ของราคาขาย
สิ่งที่เราต้องการคือ ต้องการแสดงผลรวมราคา หรือยอดขายในแต่ละวัน และค่าคอมมิชชั่นในแต่ละวัน
รวมทั้งผลรวมสะสมของทั้งสองรายการในแต่ละวันด้วย
ผลที่ได้
Date | Product | Price | Com. |
---|---|---|---|
2015-07-01 | Product A | 300 | 60 |
Product B | 450 | 90 | |
Product B | 450 | 90 | |
รวมรายวัน | 1200 | 240 | |
รวมสะสม | 1200 | 240 | |
2015-07-02 | Product C | 550 | 110 |
Product E | 550 | 110 | |
Product F | 700 | 140 | |
รวมรายวัน | 1800 | 360 | |
รวมสะสม | 3000 | 600 | |
2015-07-03 | Product G | 400 | 80 |
รวมรายวัน | 400 | 80 | |
รวมสะสม | 3400 | 680 |
ต่อไปจะเป็นกรณีการใช้งานกับฐานข้อมูล
โดยเราจะใช้วิธีการเชื่อมต่อแบบ
mysqli แทน mysql ธรรมดา เพื่อรองรับในอนาคต
ดาวน์โหลดไฟล์ พร้อมใช้ ชื่อ db_connect.php ได้ที่
โค้ดตัวอย่างทั้งหมด คำอธิบายแสดงในโค้ด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | <?php include ( "db_connect.php" ); // เรียกใช้ไฟล์ ตั้งค่า และฟังก์ชั่น เกี่ยวกับฐานข้อมูล $mysqli = connect(); // สร้าง ตัวแปร mysqli instance สำหรับเรียกใช้งานฐานข้อมูล ?> <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>Document</title> <link rel= "stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" > </head> <body> <br> <br> <br> <div style= "margin:auto;width:80%;" > <br> <br> <table class = "table table-bordered table-condensed" style= "width:550px;" > <thead> <tr class = "bg-success" > <th> Date </th> <th>Product</th> <th>Price</th> <th>Com.</th> </tr> </thead> <tbody> <?php // ส่วนของการกำนหดแสดงการแบ่ง ส่วนของข้อมูลของวันที่ที่ต่างกัน $temp_data1 =null; $temp_data2 =null; $data_show =1; // 1 แสดง 0 ไม่แสดง // กำหนดสำหรับอ้างอิง key ของตัวแปร $i =1; // ส่วนกำหนดตัวแปร สำหรับเก็บค่าวันที่ และเปรียบเทียบ $arr_dateCheck =[]; // ส่วนกำหนดตัวแปรสำหรับเก็บค่าข้อมูลสะสมในแต่ละ คอลัมน์ที่ต้องการ กำหนดเป็น array $aggre_price =[]; $aggre_com =[]; // ส่วนกำหนดตัวแปรสำหรับเก็บค่าข้อมูลในแต่ละ คอลัมน์ที่ต้องการ กำหนดเป็น array $data_price =[]; $data_com =[]; $q =" SELECT * FROM tbl_sale ORDER BY sale_date "; $rs = $mysqli ->query( $q ); $total = $rs ->num_rows; while ( $data = $rs ->fetch_assoc()){ $show_row_end =0; // เริ่มต้นการแบ่ง กำหนดเป็น 0 // จัดรูปแบบ key วันที่ที่จะใช้เก็บข้อมูล (หากไม่ได้ใช้วันที่ ประยุกต์เป็นอย่างอื่นตามต้องการ) $dateKey = date ( "dmY" , strtotime ( $data [ 'sale_date' ])); /// ส่วนของการกำนหด การเปรียบค่าของรายการ เพื่อแบ่งวันที่เป็นสัดส่วน $temp_data1 = $data [ 'sale_date' ]; if ( $temp_data2 ==null){ $temp_data2 = $temp_data1 ; $data_show =1; } else { if ( $temp_data1 == $temp_data2 ){ $data_show =0; $temp_data2 = $temp_data1 ; } else { $temp_data2 = $temp_data1 ; $data_show =1; } } // เก็บค่าวันที่ของรายการข้อมูลไว้ในตัวแปร สำหรับเปรียบเทียบ $arr_dateCheck [ $i ]= $data [ 'sale_date' ]; // ถ้าไม่ใช้ข้อมูลรายการแรก และ ข้อมูลวันที่รายการก่อนหน้า ไม่เท่ากับรายการที่กำลังแสดง // นั่นหมายถึงจุดที่เราจะกำหนดว่า เป็นรายการสุดท้ายของวันที่หนึ่งๆ if ( $i >1 && $arr_dateCheck [ $i -1]!= $data [ 'sale_date' ]){ // กำหนด key วันที่ที่จะเช็ค $dateKeyCheck = date ( "dmY" , strtotime ( $arr_dateCheck [ $i -1])); $show_row_end =1; // ให้แสดง หรือแทรกแถวที่ต้องการได้ } // ส่วนของการเก็บข้อมูลไว้ใน array เพื่อใช้งานผลรวม if (!isset( $data_price [ $dateKey ])){ // ถ้าไม่มีตัวแปร $data_price [ $dateKey ]=[]; // ให้กำหนด array_push ( $data_price [ $dateKey ], $data [ 'sale_price' ]); // และเพิ่มค่า } else { array_push ( $data_price [ $dateKey ], $data [ 'sale_price' ]); // เพิ่มค่าใน array } if (!isset( $data_com [ $dateKey ])){ $data_com [ $dateKey ]=[]; array_push ( $data_com [ $dateKey ], $data [ 'sale_com' ]); } else { array_push ( $data_com [ $dateKey ], $data [ 'sale_com' ]); } ?> <?php // แทรกแถวที่ต้องการกรณีปกติ กรณีนี้ รายการสุดท้ายจะไม่ขึ้น เราจะเพิ่มการแทรกไว้ด้านหลัง // ของข้อมูลแทน if ( $show_row_end ==1){?> <?php // รวมค่าข้อมูลแต่ละวัน แล้วเพิ่มเข้าไปใน array รายการค่าสะสม array_push ( $aggre_price , array_sum ( $data_price [ $dateKeyCheck ])); array_push ( $aggre_com , array_sum ( $data_com [ $dateKeyCheck ])); ?> <tr class = "bg-warning" > <td class = "text-right" >รวมรายวัน</td> <td></td> <td><?= array_sum ( $data_price [ $dateKeyCheck ])?></td> <td><?= array_sum ( $data_com [ $dateKeyCheck ])?></td> </tr> <tr class = "bg-info" > <td class = "text-right" >รวมสะสม</td> <td></td> <td><?= array_sum ( $aggre_price )?></td> <td><?= array_sum ( $aggre_com )?></td> </tr> <?php } ?> <tr> <td> <?php if ( $data_show ==1){?> <?= $data [ 'sale_date' ]?> <?php } ?> </td> <td><?= $data [ 'sale_product' ]?></td> <td><?= $data [ 'sale_price' ]?></td> <td><?= $data [ 'sale_com' ]?></td> </tr> <?php if ( // สำหรับแทรก กรณีเป็นรายการสุดท้ายในตาราง $i == $total ){?> <?php // รายการสุดท้าย กำหนด key เช็คเป็นค่าที่รูปแบบตรงกัน $dateKeyCheck = date ( "dmY" , strtotime ( $data [ 'sale_date' ])); // รวมค่าข้อมูลแต่ละวัน แล้วเพิ่มเข้าไปใน array รายการค่าสะสม array_push ( $aggre_price , array_sum ( $data_price [ $dateKeyCheck ])); array_push ( $aggre_com , array_sum ( $data_com [ $dateKeyCheck ])); ?> <tr class = "bg-warning" > <td class = "text-right" >รวมรายวัน</td> <td></td> <td><?= array_sum ( $data_price [ $dateKeyCheck ])?></td> <td><?= array_sum ( $data_com [ $dateKeyCheck ])?></td> </tr> <tr class = "bg-info" > <td class = "text-right" >รวมสะสม</td> <td></td> <td><?= array_sum ( $aggre_price )?></td> <td><?= array_sum ( $aggre_com )?></td> </tr> <?php } ?> <?php $i ++; } ?> </tbody> </table> </div> </body> </html> |
เนื้อหาส่วนนี้ค่อนข้างซับซ้อนและมีรูปแบบการใช้งานที่จำเป็นต้องทำความเข้าใจ
สามารถนำไปประยุกต์กับรูปแบบ หรือตารางอื่นๆ ได้