|
 |
|
เก็บข้อมูลการใช้รถยนต์ ต้องการให้แสดงรถทุกคันออกมา แล้วเช็คว่าคันไหนมีการใช้งานบ้าง ให้ checkbox ไว้ |
|
 |
|
|
 |
 |
|
ตารางเก็บรถยนต์

ตารางเก็บรายละเอียดการใช้

อยากให้แสดงรถทุกคันออกมา ถ้าคันไหนมีการใช้งานให้ checkbox แบบภาพล่างนี้ ต้องเขียนโค้ดยังไงครับ

AjaxPHPSearchRecord1
<?php
/*** By Weerachai Nukitram***/
/*** http://www.ThaiCreate.Com ***/
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(Search) {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'AjaxPHPSearchRecord2.php';
var pmeters = 'mySearch='+Search;
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
</script>
<body Onload="JavaScript:doCallAjax('');">
<h1>My Customer</h1>
<form name="frmMain">
Search <input type="text" name="txtSearch" id="txtSearch">
<input type="button" name="btnSearch" id="btnSearch" value="Search" OnClick="JavaScript:doCallAjax(document.getElementById('txtSearch').value);">
<br><br>
<span id="mySpan"></span>
</form>
</body>
</html>
AjaxPHPSearchRecord2
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
$strSearch = $_POST["mySearch"];
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("test");
mysql_query("SET NAMES UTF8");
$strSQL = "SELECT * FROM tb_car";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">ID</div></th>
<th width="98"> <div align="center">Car Name</div></th>
<th width="198"> <div align="center">Car Use</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
$car_id = $objResult['car_id'];
$car_name = $objResult['car_name'];
$strSQL2 = "SELECT * FROM tb_car_use";
$objQuery2 = mysql_query($strSQL2);
while($objResult2 = mysql_fetch_array($objQuery2))
{
$id = $objResult2["id"];
$car_name2 = $objResult2["car_name"];
//}
?>
<tr>
<td><div align="center"><?=$id;?></div></td>
<td><div align="center"><?=$car_name2;?></div></td>
<?
if($car_name2 == $car_name){
echo "<td align=\"center\"><input type=\"checkbox\" checked></td>";
}else{
echo "<td align=\"center\"><input type=\"checkbox\"></td>";
}
echo "</tr>";
}
}
?>
</table>
<?
mysql_close($objConnect);
?>
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2014-06-21 18:22:18 |
By :
giverplus |
View :
993 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
น่าจะราว ๆ นี้นะครับ โค้ดสดนะครับ ไม่ได้เทส
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
$strSearch = $_POST["mySearch"];
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("test");
mysql_query("SET NAMES UTF8");
$strSQL = "SELECT * FROM tb_car";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">ID</div></th>
<th width="98"> <div align="center">Car Name</div></th>
<th width="198"> <div align="center">Car Use</div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
$car_id = $objResult['car_id'];
$car_name = $objResult['car_name'];
$strSQL2 = "SELECT * FROM tb_car_use";
$objQuery2 = mysql_query($strSQL2);
$objResult2 = mysql_fetch_array($objQuery2);
if ($car_name==$objResult2["car_name"]) {
$sl = "checked";
}else{
$sl="";
}
?>
<tr>
<td><div align="center"><?=$id;?></div></td>
<td><div align="center"><?=$car_name;?></div></td>
<td><div align="center"><input type="checkbox" <?=$sl?>></div></td>
</tr>
<?php
}
?>
</table>
<?
mysql_close($objConnect);
?>
|
ประวัติการแก้ไข 2014-06-21 18:55:40
 |
 |
 |
 |
Date :
2014-06-21 18:53:02 |
By :
deawx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โม้จากโค้ดคุณเลยละกัน
Code (PHP)
while($objResult = mysql_fetch_array($objQuery))
{
$car_id = $objResult['car_id'];
$car_name = $objResult['car_name'];
$strSQL2 = "SELECT * FROM tb_car_use where car_name='$car_name' ";
$objQuery2 =mysql_query($strSQL2) or die(mysql_error());
?>
<tr>
<td><div align="center"><?=$car_id;?></div></td>
<td><div align="center"><?=$car_name;?></div></td>
<?
if(mysql_num_rows($objQuery2)>0 ){
echo "<td align=\"center\"><input type=\"checkbox\" checked></td>";
}else{
echo "<td align=\"center\"><input type=\"checkbox\"></td>";
}
echo "</tr>";
}
|
ประวัติการแก้ไข 2014-06-21 19:01:57
 |
 |
 |
 |
Date :
2014-06-21 19:01:22 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณทั้ง 2 ท่านมากครับ ไปต่อได้ซะที
|
 |
 |
 |
 |
Date :
2014-06-21 19:08:54 |
By :
giverplus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|