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

เขียนโค๊ด PHP แสดงคำตอบจากแบบสอบถามลงใน Radio Button

สำหรับตัวอย่างนี้ จะเป็นการเขียนโค๊ดดึงข้อมูลมาแสดง โดยจะใช้ radio button เพื่อแสดงค่าที่มีการตอบแบบสอบถามไว้ โดยการเช็คคะแนนความพึงพอใจ เมื่อตรงกับช่องไหน ก็ให้ทำเครื่องหมายที่ช่องนั้นๆ

เขียนโค๊ด PHP แสดงรายการคำตอบจากแบบสอบถามลงใน Radio Button



วิดีโอแสดงขั้นตอนการเขียนโปรแกรม PHP เพื่อเรียกรายงานแบบสอบถาม


ซอร์สโค๊ดตัวอย่าง

<style>
#question td{ text-align : center;}
</style>
<h2>รายงานแบบสอบถาม กับการแสดงคำตอบแบบตัวเลือกด้วย Radio Button</h2>
<?php
$form_id = 3;
$db = new dbConnect();
$sql = "SELECT * FROM tb_question_form WHERE qfrm_id = $form_id";
$qry = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($qry);
echo '<h3>',$row['qfrm_title'],'</h3>';
?>
<table id="question" border="1">
<thead>
<tr>
<th>ลำดับ</th>
<th width="300">รายการ</th>
<th width="70">ดีมาก</th>
<th width="70">ดี</th>
<th width="70">พอใช้</th>
<th width="70">น้อย</th>
<th width="70">น้อยที่สุด</th>
</tr>
</thead>
<tbody>
<?php
$no=0;
$sql = "SELECT * FROM  `tb_question_list`
INNER JOIN tb_question_answer ON `tb_question_list`.qtn_id = tb_question_answer.ans_qtn_ref
WHERE qtn_form_id = $form_id ";
$qry = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($qry)){
$no++;
$title = $row['qtn_title'];
$id = $row['qtn_id'];
$check1="";
$check2="";
$check3="";
$check4="";
$check5="";
if($row['ans_qtn_value']==5){
$check1 = "checked=checked";
}elseif($row['ans_qtn_value']==4){
$check2 = "checked=checked";
}elseif($row['ans_qtn_value']==3){
$check3 = "checked=checked";
}elseif($row['ans_qtn_value']==2){
$check4 = "checked=checked";
}elseif($row['ans_qtn_value']==1){
$check5 = "checked=checked";
}
echo <<<DOC
<tr>
<td>$no</td>
<td>$title</td>
<td><input type="radio" name="choice$id" $check1/></td>
<td><input type="radio" name="choice$id" $check2 /></td>
<td><input type="radio" name="choice$id" $check3 /></td>
<td><input type="radio" name="choice$id" $check4 /></td>
<td><input type="radio" name="choice$id" $check5 /></td>
</tr>
DOC;
}
?>
<!--
<tr>
<td>2</td>
<td>รายการที่ 2</td>
<td><input type="radio" name="choice2" /></td>
<td><input type="radio" name="choice2" /></td>
<td><input type="radio" name="choice2" /></td>
<td><input type="radio" name="choice2" /></td>
<td><input type="radio" name="choice2" /></td>
</tr>
<tr>
<td>3</td>
<td>รายการที่ 3</td>
<td><input type="radio" name="choice3"  /></td>
<td><input type="radio" name="choice3"  /></td>
<td><input type="radio" name="choice3"  /></td>
<td><input type="radio" name="choice3"  /></td>
<td><input type="radio" name="choice3"  /></td>
</tr>
<tr>
<td>4</td>
<td>รายการที่ 4</td>
<td><input type="radio" name="choice4"  /></td>
<td><input type="radio" name="choice4"  /></td>
<td><input type="radio" name="choice4"  /></td>
<td><input type="radio" name="choice4"  /></td>
<td><input type="radio" name="choice4"  /></td>
</tr>
<tr>
<td>5</td>
<td>รายการที่ 5</td>
<td><input type="radio"  name="choice5" /></td>
<td><input type="radio"  name="choice5" /></td>
<td><input type="radio"  name="choice5" /></td>
<td><input type="radio"  name="choice5" /></td>
<td><input type="radio"  name="choice5" /></td>
</tr>
-->
</tbody>
</table>




"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) ม...