PHP สำหรับผู้เริ่มต้น : การเขียน SQL คิวรี่ ค่าน้ำ ค่าไฟ เดือนที่แล้วและเดือนปัจจุบัน

โดย SONGCHAI SAETERN

การ Query ข้อมูล ค่าน้ำค่าไฟเพื่อนำมาคำนวณ



ดูตัวอย่างได้จาก Video ด้านล่างนี้





[PHP Code]

<?php
$user = 'tobedev';
$pass = 'abcd.1234';
?>
<html>
<head>
<title>CODEMANIA.BLOGSPOT.COM</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">

<style>

    .highlight {
        background-color: #FFFF88;
    }

    .red_text{
        color : red;
    }
   
    table th,table td{
        text-align: center !important;
    }
</style>
</head>
<body>
    <?php
       
        $year = isset($_GET['year']) ? $_GET['year'] : date('Y');
        $month = isset($_GET['month']) ? $_GET['month'] : date('m');
        if(strlen($month)==1){
            $month = '0'.$month;
        }
        $current_year_month = $year . '-' . $month;
         $prev_year_month = date('Y-m', strtotime('-1 months', strtotime($current_year_month)));
       
        try {
            $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

            $sth = $dbh->prepare("SELECT room_id,
                                    SUM( IF( DATE_FORMAT( date_record, '%Y-%m' ) = '$prev_year_month', water, '' ) ) AS prev_water,
                                    SUM( IF( DATE_FORMAT( date_record, '%Y-%m' ) = '$current_year_month', water, '' ) ) AS current_water,
                                    SUM( IF( DATE_FORMAT( date_record, '%Y-%m' ) = '$prev_year_month', elect, '' ) ) AS prev_elect,
                                    SUM( IF( DATE_FORMAT( date_record, '%Y-%m' ) = '$current_year_month', elect, '' ) ) AS current_elect
                                FROM `tb_meter_list`
                                WHERE DATE_FORMAT( date_record, '%Y-%m' ) = '$current_year_month'
                                    OR DATE_FORMAT( date_record, '%Y-%m' ) = '$prev_year_month'
                                GROUP BY room_id");
            $sth->execute();
            $result = $sth->fetchAll();
       
            $dbh = null;
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }
       
        //echo '<pre>', print_r($data, true), '</pre>';
    ?>
       
    <div class="container">
   
    <div class="header clearfix">
        <h3 class="text-muted"><span class="red_text">PHP MySQL</span> : CODEMANIA.BLOGSPOT.COM    </h3>
    </div>
     
    <form class="form-horizontal" method="GET" action="meter_list.php">
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">เลือกเดือน : </label>
           
             <input type="text" class="form-control" name="year" placeholder="เดือน" value="<?php echo $year;?>">
             <input type="text" class="form-control" name="month" placeholder="เดือน" value="<?php echo $month;?>">
            <input type="submit" value="ประมวลผล" />
        </div>
    </form>
   
      <div class="row">       
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th rowspan=2>ห้อง</th>
                    <th colspan=2>ค่าน้ำ</th>
                    <th colspan=2>ค่าไฟ</th>
                </tr>
                <tr>
                    <td align="center">เดือนที่แล้ว</td>
                    <td align="center">เดือนปัจจุบัน</td>
                    <td align="center">เดือนที่แล้ว</td>
                    <td align="center">เดือนปัจจุบัน</td>
                </tr>
            </thead>
            <tbody>
                <?php
                    foreach($result as $row){
                ?>
                <tr>
                    <td><?php echo $row['room_id'];?></td>
                    <td><?php echo $row['prev_water'];?></td>
                    <td><?php echo $row['current_water'];?></td>
                    <td><?php echo $row['prev_elect'];?></td>
                    <td><?php echo $row['current_elect'];?></td>
                </tr>
                <?php }?>
            </tbody>
        </table>
      </div>
     
    </div><!-- container -->

    <footer class="footer">
        <br/><br/>
        <div class="container">
        <i>ติดตามความเคลื่อนไหวได้ที่ :: <a href='https://www.facebook.com/ToBeDeveloper'>https://www.facebook.com/ToBeDeveloper</a></i>
        </div>
    </footer>

</div> <!-- /container -->
</body>
</html>


[ SQL สำหรับสร้างตาราง] 

--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `tb_meter_list`
--

CREATE TABLE IF NOT EXISTS `tb_meter_list` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `room_id` int(4) NOT NULL,
  `water` double(15,2) NOT NULL,
  `elect` double(15,2) NOT NULL,
  `date_record` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='ตารางจดบันทึกค่าน้ำค่าไฟหอพัก' AUTO_INCREMENT=10 ;

--
-- Dumping data for table `tb_meter_list`
--

INSERT INTO `tb_meter_list` (`id`, `room_id`, `water`, `elect`, `date_record`) VALUES
(1, 1, 2100.00, 20001.00, '2017-02-15'),
(2, 1, 2159.00, 20101.00, '2017-03-16'),
(3, 2, 2100.00, 10001.00, '2017-02-15'),
(4, 2, 2139.00, 10091.00, '2017-03-16'),
(5, 2, 2199.00, 10131.00, '2017-04-15'),
(6, 2, 2229.00, 10171.00, '2017-05-16'),
(7, 3, 1199.00, 60131.00, '2017-02-15'),
(8, 3, 1229.00, 60171.00, '2017-03-17'),
(9, 3, 1281.00, 60211.00, '2017-04-15');



PHP CI MANIA PHP Code Generator 
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม เขียนโปรแกรมง่ายและสะดวกขึ้น
สนใจสั่งซื้อราคาสุดคุ้ม >> http://fastcoding.phpcodemania.com/