ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: icenobu ที่ 21 เมษายน 2011, 15:14:04



หัวข้อ: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: icenobu ที่ 21 เมษายน 2011, 15:14:04
มีคำถามอยู่ 3 ข้อ
1. ตรงจุดค้นหาต้องการจะค้นหา section_detail แต่มันไปค้นหาที่ title อ่ะ พยายามแก้แล้วแต่ไม่ได้อ่ะค่ะ
2. ต้องการจะเพิ่มในส่วนของการค้นหาแบบที่มีตารางวันที่ให้เลือกจะสามารถทำได้อย่างไร ลองใส่แล้วก็ไม่ได้
3. อยากให้ข้อมูลที่ออกมาสามารถ print ออกมาได้ ตรงนี้พอทำได้ในรูปแบบ excel ค่ะ คืออยากได้แบบอื่นมั่งค่ะ สามรถทำอย่างไรได้บ้าง


index.php

โค๊ด:
<?
// Config DB
$db_hostname='localhost';
$db_username='root';
$db_password='root';
$db_name="icenobu";
mysql_connect($db_hostname,$db_username,$db_password) or die("Unable to Connect");
mysql_query("SET NAMES tis620");
mysql_select_db( $db_name );
// End Config DB
?>
<html>
<head>
<title>Search Data</title>
</head>
<body>
<form method="get" action="search.php">
<input type="text" name="keyword">
<select name="emp_id">
<option value="">ทั้งหมด</option>
<?
$sql = "select emp_id, department_id  from cases ";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
$emp_id = $rows["emp_id"];
$title = $rows["title"];
echo "<option value=\"$emp_id\">$department_id</option>";
}
?>
</select>
<input type="submit" value="ค้นหา">
</form>
</body>
</html>

search.php

โค๊ด:
<?
$keyword = $_GET["keyword"];
$emp_id = $_GET["emp_id"];

// Config DB
$db_hostname='localhost';
$db_username='root';
$db_password='root';
$db_name="icenobu";
mysql_connect($db_hostname,$db_username,$db_password) or die("Unable to Connect");
mysql_query("SET NAMES tis620");
mysql_select_db( $db_name );
// End Config DB

?><html>
<head>
<title>Search Data</title>
</head>
<body>
<a href="index.php">กลับหน้าค้นหา</a>
<table width="100%" border="1">
<tr align="center">
<td>รหัสพนักงาน</td>
<td>ชื่อเรื่อง</td>
<td>รายละเอียด</td>
<td>รหัสแผนก</td>
<td>แผนก</td>
</tr>
<?
$sql = "
SELECT
`cases`.`department_id`,
`section`.`section_id`,
`section`.`section_detail`
FROM cases LEFT JOIN section ON (cases.department_id=section.section_id)
";
if($keyword) { $sql .= " and section.section_detail like '%$keyword%'"; }
if($department_id) { $sql .= " and cases.department_id = '$department_id'";}

$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
$i++;
$emp_id = $rows["emp_id"];
$title = $rows["title"];
$section_id = $rows["section_id"];
$description = $rows["description"];
$solution_date = $rows["solution_detail"];
echo "
<tr>
<td>$emp_id</td>
<td>$title</td>
<td>$description</td>
<td>$section_id</td>
<td>$section_detail</td>
</tr>
";
}
?>
</table>
</body>
</html>


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: kobkung ที่ 21 เมษายน 2011, 18:38:39
1. แน่ใจหรือว่าค้นใน title ดูแล้ว title ไม่อยู่ใน case ค้น ผลที่ออกมามันไปเหมือนเองป่าว left join ถูกไหม
2. จะใส่ ปฏิทินไปใน index หรือจะใส่เงื่อนไขเพิ่มใน search.php
3. ออกเป็น pdf หรือ word ก็ได้ ลองดูเรื่อง header


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: icenobu ที่ 21 เมษายน 2011, 19:22:29
1. แน่ใจว่ามันไปค้นใน title ค่ะ ต้องการอยากให้ค้นที่ section_detail ทำไม่ได้จริงๆอ่ะค่ะ ช่วยอธิบายให้หน่อยค่ะ
2. จะใส่ปฏิทินไปใน index ค่ะ
3. มันสั่ง print จากข้อมูลที่โชว์เลยได้ไหมค่ะ โดยไม่ผ่าน pdf หรือโปรแกรมอื่นๆ


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: ohmohm ที่ 21 เมษายน 2011, 20:01:32
ที่ search.php มี SELECT LEFT JOIN ให้ระวัง ถ้าหลัง ON มีเงื่อนไขที่อ้าง columns ที่มาจาก table ที่อยู่หลัง LEFT JOIN แล้วเงื่อนไขนั้นแม้เป็นเท็จ แต่ผลลัพธ์ของ SELECT จะออกมาอยู่ดี โดยแสดงข้อมูลที่เจอจาก table ที่อยู่หน้า LEFT JOIN ส่วน table ที่อยู่หลังที่ไม่มีข้อมูล จะแสดงเป็น NULL แทน

ลองเปลี่ยนเป็น INNER JOIN ไหม


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: icenobu ที่ 22 เมษายน 2011, 08:38:46
อิอิ ดัดได้แล้วค่ะ

โค๊ด:
	select l.emp_id, l.department_id, l.title, t.section_id, l.description, t.section_detail
from `cases` l, section t
where l.department_id = t.section_id

แต่ติดตรงข้อ 2 อีกที่ขอแนวทางใส่การค้นหาแบบปฏิทินทีค่ะ


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: kobkung ที่ 22 เมษายน 2011, 10:15:10
ลองหาพวก javascript มาใส่ ดิ js , jquery ก็ได้  google มีเพียบ


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: icenobu ที่ 22 เมษายน 2011, 10:26:21
อิอิ ได้แล้วค่ะ แต่มีคำถามใหม่มาอีก ที่ว่าใช้คั่ง count(case_id),case_type
จะทำยังไงให้มันโชว์ผลลัพธ์ที่มัน count ไว้อ่ะค่ะ


โค๊ด:
<?
$keyword = $_GET["keyword"];
$emp_id = $_GET["emp_id"];

// Config DB
$db_hostname='localhost';
$db_username='root';
$db_password='root';
$db_name="icenobu";
mysql_connect($db_hostname,$db_username,$db_password) or die("Unable to Connect");
mysql_query("SET NAMES tis620");
mysql_select_db( $db_name );
// End Config DB

?><html>
<head>
<title>Search Data</title>
</head>
<body>
<a href="index.php">กลับหน้าค้นหา</a>
<table width="100%" border="1">
<tr align="center">
<td>รหัสพนักงาน</td>
<td>รหัสแผนก</td>
<td>ชื่อเรื่อง</td>
<td>รายละเอียด</td>
<td>จำนวน</td>
</tr>
<?
$sql = "
SELECT l.emp_id, l.department_id, l.title, t.section_id, l.description, t.section_detail, department_id,count(case_id),case_type
FROM `cases` l, section t
WHERE l.department_id = t.section_id
GROUP BY department_id,case_type
";
if($keyword) { $sql .= " and l.title like '%$keyword%'"; }
if($section_detail) { $sql .= " and t.section_detail = '$section_detail'";}

$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
$i++;
$emp_id = $rows["emp_id"];
$section_detail = $rows["section_detail"];
$title = $rows["title"];
$description = $rows["description"];
$case_id = $rows["case_id"];
echo "
<tr>
<td>$emp_id</td>
<td>$section_detail</td>
<td>$title</td>
<td>$description</td>
<td>$case_id</td>
</tr>
";
}
?>
</table>
</body>
</html>


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: kobkung ที่ 22 เมษายน 2011, 13:22:27
ใจร้าย ถามไม่หยุด  :wanwan004:

สร้าง query ไป count select count(field) แล้วก็ เอาตัวแปรมาโชว์เป็น array ก็ได้
หรือ mysql_num_row เอา  แบบ select count เร็วกว่า โค้ดหาเองเน้อ  :o


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: icenobu ที่ 22 เมษายน 2011, 13:46:24
ใจร้าย ถามไม่หยุด  :wanwan004:

สร้าง query ไป count select count(field) แล้วก็ เอาตัวแปรมาโชว์เป็น array ก็ได้
หรือ mysql_num_row เอา  แบบ select count เร็วกว่า โค้ดหาเองเน้อ  :o


ขอบคุณค่า เด่วลองดูก่อง  เค้าออกจะใจดีน่ะ เด่วเถอะ  :wanwan010:


หัวข้อ: Re: ช่วยดู code ค้นหาให้ทีค่ะ
เริ่มหัวข้อโดย: dreamer ที่ 22 เมษายน 2011, 15:00:06
select count(case_id),case_type from table_name group by case_type

จะนับจำนวนแยกตาม case_type

ไม่รู้ว่าต้องการแบบนี้หรือเปล่านะ