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

เขียนโค๊ด 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...

การดึงข้อมูล 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(                 ...