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

การสลับสีตารางแบบง่ายๆ PHP MySQLi

สำหรับวิธีการสลับสีตารางแบบง่ายๆ ในตัวอย่างนี้ จะเป็นการเปลี่ยนสี bgcolor โดยจะตรวจสอบก่อนว่ารหัสตรงกันหรือไม่ ถ้าตรงกันก็ใช้สีเดิม แต่ถ้ารหัสเปลี่ยนก็ใช้อีกสีสลับกันไป



โค้ด PHP ตัวอย่าง




<?php
$conn = mysqli_connect("localhost","tobedev","abcd.1234", "test");  //ข้อมูลนี้ได้มาจากตอนติดตั้งเว็บเซิร์ฟเวอร์
mysqli_set_charset($conn, "utf8");//ส่วนนี้คือการตั้งค่า encoding แบบสากล ถ้าฐานข้อมูลเป็น tis-620 ก็ต้องเปลี่ยนด้วย
$allData = array();
$strSQL = "SELECT * FROM `example` ";
$qry = mysqli_query($conn, $strSQL) or die('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ Error : '. mysqli_error());
while($row = mysqli_fetch_assoc($qry)){
$allData[] = $row;
}
mysqli_close($conn);//ปิดการเชื่อมต่อฐานข้อมูล
?>
<html>
<head>
<title>SunZanDesgin.Blogspot.Com</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<table border="1" width="400">
<tr>
<th>id</th>
<th>code</th>
<th>name</th>
</tr>
<?php
$prev_code = '';
$color1 = '#ffffff';
$color2 = '#cccccc';
$prev_color = $color1;
$bg_color = $color1;
foreach($allData as $row){
if($row['code']==$prev_code){
$bg_color = $prev_color;
}else{
if($prev_color==$color1){
$bg_color = $color2;
}else{
$bg_color = $color1;
}
}
echo "<tr bgcolor='$bg_color'>
<td>$row[id]</td>
<td>$row[code]</td>
<td>$row[name]</td>
</tr>";
$prev_code = $row['code'];
$prev_color = $bg_color;
}
?>
</table>
</body>
</html>




SQL สร้างตารางตัวอย่าง


--
-- Table structure for table `example`
--
CREATE TABLE `example` (
  `id` int(11) NOT NULL,
  `code` varchar(2) NOT NULL,
  `name` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `example`
--
INSERT INTO `example` (`id`, `code`, `name`) VALUES
(1, '01', 'Apple'),
(2, '01', 'Mango'),
(3, '02', 'Pen'),
(4, '02', 'Ruler'),
(5, '03', 'Pan'),
(6, '03', 'Pot'),
(7, '03', 'Cup');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `example`
--
ALTER TABLE `example`
  ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `example`
--
ALTER TABLE `example`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;


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

สนใจสั่งซื้อเพียง 4,500 บาท
http://fastcoding.phpcodemania.com/




สนับสนุนค่ากาแฟผู้เขียนได้ที่

PayPal

ความคิดเห็น

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

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

การดึงข้อมูล MySQL มาแสดงในปฏิทิน Fullcalendar ด้วย PHP

หลักการทำงาน เรียกข้อมูลจากตาราง tb_event ด้วยฟังก์ชั่นของ MySQLi ข้อมูลจากฐานข้อมูลจะถูกวนลูปเก็บไว้ใน $data และจะถูกส่งไปที่ Fullcalendar ด้วยการแปลงข้อมูลจาก PHP ให้อยู่ในรูปแบบ JSON อาร์เรย์ด้วยฟังก์ชั่น json_encode() PHP Code <?php //Database $data = array(); $link = mysqli_connect("127.0.0.1", "tobedev", "1234", "tobedev_example"); mysqli_set_charset($link, 'utf8'); if (!$link) {     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 * FROM tb_event"; if ($result = $link->query($query)) {     /* fetch object array */     while ($obj = $result->fetch_object()) {        $data[] = array(                 ...