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

สอนเขียน PHP ด้วยการหาความน่าจะเป็นของตัวเลข 4 หลัก


ส่วนที่ต้องแก้ไขมีเพียงแค่การเชื่อมต่อฐานข้อมูลนะครับ

mysqli_connect("127.0.0.1", "dev_user", "dev.123456", "test");
dev_user คือ ยูสเซอร์สำหรับเชื่อต่อฐานข้อมูล
dev.123456 คือ รหัสผ่านสำหรับเชื่อมต่อฐานข้อมูล
test คือชื่อของฐานข้อมูล

โค้ด PHP

<?php
class numObj{
    public $result = array();
    private $store = array();
  
    function __construct(){
        $this->index();
    }
  
    private function index(){
        $dbNumber = $this->load_number();
        $data = array();
        for($i=0;$i<9999;$i++):
            $number = str_pad($i, 4, '0', STR_PAD_LEFT);
            if($this->check_number($number)===true):
                //echo '<br>', $number;
                $this->store[] = $number;
                $n=0;
                $nn=array();
                $yy=array();
                foreach($dbNumber as $arr):
                    $num = $arr['number'];
                    $a = array($num[0], $num[1], $num[2]);
                    $b = array($number[0], $number[1], $number[2], $number[3]);
                    if(count(array_unique(array_intersect($a, $b))) >= 3):
                        $n++;
                        $nn[] = $num;
                        $yy[] = $arr['year'];
                    endif;
                endforeach;
              
                $data[] = array('rand_num' => $number, 'qty' => $n, 'number' => $nn, 'year' => $yy);
            endif;
        endfor;
          
        $this->result = $data;
    }
  
    private function check_number($num){
        //1234 == 4321  = 2134  = 2314 ..... in 4 number is false
        $compare = false;
        $store = $this->store;
        if(empty($store)){
            $compare = true;
        }else{
            $compare = true;
            $j = array($num[0], $num[1], $num[2], $num[3]);
            foreach($store as $arr):
                $k = array($arr[0], $arr[1], $arr[2], $arr[3]);
                if(count(array_intersect($j, $k)) == 4):
                    $compare = false;
                    //echo '<pre>',print_r(array_intersect($j, $k),true),'</pre>';
                    break;
                endif;
            endforeach;
        }
        return $compare;
    }
  
    private function load_number(){
        $data = array();
        $mysqli = mysqli_connect("127.0.0.1", "dev_user", "dev.123456", "test");

        if (!$mysqli) {
            echo "Error: Unable to connect to MySQL." . PHP_EOL;
            echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
            echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
            exit;
        }

        $query = "SELECT rear, LEFT(date_num,7) FROM tb_number ORDER by date_num DESC";

        if ($stmt = $mysqli->prepare($query)) {

            /* execute statement */
            $stmt->execute();

            /* bind result variables */
            $stmt->bind_result($rear,$year);

            /* fetch values */
            while ($stmt->fetch()) {
                $data[] = array('number' => $rear, 'year'=>$year);
            }

            /* close statement */
            $stmt->close();
        }

        mysqli_close($mysqli);
        return $data;
    }
  
    public static function display(){
        $obj = new numObj();
        $data = $obj->result;
        if(!empty($data)){
            $no=1;
            //-- SORT
            $price = array();
            foreach ($data as $key => $row)
            {
                $price[$key] = $row['qty'];
            }
            array_multisort($price, SORT_DESC, $data);
            //--
            foreach($data as $row):
                $all_number = implode(', ', $row['number']);
                $all_year =  implode(', ', $row['year']);
                echo "<tr>
                        <td>$no</td>
                        <td>$row[rand_num]</td>
                        <td>$row[qty]</td>
                        <td>$all_number</td>
                        <td>$all_year</td>
                      </tr>";
                $no++;
            endforeach;
        }
    }
}
?>
<html lang="en"><head>
    <meta charset="utf-8">
    <title>PHP กับการเปรียบเทียบความน่าจะเป็น</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  </head>

    <body>
        <table class="table">
            <thead>
              <tr>
                <th>#</th>
                <th>4Digit Number</th>
                <th>Qty</th>
                <th>DB Number</th>
                <th>Year</th>
              </tr>
            </thead>
            <tbody>
                <?php
                    numObj::display();
                ?>
            </tbody>
        </table>
    </body>
</html>

โค้ด SQL สำหรับสร้างฐานข้อมูล


CREATE TABLE IF NOT EXISTS `tb_number` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date_num` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `day` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  `month` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  `year` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
  `front` varchar(3) COLLATE utf8_unicode_ci NOT NULL,
  `rear` varchar(3) COLLATE utf8_unicode_ci NOT NULL,
  `two` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=121 ;

--
-- Dumping data for table `tb_number`
--

INSERT INTO `tb_number` (`id`, `date_num`, `day`, `month`, `year`, `front`, `rear`, `two`) VALUES
(1, '2016-06-01', '01', '06', '2559', '511', '825', '14'),
(2, '2016-05-16', '16', '05', '2559', '141', '737', '98'),
(3, '2016-05-02', '02', '05', '2559', '399', '459', '02'),
(4, '2016-04-16', '16', '04', '2559', '221', '609', '87'),
(5, '2016-04-01', '01', '04', '2559', '066', '720', '92'),
(6, '2016-03-16', '16', '03', '2559', '134', '918', '32'),
(7, '2016-03-01', '01', '03', '2559', '439', '686', '06'),
(8, '2016-02-16', '16', '02', '2559', '356', '364', '98'),
(9, '2016-02-01', '01', '02', '2559', '927', '800', '09'),
(10, '2016-01-17', '17', '01', '2559', '304', '371', '50'),
(11, '2015-12-30', '30', '12', '2558', '008', '217', '02'),
(12, '2015-12-17', '17', '12', '2558', '930', '255', '08'),
(13, '2015-12-01', '01', '12', '2558', '915', '350', '78'),
(14, '2015-11-16', '16', '11', '2558', '795', '283', '03'),
(15, '2015-11-01', '01', '11', '2558', '361', '211', '45'),
(16, '2015-10-16', '16', '10', '2558', '968', '630', '62'),
(17, '2015-10-01', '01', '10', '2558', '594', '825', '07'),
(18, '2015-09-16', '16', '09', '2558', '743', '148', '06'),
(19, '2015-09-01', '01', '09', '2558', '021', '094', '89'),
(20, '2015-08-16', '16', '08', '2558', '033', '363', '40'),
(21, '2015-08-01', '01', '08', '2558', '518', '677', '53'),
(22, '2015-07-16', '16', '07', '2558', '121', '507', '49'),
(23, '2015-07-01', '01', '07', '2558', '759', '049', '26'),
(24, '2015-06-16', '16', '06', '2558', '644', '742', '05'),
(25, '2015-06-02', '02', '06', '2558', '388', '881', '65'),
(26, '2015-05-16', '16', '05', '2558', '011', '421', '38'),
(27, '2015-05-02', '02', '05', '2558', '543', '466', '30'),
(28, '2015-04-16', '16', '04', '2558', '506', '260', '38'),
(29, '2015-04-01', '01', '04', '2558', '605', '704', '70'),
(30, '2015-03-16', '16', '03', '2558', '048', '151', '92'),
(31, '2015-03-01', '01', '03', '2558', '240', '237', '34'),
(32, '2015-02-16', '16', '02', '2558', '001', '864', '90'),
(33, '2015-02-01', '01', '02', '2558', '155', '537', '79'),
(34, '2015-01-16', '16', '01', '2558', '244', '351', '74'),
(35, '2014-12-30', '30', '12', '2557', '461', '704', '57'),
(36, '2014-12-16', '16', '12', '2557', '948', '354', '90'),
(37, '2014-12-01', '01', '12', '2557', '480', '449', '11'),
(38, '2014-11-16', '16', '11', '2557', '479', '804', '25'),
(39, '2014-11-01', '01', '11', '2557', '206', '608', '44'),
(40, '2014-10-16', '16', '10', '2557', '656', '409', '94'),
(41, '2014-10-01', '01', '10', '2557', '375', '615', '44'),
(42, '2014-09-16', '16', '09', '2557', '772', '269', '35'),
(43, '2014-09-01', '01', '09', '2557', '856', '763', '22'),
(44, '2014-08-16', '16', '08', '2557', '662', '842', '91'),
(45, '2014-08-01', '01', '08', '2557', '766', '391', '82'),
(46, '2014-07-10', '10', '07', '2557', '468', '728', '45'),
(47, '2014-07-01', '01', '07', '2557', '378', '477', '39'),
(48, '2014-06-16', '16', '06', '2557', '673', '920', '95'),
(49, '2014-06-01', '01', '06', '2557', '781', '198', '18'),
(50, '2014-05-16', '16', '05', '2557', '087', '523', '20'),
(51, '2014-05-02', '02', '05', '2557', '103', '297', '52'),
(52, '2014-04-16', '16', '04', '2557', '153', '406', '26'),
(53, '2014-04-01', '01', '04', '2557', '028', '866', '95'),
(54, '2014-03-16', '16', '03', '2557', '531', '404', '79'),
(55, '2014-03-01', '01', '03', '2557', '906', '318', '35'),
(56, '2014-02-16', '16', '02', '2557', '384', '245', '01'),
(57, '2014-02-01', '01', '02', '2557', '180', '149', '95'),
(58, '2014-01-16', '16', '01', '2557', '306', '902', '52'),
(59, '2013-12-30', '30', '12', '2556', '561', '072', '48'),
(60, '2013-12-16', '16', '12', '2556', '341', '767', '79'),
(61, '2013-12-01', '01', '12', '2556', '168', '795', '27'),
(62, '2013-11-16', '16', '11', '2556', '806', '925', '28'),
(63, '2013-11-01', '01', '11', '2556', '739', '804', '47'),
(64, '2013-10-16', '16', '10', '2556', '963', '289', '60'),
(65, '2013-10-01', '01', '10', '2556', '647', '882', '14'),
(66, '2013-09-16', '16', '09', '2556', '562', '684', '63'),
(67, '2013-09-01', '01', '09', '2556', '548', '123', '05'),
(68, '2013-08-16', '16', '08', '2556', '321', '327', '20'),
(69, '2013-08-01', '01', '08', '2556', '356', '435', '82'),
(70, '2013-07-16', '16', '07', '2556', '566', '996', '86'),
(71, '2013-07-01', '01', '07', '2556', '646', '905', '51'),
(72, '2013-06-16', '16', '06', '2556', '289', '673', '69'),
(73, '2013-06-01', '01', '06', '2556', '935', '489', '90'),
(74, '2013-05-16', '16', '05', '2556', '687', '125', '56'),
(75, '2013-05-02', '02', '05', '2556', '603', '458', '07'),
(76, '2013-04-16', '16', '04', '2556', '843', '846', '86'),
(77, '2013-04-01', '01', '04', '2556', '571', '688', '53'),
(78, '2013-03-16', '16', '03', '2556', '968', '433', '52'),
(79, '2013-03-01', '01', '03', '2556', '976', '241', '37'),
(80, '2013-02-16', '16', '02', '2556', '368', '257', '09'),
(81, '2013-02-01', '01', '02', '2556', '565', '566', '66'),
(82, '2013-01-16', '16', '01', '2556', '820', '981', '08'),
(83, '2012-12-30', '30', '12', '2555', '302', '358', '00'),
(84, '2012-12-16', '16', '12', '2555', '529', '524', '72'),
(85, '2012-12-01', '01', '12', '2555', '110', '443', '43'),
(86, '2012-11-16', '16', '11', '2555', '639', '500', '15'),
(87, '2012-11-01', '01', '11', '2555', '524', '694', '63'),
(88, '2012-10-16', '16', '10', '2555', '281', '343', '28'),
(89, '2012-10-01', '01', '10', '2555', '124', '025', '58'),
(90, '2012-09-16', '16', '09', '2555', '540', '143', '79'),
(91, '2012-09-01', '01', '09', '2555', '329', '997', '07'),
(92, '2012-08-16', '16', '08', '2555', '683', '877', '28'),
(93, '2012-08-01', '01', '08', '2555', '895', '590', '50'),
(94, '2012-07-16', '16', '07', '2555', '904', '050', '11'),
(95, '2012-07-01', '01', '07', '2555', '915', '900', '60'),
(96, '2012-06-16', '16', '06', '2555', '159', '373', '51'),
(97, '2012-06-01', '01', '06', '2555', '882', '727', '38'),
(98, '2012-05-16', '16', '05', '2555', '814', '418', '31'),
(99, '2012-05-02', '02', '05', '2555', '889', '501', '29'),
(100, '2012-04-16', '16', '04', '2555', '583', '470', '62'),
(101, '2012-04-01', '01', '04', '2555', '257', '562', '69'),
(102, '2012-03-16', '16', '03', '2555', '607', '064', '08'),
(103, '2012-03-01', '01', '03', '2555', '222', '518', '79'),
(104, '2012-02-16', '16', '02', '2555', '648', '684', '18'),
(105, '2012-02-01', '01', '02', '2555', '320', '605', '32'),
(106, '2012-01-16', '16', '01', '2555', '451', '445', '81'),
(107, '2011-12-30', '30', '12', '2554', '526', '402', '65'),
(108, '2011-12-16', '16', '12', '2554', '884', '178', '21'),
(109, '2011-12-01', '01', '12', '2554', '408', '147', '02'),
(110, '2011-11-16', '16', '11', '2554', '997', '777', '57'),
(111, '2011-11-01', '01', '11', '2554', '805', '540', '54'),
(112, '2011-10-16', '16', '10', '2554', '955', '756', '83'),
(113, '2011-10-01', '01', '10', '2554', '511', '052', '15'),
(114, '2011-09-16', '16', '09', '2554', '731', '198', '28'),
(115, '2011-09-01', '01', '09', '2554', '724', '533', '85'),
(116, '2011-08-16', '16', '08', '2554', '536', '960', '62'),
(117, '2011-08-01', '01', '08', '2554', '218', '756', '12'),
(118, '2011-07-16', '16', '07', '2554', '116', '556', '12'),
(119, '2011-07-01', '01', '07', '2554', '622', '953', '51'),
(120, '2011-06-16', '16', '06', '2554', '351', '276', '88');

ลองนำไปทดสอบเพื่อดูผลลัพธ์กันได้เลยนะครับ

ความคิดเห็น

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

แจกฟรี!! ระบบต่างๆที่พัฒนาด้วย 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) ม...