|
 |
|
เรียกข้อมูลจากฐานข้อมูลมาแสดง แล้วค่อยค้นหาข้อมูลที่ต้องการ select & search |
|
 |
|
|
 |
 |
|
จะไม่ลองพยายาม แก้ หรือ ประยุกต์ เองบ้างเลยหรอ เห็นติดนิดนึง ก็โพสถามแล้ว
ลองทดลองเขียนไปเรื่อยๆ ก่อนดีไหมค่ะ 
|
 |
 |
 |
 |
Date :
2014-02-27 11:32:54 |
By :
Master Melon |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
if($_GET["txtKeyword"] != ""){
$condition = " Name LIKE '%".$_GET["txtKeyword"]."%' or Email LIKE '%".$_GET["txtKeyword"]."%' ";
}else{
$condition = " 1 ";
}
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
// Search By Name or Email
$strSQL = "SELECT * FROM customer WHERE $condition ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
}
?>
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-02-27 11:55:49 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อธิบาย : ตัดส่วนเงื่อนไขของ if($_GET["txtKeyword"] != "") ออกครับ
if($_GET["txtKeyword"] != "") แปลว่า ถ้า txtKeyword ที่รับค่าจาก GET ไม่เท่ากับค่าว่างถึงจะแสดงผล เพราะฉะนั้นตัดออกจะแสดงผลทันทีครับ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
// Search By Name or Email
$strSQL = "SELECT * FROM customer WHERE (Name LIKE '%".$_GET["txtKeyword"]."%' or Email LIKE '%".$_GET["txtKeyword"]."%' )";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-02-27 11:59:21 |
By :
Ex-[S]i[L]e[N]t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าเรามีจินตนาการเหมือนกับเราจินตนาการเวลาดูหนัง AV  มันจะง่ายมากตาม คห. ด้านบนเลยครับ
แนะนำอีกอย่างนะครับ ในส่วนของ Connect DB ใช้แบบ include ไฟล์ดีกว่าครับ สะดวกในการแก้ไข และไม่เป็นภาระกับลูกหลานในภายภาคหน้า
Code (PHP)
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
|
ประวัติการแก้ไข 2014-02-27 12:03:12
 |
 |
 |
 |
Date :
2014-02-27 12:00:24 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาปีกกาปิดที่บรรทัด 54 ออกครับ
54.}
|
 |
 |
 |
 |
Date :
2014-02-27 12:06:07 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากคัฟ
|
 |
 |
 |
 |
Date :
2014-02-27 12:07:43 |
By :
kunnawut |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|