ตัวอย่างต่อไปนี้เป็นแนวทางอย่างง่าย สำหรับการดึงข้อมูลมาแสดง
และสร้าง qrcode จากข้อมูลนั้นๆ
ตัวอย่าง
| Province | qrcode |
|---|---|
| กรุงเทพมหานคร |
|
| กระบี่ |
|
| กาญจนบุรี |
|
ดาวน์โหลด phpqrcode ได้ที่
หลังจากโหลดเสร็จแตกไฟล์แล้ว copy โฟลเดอร์
phpqrcode ทั้งหมดไว้ใช้งาน
สร้างไฟล์ gen_qrcode.php แล้วใส่โค้ดอย่างง่ายดังนี้
<?php
include('phpqrcode/qrlib.php');
QRcode::png($_GET['w']);
?>
สร้างไฟล์ test_db_qrcode.php แล้วเขียนโค้ดตามแนวทางด้านล่าง
<?php
header("Content-type:text/html; charset=UTF-8");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// เชื่อมต่อกับฐานข้อมูล
$link=mysql_connect("localhost","root","test"); // เชื่อมต่อ Server
mysql_select_db("test"); // ติดต่อฐานข้อมูล
mysql_query("set character set utf8"); // กำหนดค่า character set ที่จะใช้แสดงผล
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</head>
<body>
<br>
<div style="margin:auto;width:80%;">
<table class="table table-bordered">
<tr class="active">
<th>Province</th>
<th>qrcode</th>
</tr>
<?php
$q="
SELECT * FROM province_th LIMIT 3
";
$qr=mysql_query($q);
while($rs=mysql_fetch_assoc($qr)){
?>
<tr>
<td>
<?=$rs['province_name']?>
</td>
<td>
<img src="gen_qrcode.php?w=<?=$rs['province_name']?>" alt="">
</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
โดยจากตัวอย่างจะเป้นการไปดึงข้อมูลจังหวัดมา 3 รายการ แล้ววนลูปแสดงข้อมูล
พร้อมแสดงรูป qrcode ของแต่ละค่าชื่อ จังหวัด
เราสามารถนำไปประยุกต์ต่างๆ เพิ่มเติมได้มากมาย ตัวอย่างข้างต้นเป็แแนวทาง
ดูการใช้งาน และการปรับแต่งต่างๆ ได้ที่