 |
สอบถามการใช้งาน between ในการ search กับการ export ข้อมูลตามเงื่อนใขที่เลือกครับ |
|
 |
|
|
 |
 |
|
ผมมีปัญหาการ search โดยใช้ between search แล้วมันไม่มีข้อมูลขึ้น ข้อมูลที่ผมอยาก search เป็นข้อมูลวันที่ซึ่งผมเก็บใว้ในฐานข้อมูลเป็น varchar ผมเคยเขียน code php search between แต่ทีนี้ไม่ได้ใช้ พอเอามาใช้แล้วมันไม่ search ให้ก็เลยงง ผมเอารูป form, ข้อมูลที่ต้องการ search และ code มาให้ดูด้วยครับ


Code (PHP)
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table>
<tr>
<td><b><font face="arial,tahoma,impact" size="3" color="#3a3a3a">วันที่ : </font></b></td>
<td>
<input name="txtKeyword1" type="text" id="txtKeyword1" value="<?=$_GET["txtKeyword1"];?>" size="10">
<script language="JavaScript">
new tcal ({'formname': 'frmSearch','controlname': 'txtKeyword1'});
</script>
</td>
<td><b><font face="arial,tahoma,impact" size="3" color="#3a3a3a">ถึงวันที่ : </font></b></td>
<td>
<input name="txtKeyword2" type="text" id="txtKeyword2" value="<?=$_GET["txtKeyword2"];?>" size="10">
<script language="JavaScript">
new tcal ({'formname': 'frmSearch','controlname': 'txtKeyword2'});
</script>
</td>
<td><b><font face="arial,tahoma,impact" size="3" color="#3a3a3a">หมายเลขรายการ : </font></b></td>
<td><input name="txtKeyword3" type="text" id="txtKeyword3" value="<?=$_GET["txtKeyword3"];?>" size="10"></td>
<td><b><font face="arial,tahoma,impact" size="3" color="#3a3a3a">ชื่อรายการ : </font></b></td>
<td><input name="txtKeyword4" type="text" id="txtKeyword4" value="<?=$_GET["txtKeyword4"];?>" size="10"></td>
<td><b><font face="arial,tahoma,impact" size="3" color="#3a3a3a">ชื่อผู้เบิก : </font></b></td>
<td><input name="txtKeyword5" type="text" id="txtKeyword5" value="<?=$_GET["txtKeyword5"];?>" size="10"></td>
<td>
<input type="submit" name="search" id="search" value="ค้นหา">
</td>
</tr>
</table>
</form>
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("cs");
$strSQL = "SELECT * FROM data_order_1 WHERE 1";
if($_GET["txtKeyword1"] != "" or $_GET["txtKeyword2"] != "" or $_GET["txtKeyword3"] != "" or $_GET["txtKeyword4"] != "" or $_GET["txtKeyword5"] != "" ){
//$strSQL .= " and (Date LIKE '%".$_GET["txtKeyword1"]."%' and Number LIKE '%".$_GET["txtKeyword2"]."%' and Name2 LIKE '%".$_GET["txtKeyword3"]."%' and Name1 LIKE '%".$_GET["txtKeyword4"]."%' ) ";
$strSQL .= " and (Date between '%".$_GET["txtKeyword1"]."%' and '%".$_GET["txtKeyword2"]."%' ) ";
}
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
ส่วนเรื่องการ export ผมเคยโพสถามการ export ใว้ แต่ไม่ได้เอา form การ export แนบใว้ให้ ผมเลยจะถามว่าถ้าผมใช้ form search แบบด้านบนมาเป็น form ในการ export ด้วยผมต้องเขียน get ข้อมูลแบบไหน code export ผมใช้ตัวนี้อยู่ ยังไงรบกวนด้วยครับ ขอบคุณครับ
Code (PHP)
<?ob_start();?>
รายการเบิก/รับ สินค้าตัวอย่าง
<?
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="export data order 1.csv"');
$host="localhost";
$username="root";
$password="1234";
$db="cs";
$tb="data_order_1";
mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
echo "วันที่,หมายเลขรายการ,รหัสพนักงาน,ชื่อพนักงาน,หน่วยงาน,รหัสสินค้า,ชื่อสินค้า,ขนาด,ราคา,จำนวนเบิก,ราคารวม,จำนวนรับ,คงเหลือ,ลักษณะการใช้งาน,Packing,วิธีการขนส่ง,ผู้จัดทำ,ผู้รับ,วันที่ต้องการ,วันที่่ส่งจริง, \n";
$sql = "select * from $tb";
$dbquery = mysql_query($sql);
$num_rows = mysql_num_rows($dbquery);
$i=0;
while ($i < $num_rows){
$result= mysql_fetch_array($dbquery);
echo "$result[Date],$result[Number],$result[Code1],$result[Name1],$result[Section],$result[Code2],$result[Name2],$result[Size],$result[Price],$result[Num1],$result[PT],$result[Num2],$result[Remain],$result[Uses],$result[Packing],$result[Shipping],$result[Sign],$result[Address],$result[Datew],$result[Datew2], \n";
$i++;
}
?>
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2012-10-22 09:35:30 |
By :
prapakorn |
View :
4065 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้ search between ใช้ได้แล้ว แต่ search ช่องอื่นใช้ไม่ได้ รบกวนช่วยดูให้หน่อยครับ
Code (PHP)
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("cs");
$strSQL = "SELECT * FROM data_order_1 WHERE 1";
if($_GET["txtKeyword1"] != "" or $_GET["txtKeyword2"] != "" or $_GET["txtKeyword3"] != "" or $_GET["txtKeyword4"] != "" or $_GET["txtKeyword5"] != "" ){
$strSQL .= " and (Date between '".$_GET["txtKeyword1"]."' and '".$_GET["txtKeyword2"]."') or (Number LIKE '".$_GET["txtKeyword3"]."' and Name2 LIKE '".$_GET["txtKeyword4"]."' and Name1 LIKE '".$_GET["txtKeyword5"]."' ) ";
//$strSQL .= " and (Date between '".$_GET["txtKeyword1"]."' and '".$_GET["txtKeyword2"]."' ) ";
}
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
|
 |
 |
 |
 |
Date :
2012-10-22 22:44:59 |
By :
prapakorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัว search ใช้ได้หมดแล้ว ผมเอา code ใว้ให้ดูเผื่อใครจะเอาไปใช้
Code (PHP)
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("cs");
$strSQL = "SELECT * FROM data_order_1 WHERE 1";
if($_GET["txtKeyword1"] != "" or $_GET["txtKeyword2"] != "" or $_GET["txtKeyword3"] != "" or $_GET["txtKeyword4"] != "" or $_GET["txtKeyword5"] != "" ){
if($_GET["txtKeyword1"] != "" or $_GET["txtKeyword2"] != ""){
$strSQL .= " and (Date between '".$_GET["txtKeyword1"]."' and '".$_GET["txtKeyword2"]."') ";
}
if($_GET["txtKeyword3"] != ""){
$strSQL .= " and Number LIKE '%".$_GET["txtKeyword3"]."%' ";
}
if($_GET["txtKeyword4"] != ""){
$strSQL .= " and Name2 LIKE '%".$_GET["txtKeyword4"]."%' ";
}
if($_GET["txtKeyword5"] != ""){
$strSQL .= " and Name1 LIKE '%".$_GET["txtKeyword5"]."%' ";
}
}
ส่วนเรื่องการ export ข้อมูล
ผมก็ยังต้องการอยู่นะครับ ยังไงรบกวนช่วยชี้แนะการ export จากเงื่อนใขตามการ search ให้ด้วยนะครับ ขอบคุณครับ(ใกล้เสร็จแล้ว)
|
 |
 |
 |
 |
Date :
2012-10-22 23:02:45 |
By :
prapakorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใน mySQL ใช้ Format แบบนั้นไม่ถูกครับ จะต้องเป็น YYYY-MM-DD 
|
 |
 |
 |
 |
Date :
2012-10-23 07:31:23 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่วิน มีบทความเกี่ยวกับการ export ตามเงื่อนใขไหมครับ ถ้ายังไงรบกวนช่วยดูการ export ให้หน่อยครับเหลืออยอยู่อย่างเดียวแล้วตอนนี้ ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2012-10-23 20:46:45 |
By :
prapakorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันอยู่ทีการเขียน SQL ครับ มันก็ไม่ได้ยากอะไรมากมายน่ะครับ 
|
 |
 |
 |
 |
Date :
2012-10-24 08:36:56 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยดูโค้ชให้หน่อยค่ะ มันสามารถค้นหาตามห้องได้ แต่ต้องการจะให้ค้นหาในช่วงระหว่างวันที่ด้วยจะต้องแก้ไขอย่างไรบ้างค่ะ
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="frmSearch" method="get" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table width="627" border="0">
<tr>
<input type="text" name="date" id="date">ถึงวันที่<input type="text" name="date" id="date">
<?
$sqlname="SELECT * FROM atten WHERE date BETWEEN 'date' AND 'date'";
$results=mysql_query($sqlname);
?>
<td width="271" align="right"><span class="style4"><strong>ห้อง</strong></span></td>
<td width="346"><select name="txtKeyword">
<option value="<?php echo $_GET["txtKeyword"];?>">--เลือก--</option>
<?
$result = mysql_query("SELECT * FROM class");
while($row = mysql_fetch_array($result)){
?><option value="<? echo $row['class_id'] ?>"><? echo $row['class_name'] ?></option>
<? } ?>
</select>
<input type="submit" value="Search"></td>
</tr>
</table>
</form>
<br>
<?php
if($_GET["txtKeyword"] != "")
{
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("web");
$strSQL = "SELECT * FROM atten WHERE (id LIKE '%".$_GET["txtKeyword"]."%' )";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="800" border="0">
<tr bgcolor="#0099FF">
<th width="91"> <div align="center">วันที่ </div></th>
<th width="91"> <div align="center">รหัส </div></th>
<th width="98"> <div align="center">ชื่อ </div></th>
<th width="98"> <div align="center">นามสกุล </div></th>
<th width="98"> <div align="center">สถานะ </div></th>
</tr>
<?php
while($arr = mysql_fetch_array($objQuery)){
?>
<tr bgcolor="#FFFFFF">
<td><input name="date[]" type="text" id="date" size="9" value="<?=$arr[date]?>"></td>
<td><input name="stu_id[]" type="text" id="stu_id" size="9" value="<?=$arr[id]?>"></td>
<td><input name="stu_name[]" type="text" id="stu_name" value="<?=$arr[name]?>"></td>
<td><input name="stu_lname[]" type="text" id="stu_lname" value="<?=$arr[lname]?>"></td>
<td><input name="room_id[]" type="text" id="room_id" size="10" value="<?=$arr[status_id]?>"></td>
</tr>
<?php } ?>
</table>
<?php
mysql_close($objConnect);
}
?>
</td>
</body></html>
|
 |
 |
 |
 |
Date :
2014-11-04 23:06:19 |
By :
mimm |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|