ข้ามไปที่เนื้อหาหลัก

ฝึกเขียน PHP OOP ด้วยโจทย์ปัญหาคณิตศาสตร์ กับการคำนวณยอดจำหน่ายคูปอง


       ภาษา PHP นอกจากใช้เขียนเว็บไซต์แล้วยังสามารถทำงานได้อีกหลายอย่าง เพราะสามารถทำงานร่วมกับฐานข้อมูลเช่น MySQL ดังนั้น เราก็สามารถสร้างเว็บเพจที่มีพลังได้ดังเช่นการเขียนโปรแกรมภาษาอื่นๆ




       วันนี้เป็นอีกวันที่จะเขียนบทความแนวโจทย์ปัญหาคณิตศาสตร์ เพื่อใช้ฝึกเขียน PHP กันอีกครั้ง โดยจะเป็นการคำนวณยอดจำหน่ายคูปองในแต่ละวัน จากจำนวนยอดคงเหลือ ซึ่งโจทย์ปัญหาดังรูปด่างล่างนี้


ฝึกเขียน PHP OOP การคำนวณยอดจำหน่ายคูปอง



หลังจากได้โจทย์แล้ว ก็มาดูกันที่วิธีคิดครับ ผมจะใช้หลักการดังที่แสดงไว้ในตารางด้านล่างนี้


คำนวณจำนวนคูปองที่จำหน่ายไป
มีคูปองใบละ 5 บาท จำนวน 100 ใบ
จำนวนคงเหลือ 25 ใบ
ดังนั้น ยอดจำหน่ายจะได้เท่ากับ 100 - 25 75 ใบ

มีคูปองใบละ 10 บาท จำนวน 100 ใบ
จำนวนคงเหลือ 50 ใบ
ดังนั้น ยอดจำหน่ายจะได้เท่ากับ 100 - 50 50 ใบ

มีคูปองใบละ 20 บาท จำนวน 100 ใบ
จำนวนคงเหลือ 30 ใบ
ดังนั้น ยอดจำหน่ายจะได้เท่ากับ 100 - 30 70 ใบ

คิดราคาคูปองที่จำหน่ายได้ทั้งหมด

จำหน่ายคูปองใบละ 5 บาท จำนวน 75 คิดเป็นเงิน 375.00 บาท
จำหน่ายคูปองใบละ 10 บาท จำนวน 50 คิดเป็นเงิน 500.00 บาท
จำหน่ายคูปองใบละ 20 บาท จำนวน 70 คิดเป็นเงิน 1,400.00 บาท
รวมเป็นเงินทั้งสิ้น (375 + 500 + 1,400) 2,275.00 บาท




มาลองเขียนโปรแกรม PHP เพื่อคำนวณค่ากันเลย


 
<?php

Class CouponProcess{

    private $store;
    private $remain;
   
    private $used5;
    private $used10;
    private $used20;
   
    /**
     * @param $date Array ส่งข้อมูลชุดที่ต้องการคำนวณมาในคลาส
     */
    public function __construct($data)
    {
        $this->store = $data['store'];
        $this->remain = $data['remain'];
       
        $this->used5 = $this->store[5] - $this->remain[5];
        $this->used10 = $this->store[10] - $this->remain[10];
        $this->used20 = $this->store[20] - $this->remain[20];
    }
   
    /**
     * @param $c5 Number จะรับค่าข้อมูลคูปอง 5 บาท
     * @param $c10 Number จะรับค่าข้อมูลคูปอง 10 บาท
     * @param $c20 Number จะรับค่าข้อมูลคูปอง 20 บาท
     * @return Array จะส่งค่าออกไปในรูปแบบข้อมูลเป็นชุด ประกอบด้วยคีย์  coupon5, coupon10, coupon20
     */
    public function send_output($c5, $c10, $c20)
    {
        return array('coupon5' => $c5, 'coupon10' => $c10, 'coupon20' => $c20);
    }
   
    /**
     * หาจำนวนคูปองที่พิมพ์ออกมาทั้งหมด
     * @return Array จะส่งค่าออกไปในรูปแบบข้อมูลเป็นชุด ประกอบด้วยคีย์  coupon5, coupon10, coupon20
     */
    public function get_coupon()
    {
        return $this->send_output($this->store[5], $this->store[10], $this->store[20]);
    }
   
    /**
     * หาจำนวนคูปองที่ถูกใช้ไป
     * @return Array จะส่งค่าออกไปในรูปแบบข้อมูลเป็นชุด ประกอบด้วยคีย์  coupon5, coupon10, coupon20
     */
    public function get_used()
    {
        return $this->send_output($this->used5, $this->used10, $this->used20);
    }
   
    /**
     * หาจำนวนคูปองคงเหลือ
     * @return Array จะส่งค่าออกไปในรูปแบบข้อมูลเป็นชุด ประกอบด้วยคีย์  coupon5, coupon10, coupon20
     */
    public function get_remain()
    {
        return $this->send_output($this->remain[5], $this->remain[10], $this->remain[20]);
    }
   
    /**
     * หาราคาของจำนวนคูปองที่ถูกใช้ไป
     * @return Array จะส่งค่าออกไปในรูปแบบข้อมูลเป็นชุด ประกอบด้วยคีย์  coupon5, coupon10, coupon20
     */
    public function get_used_price()
    {
        $c5 = $this->used5 * 5;
        $c10 = $this->used10 * 10;
        $c20 = $this->used20 * 20;
        return $this->send_output($c5, $c10, $c20);
    }

}
/* END OF Class CouponProcess */


/* ---- เตรียมข้อมูลสำหรับเรียกใช้ ---- */

//store เก็บจำนวนคูปองที่พิมพ์ต่อวัน
//remain เก็บจำนวนคปองคงเหลือในแต่ละวัน
$dataArray = array(
            '2014-07-01' => array(
                                'store' => array(5=>100, 10=>100, 20=>100),
                                'remain' => array(5=>20, 10=>35, 20=>15)
                            ),
            '2014-07-02' => array(
                                'store' => array(5=>100, 10=>100, 20=>100),
                                'remain' => array(5=>25, 10=>50, 20=>30)
                            )
        );

       
/* ---- ประมวลผลข้อมูล ตามวันที่ที่ส่งเข้ามา ---- */
$couponArray = array();
$remainArray = array();
$usedArray = array();
$priceArray = array();
$totalPrice = 0;
   
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');//วันที่ ที่ต้องการดูข้อมูล

$data = isset($dataArray[$date]) ? $dataArray[$date] : array();    //หาข้อมูลตามวันที่ ที่ส่งเข้ามาผ่าน $_GET

if(!empty($data)){
    $obj = new CouponProcess($data);
    $couponArray = $obj->get_coupon();
    $remainArray = $obj->get_remain();
    $usedArray = $obj->get_used();
    $priceArray = $obj->get_used_price();
    $totalPrice = $priceArray['coupon5'] + $priceArray['coupon10'] + $priceArray['coupon20'];
}else{
    $couponArray['coupon5'] = 0;
    $couponArray['coupon10'] = 0;
    $couponArray['coupon20'] = 0;
    $remainArray['coupon5'] = 0;
    $remainArray['coupon10'] = 0;
    $remainArray['coupon20'] = 0;
    $usedArray['coupon5'] = 0;
    $usedArray['coupon10'] = 0;
    $usedArray['coupon20'] = 0;
    $priceArray['coupon5'] = 0;
    $priceArray['coupon10'] = 0;
    $priceArray['coupon20'] = 0;   
}
?>

<!DOCTYPE html>
<html>
  <head>
    <title>การเขียนโปรแกรม PHP คำนวณยอดจำหน่ายคูปองในแต่ละวัน</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style>
        .store{ color : blue;}
        .remain{ color : orange;}
        .used{ color : red;}
        .c5{ color : #20B124;}
        .c10{ color : #F810FF;}
        .c20{ color : #0BC5EE;}
        table{
            border-collapse: collapse;
        }
        td{ border: 1px solid #cccccc; }
    </style>
  </head>
  <body>
    <div style="padding : 10px">
    เลือกวันที่ => | <a href="<?php echo $_SERVER['PHP_SELF'];?>?date=2014-07-01">2014-07-01</a>
    | <a href="<?php echo $_SERVER['PHP_SELF'];?>?date=2014-07-02">2014-07-02</a> |
    </div>
    <div style="clear:both"/>
    <div id="content" style="border: 2px solid #cccccc; float: left; padding : 20px">
        <h1>การเขียนโปรแกรม PHP <br/>คำนวณยอดจำหน่ายคูปองวันที่
        <font color="blue"><?php echo $date;?></font></h1>
    <?php
        if(empty($data)):
            echo '<p><font color="red">ไม่พบข้อมูลของวันที่ '. $date .'</font></p>';
        endif;
    ?>
        <p>
            จำนวนคูปอง <span class="c5">5</span> บาท <span class="store">
            <?php echo number_format($couponArray['coupon5']);?></span> ใบ<br/>
            จำนวนคูปอง <span class="c10">10</span> บาท <span class="store">
            <?php echo number_format($couponArray['coupon10']);?></span> ใบ<br/>
            จำนวนคูปอง <span class="c20">20</span> บาท <span class="store">
            <?php echo number_format($couponArray['coupon20']);?></span> ใบ<br/>
        </p>
        <p>
            จำนวนคูปอง <span class="c5">5</span> บาท คงเหลือ  <span class="remain">
            <?php echo number_format($remainArray['coupon5']);?></span> ใบ<br/>
            จำนวนคูปอง <span class="c10">10</span> บาท  คงเหลือ  <span class="remain">
            <?php echo number_format($remainArray['coupon10']);?></span> ใบ<br/>
            จำนวนคูปอง <span class="c20">20</span> บาท  คงเหลือ  <span class="remain">
            <?php echo number_format($remainArray['coupon20']);?></span> ใบ<br/>
        </p>
        <p>
            <i><u>จากข้อมูลด้านบน ให้หาข้อมูลดังต่อไปนี้</u></i><br/>
            1) จำนวนคูปองที่จำหน่ายได้ในแต่ละวัน<br/>
            2) มูลค่าของคูปองที่จำหน่ายได้ในแต่ละวัน<br/>
            * ให้แสดงแยกตามประเภทและสรุปผลรวมด้วย
        </p>
    </div>
    <div style="clear:both">
        <br/>
        <p><u><strong>มาลองเขียนโปรแกรม PHP เพื่อคำนวณค่ากันเลย</strong></u></p>
        <div>
            <table width="400" border="1" cellspacing="0" cellpadding="5">
                <tr>
                    <td>มีคูปองใบละ <span class="c5">5</span> บาท จำนวน</td>
                    <td align="right">
                        <span class="store"><?php echo number_format($couponArray['coupon5']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
                <tr>
                    <td>จำนวนคงเหลือ</td>
                    <td align="right">
                        <span class="store"><?php echo number_format($remainArray['coupon5']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
                <tr>
                    <td>ดังนั้น ยอดจำหน่ายจะได้เท่ากับ  <span class="store">
                        <?php echo number_format($couponArray['coupon5']);?>
                        - <?php echo number_format($remainArray['coupon5']);?>
                        </span>
                    </td>
                    <td align="right">
                        <span class="used"><?php echo number_format($usedArray['coupon5']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
            </table>
        </div>
        <br/>
        <div>
            <table width="400" border="1" cellspacing="0" cellpadding="5">
                <tr>
                    <td>มีคูปองใบละ <span class="c10">10</span> บาท จำนวน</td>
                    <td align="right">
                        <span class="store"><?php echo number_format($couponArray['coupon10']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
                <tr>
                    <td>จำนวนคงเหลือ</td>
                    <td align="right">
                        <span class="store"><?php echo number_format($remainArray['coupon10']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
                <tr>
                    <td>ดังนั้น ยอดจำหน่ายจะได้เท่ากับ  <span class="store">
                        <?php echo number_format($couponArray['coupon10']);?>
                        - <?php echo number_format($remainArray['coupon10']);?>
                        </span>
                    </td>
                    <td align="right">
                        <span class="used"><?php echo number_format($usedArray['coupon10']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
            </table>
        </div>
        <br/>
        <div>
            <table width="400" border="1" cellspacing="0" cellpadding="5">
                <tr>
                    <td>มีคูปองใบละ <span class="c20">20</span> บาท จำนวน</td>
                    <td align="right">
                        <span class="store"><?php echo number_format($couponArray['coupon20']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
                <tr>
                    <td>จำนวนคงเหลือ</td>
                    <td align="right">
                        <span class="store"><?php echo number_format($remainArray['coupon20']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
                <tr>
                    <td>ดังนั้น ยอดจำหน่ายจะได้เท่ากับ  <span class="store">
                        <?php echo number_format($couponArray['coupon20']);?>
                        - <?php echo number_format($remainArray['coupon20']);?>
                        </span>
                    </td>
                    <td align="right">
                        <span class="used"><?php echo number_format($usedArray['coupon20']);?></span>
                    </td>
                    <td>ใบ</td>
                </tr>
            </table>
        </div>   
        <br/>
        <h4><u>คิดราคาคูปองที่จำหน่ายได้ทั้งหมด</u></h4>
        <div>
            <table width="550" border="1" cellspacing="0" cellpadding="5">
                <tr>
                    <td>
                        จำหน่ายคูปองใบละ <span class="c5">5</span> บาท
                        จำนวน <?php echo number_format($usedArray['coupon5']);?> คิดเป็นเงิน
                    </td>
                    <td align="right">
                        <span class="store"><?php echo number_format($priceArray['coupon5'], 2);?></span>
                    </td>
                    <td>บาท</td>
                </tr>
                <tr>
                    <td>
                        จำหน่ายคูปองใบละ <span class="c10">10</span> บาท
                        จำนวน <?php echo number_format($usedArray['coupon10']);?> คิดเป็นเงิน
                    </td>
                    <td align="right">
                        <span class="store"><?php echo number_format($priceArray['coupon10'], 2);?></span>
                    </td>
                    <td>บาท</td>
                </tr>
                <tr>
                    <td>
                        จำหน่ายคูปองใบละ <span class="c20">20</span> บาท
                        จำนวน <?php echo number_format($usedArray['coupon20']);?> คิดเป็นเงิน
                    </td>
                    <td align="right">
                        <span class="store"><?php echo number_format($priceArray['coupon20'], 2);?></span>
                    </td>
                    <td>บาท</td>
                </tr>
                <tr>
                    <td>
                        <b>รวมเป็นเงินทั้งสิ้น (<?php echo number_format($priceArray['coupon5']);?> +
                                <?php echo number_format($priceArray['coupon10']);?> +
                                <?php echo number_format($priceArray['coupon20']);?>)
                        </b>
                    </td>
                    <td align="right">
                        <span class="used"><b><?php echo number_format($totalPrice, 2);?></b></span>
                    </td>
                    <td><b>บาท</b></td>
                </tr>               
            </table>
        </div>       
    </div>
    <br/>
  </body>
</html>
  




สำหรับโค๊ด PHP ตัวอย่างทั้งหมดนี้จะเป็นการเขียนส่วนของการคำนวณเก็บไว้ในคลาสที่ชื่อ CouponProcess() จะมีเมธอดหรือฟังก์ชั่นให้เรียกใช้ด้วยกัน 4 ฟังก์ชั่นประกอบด้วย

$obj->get_coupon();
$obj->get_remain();
$obj->get_used();
$obj->get_used_price();

เป็นส่วนที่ใช้ดึงข้อมูลและคำนวณยอดที่ต้องการจากคลาสที่สร้างขึ้น ส่วนรายละเอียดนั้นได้อธิบายไว้ในโค๊ด PHP + HTML ด้านบนแล้วครับ





"PHP ไม่ได้สร้างสุดยอดโปรแกรม
แต่ PHP ช่วยให้งานคุณง่ายขึ้นต่างหาก"

PHP CI MANIA PHP Code Generator 
โปรแกรมช่วยสร้างโค้ด ลดเวลาการเขียนโปรแกรม

สนใจสั่งซื้อเพียง 3,990 บาท



PHP CI MANIA PHP Code Generator 

โปรแกรมช่วยสร้างโค้ด "ลดเวลาการเขียนโปรแกรม"
ราคาสุดคุ้มเพียง 3,990 บาท 
http://www.phpcodemania.com

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจกฟรี!! ระบบต่างๆที่พัฒนาด้วย PHP สำหรับนำไปใช้ในงานต่างๆ

       สำหรับหลายท่านที่ขอโค้ดเข้ามาทาง Inbox ของเฟซบุ๊กแฟนเพจ หรือถามถึงระบบต่างๆหลังไมค์มานั้น ส่วนใหญ่ก็มีแจกอยู่แล้วในเว็บบอร์ด ThaiCreate.Com นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สอนเขียน PHP แสดงการจองห้องประชุมแบบไฮไลท์ตามช่วงเวลา (แบบเชื่อมต่อฐานข้อมูล MySQL)

ตัวอย่าง ผลลัพธ์ที่ได้จากการจองในฐานข้อมูล ตาราง tb_room สร้างตารางรายชื่อห้องประชุม สำหรับ id นั้นเป็น Primarykey จะกำหนดให้สร้างอัตโนมัติ ทุกครั้งที่เราเพิ่มชื่อห้องประชุมใหม่ -- -- Database: `tobedev_example` -- -- -------------------------------------------------------- -- -- Table structure for table `tb_room` -- CREATE TABLE IF NOT EXISTS `tb_room` (   `id` int(11) NOT NULL,   `name` varchar(30) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- -- Dumping data for table `tb_room` -- INSERT INTO `tb_room` (`id`, `name`) VALUES (1, 'ห้องประชุม 1'), (2, 'ห้องประชุม 2'), (3, 'ห้องประชุม 3'), (4, 'ห้องประชุม 4'), (5, 'ห้องประชุม 5'); -- -- Indexes for dumped tables -- -- -- Indexes for table `tb_room` -- ALTER TABLE `tb_room`   ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tb_room` -- ALTER TABLE `tb_room`   MODIFY `i...

การนำไอคอน มาแสดงบน Fullcalendar

เนื่องจากมีน้องคนหนึ่งให้ช่วยดูโค้ดเกี่ยวกับ Fullcalendar ให้หน่อย แล้วพอดีช่วงนี้ผมก็กำลังสนใจปฏิทิน Fullcalendar อยู่พอดี จึงลองหาสาเหตุที่ไม่สามารถแสดงข้อมูลจาก MySQL และไม่สามารถแทรก icon เข้าไปใน Fullcalendar ได้ จากที่สังเกตุได้ครั้งแรกคือ Error ที่ Console ของ Firefox เกี่ยวกับฟังก์ชั่นที่เขียนผิด และก็มีการ echo ค้างไว้ในส่วนของไฟล์ getCalendar.php ก็เลยจัดการทดสอบแล้วลบ echo ออกให้เหลือแค่ echo json_encode($event_array); ที่ได้ใช้งานจริงเท่านั้น ขั้นตอนการตรวจสอบความถูกต้องของโค้ด PHP 1) ต้องแน่ใจว่าคำสั่งที่เขียนไว้ สามารถดึงข้อมูลมาแสดงผลได้ด้วยการ echo $sql; 2) นำคำสั่งที่ได้ไปรันในโปรแกรมจัดการฐานข้อมูล ในที่นี้คือ phpMyAdmin 3) เมื่อตรวจสอบดูผลลัพธ์ที่ได้ หากถูกต้องมีข้อมูลก็แสดงว่าการ Query ทำงานได้ 4) มาดูการทำงานของ JavaScript ในส่วนของ jQuery มีการแจ้งเตือนที่ฟังก์ชั่น .on() ซึ่งจะใช้กับ jQuery เวอร์ชั่นใหม่เท่านั้น นั่นหมายถึงเวอร์ชั่น jQuery ที่มากับ Fullcalendar เป็นเวอร์ชั่นเก่า ก็ให้เปลี่ยนไปใช้ .live() แทน ก็จะทำงานได้ปกติ 5) ม...