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

เขียนโค๊ด 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 นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สร้างแบบฟอร์มล็อกอิน HTML + PHP

       ผมจะสร้างระบบตัวอย่างเพื่อแสดงให้เห็นการทำงานของสคริปต์ PHP ที่ใช้ในการล็อกอินอย่างง่ายๆ นะครับ โดยที่ผมจะสร้างโฟลเดอร์ใหม่ขึ้นในไดร์ฟที่เราได้กำหนดให้เป็น Directory Root สำหรับรันสคริปต์ PHP ซึ่งผมกำหนดเอาไว้ที่ D:\www ดังนั้นผมจะสร้างโฟลเดอร์ teacher_assistant ขึ้นมาใหม่จะได้พาธเป็น D:\www\teacher_assistant สำหรับการเข้าถึง จะใช้ URL ดังนี้ http://localhost/teacher_assistant หลังจากสร้างโฟลเดอร์เสร็จเรียบร้อยก็จะเริ่มกระบวนการเตรียมโครงสร้างโปรเจ็กต์ด้วยเครื่องมือช่วยเขียนโปรแกรมชื่อว่า eclipse ซึ่งดูวิธีการสร้างโปรเจ็กต์ได้จากบทความ เริ่มต้นสร้างโปรเจ็กต์ ด้วยโปรแกรม eclipse เครื่องมือเขียนโปรแกรม php ในบทความเป็นการสร้างโปรเจ็กต์ที่มีพาธเป็นโฟลเดอร์ login แต่ในที่นี้ต้องเป็นพาธให้ตรงกับโฟลเดอร์ใหม่ที่เราได้สร้างขึ้นด้วยนะครับ

PHP กับการคิดส่วนลดเป็นเปอร์เซ็น 5%, 10%, 20% ตามช่วงราคาที่กำหนด

<?php     $total_price = 1000;     $discount = 0;         if($total_price >= 500 && $total_price < 1001){         $discount = 5;     }elseif($total_price >= 1001 && $total_price <= 5000){         $discount = 10;     }elseif($total_price >= 5001){         $discount = 20;     }         $discount_bath = ($total_price*$discount)/100; ?> <pre> ซื้อสินค้าครบ 0 ถึง 499 บาท ไม่ได้ส่วนลด ซื้อสินค้าครบ 500 ถึง 1000 บาท ได้ส่วนลด 5% ซื้อสินค้าครบ 1001 ถึง 5000บาท ได้ส่วนลด 10% ซื้อสินค้าครบ 5001 บาทขึ้นไป ได้ส่วนลด 20% </pre> <h3>รวมราคาสินค้า = <?php echo number_format($total_price,2);?></h3> <h5>ส่วนลด = <?php echo $discount;?>%  (<?php echo $discount_bath;?> บาท)</h5> <h2>ราคาหลังส่วนลด = <?php echo number_format($total_price - $discount_bath,2);?>บาท</h2> "PHP ไม่ได้สร้างสุดยอดโปรแกรม แต่ PHP ช่วยให้งานคุณง่ายขึ้นต