รบดู code ค้นหาให้หน่อยค่ะมันผิดยังไงค่ะ พอใส่ข้อมูลแล้ว โชว์ข้อมูลทั้งหมดไม่ได้โชว์ที่ต้องการค้นหาค่ะ
Code (PHP)
<? include "../chksession.php"; ?>
<? include "../include/connect.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/template.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>ระบบงานครุภัณฑ์ โครงการชลประทานสงขลา</title>
<!-- InstanceEndEditable -->
<link href="../css_style.css" rel="stylesheet" type="text/css" />
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>
<!-- InstanceBeginEditable name="head1" -->
<script language="javascript">
function popup(username){
var url="regis_user.php?username="+username;
window.open(url,'','width=350,height=250,left=80,right=80,top=80');//v2.0
}
</script>
<script language="javascript">
function popup1(username){
var url="edit_user.php?username="+username;
window.open(url,'','width=350,height=250,left=80,right=80,top=80');//v2.0
}
</script>
<!-- InstanceEndEditable -->
<body>
<table width="850" height="292" border="0" align="center" cellpadding="0" cellspacing="0" class="table_big">
<tr>
<td height="150" align="left" valign="top"><img src="../image/header.jpg" width="854" height="150" /></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="108" valign="top"><!-- InstanceBeginEditable name="EditRegion3" -->
<table width="850" height="319" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"><table width="845" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30"><? include ("menu_admin.php");?></td>
</tr>
</table>
<TABLE cellSpacing=0 cellPadding=0 width=700
align=center>
<TBODY>
<TR valign="top">
<TD><form name="form1" method="post" action="">
<div align="center">
<table width="300" border="1" align="center" cellpadding="0" cellspacing="0">
<tr bordercolor="#A10200">
<td width="300" bordercolor="" bgcolor="#FFFFFF"><table width="300" align="center" bgcolor="#FFFFFF">
<tr>
<td height="16" valign="top" bgcolor="#F0ECEB"><table width="780" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#F0ECEB" bgcolor="#F0ECEB">
<tr>
<td><table width="775" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#A0A7DE">
<tr bordercolor="#A0A7DE" bgcolor="#F0ECEB"> </tr>
<tr bordercolor="#A0A7DE" bgcolor="#F0ECEB"> </tr>
</table></td>
</tr>
<td height="71"><table width="780" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#F0ECEB" bgcolor="#E8E8E8">
<td height="46">
<? if(isset($_POST["Search"]) && $_POST["Search"] =="Search"){
//$TxtSearch=trim($TxtSearch);
$_POST["TxtSearch"] = trim($_POST["TxtSearch"]);
if(isset($_POST["TxtSearch"])){
$sql="SELECT * FROM tb_login WHERE ((name LIKE '%{$_POST["TxtSearch"]}%') or (lastname LIKE '%{$_POST["TxtSearch"]}%') or (class LIKE '%{$_POST["TxtSearch"]}%')) ORDER BY id ASC;";
$result=mysql_query($sql) or die(mysql_error());
$rowsOfPage=mysql_num_rows($result);
$sql="SELECT * FROM tb_login WHERE ((name LIKE '%{$_POST["TxtSearch"]}%') or (lastname LIKE '%{$_POST["TxtSearch"]}%') or (class LIKE '%{$_POST["TxtSearch"]}%')) ORDER BY id ASC LIMIT ".(($page-1)*20).",20";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_num_rows($result);
}
} else{
if($page==null)$page=1;
$sql="SELECT * FROM tb_login";
$result=mysql_query($sql) or die(mysql_error());
$rowsOfPage=mysql_num_rows($result);
$sql="SELECT * FROM tb_login LIMIT ".(($page-1)*20).",20";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_num_rows($result);
}
?>
<table width="780" border="1" align="center" bordercolor="#F0ECEB">
<td bgcolor="#F0ECEB">
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#F0ECEB"><div align="right"><font color="#000000" size="2" face="MS Sans Serif, Tahoma, sans-serif" class="content">ค้นหาจาก(ชื่อ-สกุล-ระดับผู้ใช้)</font><span class="style14">::</span>
<!--<select name="select">
<option value="name">ชื่อ</option>
<option value="lastname">สกุล</option>
<option value="class" selected="selected">ระดับผู้ใช้</option>
</select>-->
<input name="TxtSearch" type="text" id="TxtSearch" value="<? if(isset($_POST["TxtSearch"])) echo $_POST["TxtSearch"];?>" onClick="javascript:txt.value='';">
<input name="Submit" type="submit" id="Submit" value="ค้นหา">
<input name="Search" type="hidden" id="Search" value="search">
</div></td>
</tr>
<tr>
<td bgcolor="#F0ECEB"><div align="right">
<? if($_POST["TxtSearch"] !=""){ ?>
<font face="MS Sans Serif, Tahoma, sans-serif" color="#990000" size="2">ผลการสืบค้นมีทั้งหมด <? echo $rows;?> รายการ</font>
<? } else{?>
<? }?>
</div>
<table width="800" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#F0ECEB">
<tr bgcolor="#3366FF">
<td width="808" height="133"><table width="752" height="130" border="1" align="center" cellpadding="1" cellspacing="1" bordercolor="#3366FF" bgcolor="#F0ECEB">
<tr bgcolor="#3366FF">
<td height="23" colspan="7" align="center" bgcolor="#F0ECEB"><table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="125" align="left" valign="top" bgcolor="#F0ECEB"><font color="#0000FF" size="2" face="MS Sans Serif, Tahoma, sans-serif"><a href="#" onClick="popup('<? echo $arr_data["username"];?>');"><img src="../image/insert_mem2.jpg" width="125" height="30" border="0"></a><br></font></td>
<td width="675" align="center" bgcolor="#A10200"> </td>
</tr>
</table></td>
</tr>
<tr bgcolor="#3366FF">
<td width="7%" height="23" align="center" bgcolor="#A10200"><font size="2" face="MS Sans Serif, Tahoma, sans-serif" color="#FFFFFF" >ลำดับที่</font></td>
<td width="45%" align="center" bgcolor="#A10200"><span class="style1"><font size="2" face="MS Sans Serif, Tahoma, sans-serif" color="#FFFFFF">ชื่อ - สกุล </font></span></td>
<td width="27%" align="center" bgcolor="#A10200"><font size="2" face="MS Sans Serif, Tahoma, sans-serif" color="#FFFFFF">ระดับผู้ใช้</font></td>
<td width="11%" align="center" bgcolor="#A10200"><font size="2" face="MS Sans Serif, Tahoma, sans-serif" color="#FFFFFF">แก้ไข</font></td>
<td width="10%" align="center" bgcolor="#A10200"><font size="2" face="MS Sans Serif, Tahoma, sans-serif" color="#FFFFFF">ลบ</font></td>
</tr>
<?
//$result1=mysql_query("SELECT * FROM tb_login ORDER BY class ASC");
//while($data=mysql_fetch_array($result1)){ $i++;
for($i=0;$i<$rows;$i++){
$data=mysql_fetch_array($result);
/*for($i=0;$i<$rows;$i++){
$id=mysql_result($result,$i,"id");
$username=mysql_result($result,$i,"username");
$password=mysql_result($result,$i,"password");
$name=mysql_result($result,$i,"name");
$lastname=mysql_result($result,$i,"lastname");
$class=mysql_result($result,$i,"class");*/
?>
<?
switch ($data["class"]){
case admin:
$data["class"]="ผู้ดูแลระบบ";
break;
case staff:
$data["class"]="เจ้าหน้าที่พัสดุ";
break;
case ceo:
$data["class"]="ผู้บริหาร";
break;
case user:
$data["class"]="เจ้าหน้าที่ฝ่าย";
break;
}?>
<tr bgcolor="<? if($i%2==0)echo "#FFECFF"; else echo "#FFFFFF";?>">
<td align="left" bgcolor="#F0ECEB"><div align="center"><font face="MS Sans Serif, Tahoma, sans-serif" size="2" color="#990000"> <? echo $i+1+($page-1)*20;?></font></div></td>
<td align="left" bgcolor="#FFFFFF"><div align="left"><font face="MS Sans Serif, Tahoma, sans-serif" size="2" color="#990000"> <? echo $data["name"];?> <? echo $data["lastname"];?></font></div></td>
<td align="left" bgcolor="#FFFFFF"><div align="left"><font face="MS Sans Serif, Tahoma, sans-serif" size="2" color="#990000"> </font><font face="MS Sans Serif, Tahoma, sans-serif" size="2" color="#990000"><? echo $data['class'];?></font></div></td>
<td align="left" bgcolor="#FFFFFF"><div align="center"><a href="#" onClick="popup1('<? echo $data["username"];?>');"><img src="../image/edit.png" alt="แก้ไขข้อมูล" width="20" height="20" border="0" /></a></div></td>
<td align="left" bgcolor="#FFFFFF"><div align="center"><a href="delete_user.php?id=<? echo $data["id"];?>" onClick="return confirm('ต้องการลบ <? echo $data["name"];?> หรือไม่')"><img src="../image/delete.gif" alt="ลบรายการ" width="20" height="20" border="0"></a></div></td>
</tr>
<? }?>
</table></td>
</tr>
</table></td>
</tr>
</table>
<div align="center" class="content2">หน้า <?
if($rowsOfPage%20>0)$p=1; else $p=0;
for($i=1;$i<=$rowsOfPage/20+$p;$i++)echo "<a href=?page=$i&Search={$_POST["Search"]}&TxtSearch={$_POST["TxtSearch"]}> |$i|</a> ";
?>
</strong></div></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td></td>
</tr>
</table></td>
</tr>
</table>
<br>
</div>
</form></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR><td height="2"></TBODY></TABLE>
<!-- InstanceEndEditable --></td>
</tr>
<tr>
<td height="30" align="center" bgcolor="#A20300"><?php
include ("../include/copyright.php");
echo "<center><font size='2' face='MS Sans Serif' color='#ffffff'>$echo</font></center>" ;
echo "<center><font size='2' face='MS Sans Serif' color='#ffffff'>$echo2</font></center>" ;
?></td>
</tr>
</table>
</body>
<!-- InstanceEnd --></html>
รูปเมื่่อค้นหา
Tag : PHP, MySQL
Date :
2014-03-09 20:12:43
By :
nungna11
View :
729
Reply :
6
แก้ บรรทัดนี้ครับ
<? if(isset($_POST["Search"]) && $_POST["Search"] =="Search"){
เป็น
<? if(isset($_POST["TxtSearch"])) {
Date :
2014-03-09 21:06:48
By :
mm2mail
ไม่ได้ค่ะ error ค่ะ
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20,20' at line 1
Date :
2014-03-09 21:28:35
By :
nungna11
น่าจะผิดที่ตรงนี้ครับ
(($page-1)*20).",20"
ให้ดูค่า $page ครับ ว่าตอนนี้ได้มาเป็นอะไร
ตอนนี้น่าจะได้เป็น 0 มามันเลยเลือกแถวข้อมูล ตำแหน่งที่ -20 มา 20 แถวไม่ได้
Date :
2014-03-09 22:14:20
By :
mm2mail
ให้ย้าย บรรทัดนี่้
if($page==null)$page=1;
ไว้ด้านบนบรรทัดที่แก้เป็น
<? if(isset($_POST["TxtSearch"])) {
จะได้เป็น
<?
if($page==null)$page=1;
if(isset($_POST["TxtSearch"])) {
ลองดูนะ
Date :
2014-03-09 22:16:24
By :
mm2mail
ได้แล้วค่ะ ขอบคุณมากค่ะ
Date :
2014-03-10 09:37:41
By :
nungna11
ไม่เป็นไรครับ
Date :
2014-03-10 14:10:53
By :
mm2mail
Load balance : Server 03