Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ช่วยดู scrpit ดึงข้อมูลหลายตาราง แบบมีเงื่อนไข ให้บ้างนะครับ



 

ช่วยดู scrpit ดึงข้อมูลหลายตาราง แบบมีเงื่อนไข ให้บ้างนะครับ

 



Topic : 072456



โพสกระทู้ ( 7 )
บทความ ( 0 )



สถานะออฟไลน์




สวัสดีครับ รบกวนดู script ให้บ้างนะครับ ขออธิบายโครงสร้างคร่าวๆ นะครับ
สมมุติมีตารางอยู่ 4 ตาราง
ตารางที่ 1 ชื่อ request_test = ประกอบด้วยฟิล์ด refer_no,test_part,name,request_date
ตารางที่ 2 ชื่อ result_bio = ประกอบด้วยฟิล์ด refer_no,test_name,result,test_part หมายเหตุ (test_part=B)
ตารางที่ 3 ชื่อ result_hemato = ประกอบด้วยฟิล์ด refer_no,test_name,result,test_part หมายเหตุ (test_part=H)
ตารางที่ 4 ชื่อ result_micro = ประกอบด้วยฟิล์ด refer_no,test_name,result,test_part หมายเหตุ (test_part=M)

ผมทำหน้าเวบแบบนี้ครับ หน้าแรกใช้ค้นหาและแสดงรายชื่อ หน้าที่สองเป็นการแสดงรายละเอียดจากหน้าแรกโดยคลิกที่หมายเลข refer_no
ปัญหาคือ ผมต้องการให้ถ้า refer_no มี test_part=B ก็ไปดึงรายละเอียดจากตารางที่ 2 มาแสดง
ถ้ามี test_part=H ก็ไปดึงจากตาราง 3 มาแสดง หรือ ถ้ามี test_part=M ก็ไปดึงจากตาราง 4 มาแสดง ประมาณนี้นะครับผมไม่รู้ว่าต้องเขียนแบบไหน
ผมส่ง script ที่ผมทำมาให้ดูด้วยครับ
อันนี้เป็นหน้าแรก ใช้ค้นหาและแสดงรายชื่อ
Code (PHP)
<?
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>LAB</title>
</head>
<body>
<form name="frmSearch" method="get" action="<?=$_SERVER['../SCRIPT_NAME'];?>">
<table width="100%" border="0">
<tr>
<th>ใส่ HN หรือ ชื่อ นามสกุล
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
if($_GET["txtKeyword"] != "")
{
include("../conn/labcon.php");
$strSQL ="select r.refer_no,r.hn,r.name,r.sub_test,r.request_date,r.approved_date,r.test_part from request_test r 
left join member m on m.hospname=r.ward
where m.hosid='01' and  (r.hn LIKE '%".$_GET["txtKeyword"]."%' or r.name LIKE '%".$_GET["txtKeyword"]."%') 
order by r.refer_no desc";

$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mssql_num_rows($objQuery);
	$Per_Page = 20;   // Per Page
	$Page = $_GET["Page"];
	if(!$_GET["Page"])
	{
		$Page=1;
	}
	$Prev_Page = $Page-1;
	$Next_Page = $Page+1;
	$Page_Start = (($Per_Page*$Page)-$Per_Page);
	if($Num_Rows<=$Per_Page)
	{
		$Num_Pages =1;
	}
	else if(($Num_Rows % $Per_Page)==0)
	{
		$Num_Pages =($Num_Rows/$Per_Page) ;
	}
	else
	{
		$Num_Pages =($Num_Rows/$Per_Page)+1;
		$Num_Pages = (int)$Num_Pages;
	}
	$Page_End = $Per_Page * $Page;
	IF ($Page_End > $Num_Rows)
	{
		$Page_End = $Num_Rows;
	}
	?>
<table width="100%" border="1">
<tr>
<th width="15"> <div align="center"> order_no</div></th>
<th width="15"> <div align="center">hn</div></th>
<th width="200"> <div align="center">name </div></th>
<th width="59"> <div align="center">sub_test</div></th>
<th width="97"> <div align="center">request_date </div></th>
<th width="97"> <div align="center">approved_date</div></th>
</tr>
<?
for($i=$Page_Start;$i<$Page_End;$i++)
	{
	?>
<tr>
<td><a href=result_b.php?refer_no=<?=mssql_result($objQuery,$i,"refer_no");?>   target=_blank><div align="center"><?=mssql_result($objQuery,$i,"refer_no");?></div></a></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"hn");?></div></td>
<td><?=mssql_result($objQuery,$i,"name");?></td>
<td align="center"><?=mssql_result($objQuery,$i,"sub_test");?></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"request_date");?></div></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"approved_date");?></div></td>
</tr>
<?
}
?>
</table>
<br>
	Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
	<?
	if($Prev_Page)
	{
		echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";
	}
	for($i=1; $i<=$Num_Pages; $i++){
		if($i != $Page)
		{
			echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";
		}
		else
		{
			echo "<b> $i </b>";
		}
	}
	if($Page!=$Num_Pages)
	{
		echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";
	}
		mssql_close($objConnect);
	}	
	?>	
</body>
</html>

อันนี้เป็นหน้าที่ 2 ครับ แสดงรายละเอียด แต่ยังทำแบบที่ต้องการไม่ได้ครับ
Code (PHP)
<?
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>LAB Uthaithani Hospital</title>
</head>
<body>
<br>
<?
include("../conn/labcon.php");
[font=Verdana]$strSQL ="select r.refer_no,r.hn,r.name,r.request_date,r.approved_date,b.sub_test,b.test_name
,b.result,r.report_by,r.approved_by  from request_test r
left join result_bio b  on b.refer_no=r.refer_no 
where r.test_part='B' r.refer_no='$refer_no' ";[/font]
$objQuery = mssql_query($strSQL) or die ("Error Query");
?>
<FORM name="form1" method=post action="result_b.php?refer_no=<?php echo "$refer_no"?>">
<table width="100%" border="1">
<tr>
<th width="15"> <div align="center"> order_no</div></th>
<th width="15"> <div align="center">hn</div></th>
<th width="150"> <div align="center">name </div></th>
<th width="59"> <div align="center">sub_test</div></th>
<th width="97"> <div align="center">test_name</div></th>
<th width="15"> <div align="center">result</div></th>
<th width="97"> <div align="center">request_date </div></th>
<th width="97"> <div align="center">approved_date</div></th>
<th width="97"> <div align="center">report_by</div></th>
<th width="97"> <div align="center">approved_by</div></th>
</tr>
<?
while($objResult = mssql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["refer_no"];?></div></td>
<td><div align="center"><?=$objResult["hn"];?></div></td>
<td><?=$objResult["name"];?></td>
<td align="center"><?=$objResult["sub_test"];?></td>
<td align="center"><?=$objResult["test_name"];?></td>
<td align="center"><?=$objResult["result"];?></td>
<td><div align="center"><?=$objResult["request_date"];?></div></td>
<td><div align="center"><?=$objResult["approved_date"];?></div></td>
<td align="center"><?=$objResult["report_by"];?></td>
<td align="center"><?=$objResult["approved_by"];?></td>
</tr>
<?
}
?>
</table>
</FORM>
<?
mssql_close($objConnect);
?>
</body>
</html>


ขอคุณครับ



Tag : PHP, Ms SQL Server 2005, Reporting Service







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-01-23 10:51:03 By : choked View : 1220 Reply : 7
 

 

No. 1



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์


โครงสร้าง Field และจำนวนเหมือนกันหมดทั้ง 3 ตารางใช่ไหมครับ
ในหน้าส่งค่า ให้ใช้ if เช็คว่าเป็น test_part อะไร
แล้วให้ส่งค่าชื่อตารางมาอีกค่าหนึ่ง

ในหน้าแสดงผลก็เปลี่ยนจาก FROM table เป็น FROM ตัวแปรชื่อตารางแทนครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-23 11:01:56 By : amuropao08
 


 

No. 2



โพสกระทู้ ( 7 )
บทความ ( 0 )



สถานะออฟไลน์


ผมลองทำแล้ว ก็ยังไม่ได้นะครับ Code (PHP)
<?
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>LAB Uthaithani Hospital</title>
</head>

<body>

<form name="frmSearch" method="get" action="<?=$_SERVER['../SCRIPT_NAME'];?>">
<table width="100%" border="0">

<tr>
<th>ใส่ HN หรือ ชื่อ นามสกุล
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
if($_GET["txtKeyword"] != "")
{
include("../conn/labcon.php");
//$objConnect = mssql_connect("10.10.0.15","Uthai2011","Uthai2011") or die("Error Connect to Database");
//$objDB = mssql_select_db("mediexam_uthaithani");
// Search By HN or NAME
$test_part=test_part;
$strSQL ="select r.refer_no,r.hn,r.name,r.sub_test,r.request_date,r.approved_date,r.$test_part from request_test r 
left join member m on m.hospname=r.ward
where m.hosid='01' and  (r.hn LIKE '%".$_GET["txtKeyword"]."%' or r.name LIKE '%".$_GET["txtKeyword"]."%') 
order by r.refer_no desc";
///$strSQL = "SELECT re.refer_no,re.hn,re.name,ro.request_date,ro.sub_test from register re
//LEFT JOIN result_hemato ro ON ro.refer_no=re.refer_no and ro.hn=re.hn
//WHERE ro.Checkapproved='1'  and  (re.hn LIKE '%".$_GET["txtKeyword"]."%' or re.name LIKE '%".$_GET["txtKeyword"]."%') 
//group by re.refer_no,re.hn,re.name,ro.request_date,ro.sub_test  order by re.refer_no desc ";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mssql_num_rows($objQuery);
	$Per_Page = 20;   // Per Page

	$Page = $_GET["Page"];
	if(!$_GET["Page"])
	{
		$Page=1;
	}

	$Prev_Page = $Page-1;
	$Next_Page = $Page+1;

	$Page_Start = (($Per_Page*$Page)-$Per_Page);
	if($Num_Rows<=$Per_Page)
	{
		$Num_Pages =1;
	}
	else if(($Num_Rows % $Per_Page)==0)
	{
		$Num_Pages =($Num_Rows/$Per_Page) ;
	}
	else
	{
		$Num_Pages =($Num_Rows/$Per_Page)+1;
		$Num_Pages = (int)$Num_Pages;
	}
	$Page_End = $Per_Page * $Page;
	IF ($Page_End > $Num_Rows)
	{
		$Page_End = $Num_Rows;
	}
	?>
	
ตรงนี้คือที่ใส่เงือนไขตรวจสอบนะครับ
              <?
	if  ($test_part = B)
	{
	$table = result_bio;
	}
	else if ($test_part=BK)
	{
	$table=result_bloodbank;
	}
	else if ($test_part=H)
	{
	$table=result_hemato;
	}
	else if ($test_part=I)
	{
	$table=result_immuno;
	}
	else if ($test_psrt=M)
	{
	$table=result_micro;
	}
	else if ($test_part=O)
	{
	$table=result_outlab;
	}
	else if ($test_part=U)
	{
	$table=result_urine;
	}	
	?>

<table width="100%" border="1">
<tr>
<th width="15"> <div align="center"> order_no</div></th>
<th width="15"> <div align="center">hn</div></th>
<th width="200"> <div align="center">name </div></th>
<th width="59"> <div align="center">sub_test</div></th>
<th width="97"> <div align="center">request_date </div></th>
<th width="97"> <div align="center">approved_date</div></th>
</tr>
<?
for($i=$Page_Start;$i<$Page_End;$i++)
	{
	?>
<tr>
<td><a href=result_b.php?refer_no=<?=mssql_result($objQuery,$i,"refer_no");?>   target=_blank><div align="center"><?=mssql_result($objQuery,$i,"refer_no");?></div></a></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"hn");?></div></td>
<td><?=mssql_result($objQuery,$i,"name");?></td>
<td align="center"><?=mssql_result($objQuery,$i,"sub_test");?></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"request_date");?></div></td>
<td><div align="center"><?=mssql_result($objQuery,$i,"approved_date");?></div></td>
</tr>
<?
}
?>
</table>
<br>
	Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
	<?
	if($Prev_Page)
	{
		echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";
	}
	for($i=1; $i<=$Num_Pages; $i++){
		if($i != $Page)
		{
			echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";
		}
		else
		{
			echo "<b> $i </b>";
		}
	}
	if($Page!=$Num_Pages)
	{
		echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";
	}
		mssql_close($objConnect);
	}	
	?>	
</body>
</html>




ประวัติการแก้ไข
2012-01-23 21:33:25
2012-01-23 21:34:19
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-23 11:40:07 By : choked
 

 

No. 3



โพสกระทู้ ( 7 )
บทความ ( 0 )



สถานะออฟไลน์



แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-23 21:38:17 By : choked
 


 

No. 4



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์


เป็นข้อความใส่ ' หรือ " คร่อมด้วยครับ ใน IF
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-24 07:42:50 By : amuropao08
 


 

No. 5



โพสกระทู้ ( 7 )
บทความ ( 0 )



สถานะออฟไลน์


ผมลองแล้วแต่มันไม่ส่งค่าเข้ามานะครับ มันส่งค่าแรกค่าเดียว
Code (PHP)
<?
	if  ($test_part = "B")
	{
	$table = "result_bio";
	}
	else if ($test_part="BK")
	{
	$table="result_bloodbank";
	}
	else if ($test_part="H")
	{
	$table="result_hemato";
	}
	else if ($test_part="I")
	{
	$table="result_immuno";
	}
	else if ($test_psrt="M")
	{
	$table="result_micro";
	}
	else if ($test_part="O")
	{
	$table="result_outlab";
	}
	else if ($test_part="U")
	{
	$table="result_urine";
	}	
	?>
<?
include("../conn/labcon.php");
$strSQL ="select r.refer_no,r.hn,r.name,r.request_date,r.approved_date,b.sub_test,b.test_name,b.result,r.report_by,r.approved_by from request_test r
left join $table b  on b.refer_no=r.refer_no 
where   r.refer_no='$refer_no' ";

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-24 14:27:15 By : choked
 


 

No. 6



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์


ไม่แน่ใจว่าได้รึยัง
เครื่องหมาย = ใน if ลองเพิ่มเป็น == ดูครับ
ถ้าเปรียบเทียบแบบนี้ลองใช้ switch ดูไหมครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-01-31 10:08:48 By : amuropao08
 


 

No. 7



โพสกระทู้ ( 7 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 6 เขียนโดย : amuropao08 เมื่อวันที่ 2012-01-31 10:08:48
รายละเอียดของการตอบ ::
ยังไม่ได้เลยครับ ผมลองใส่ == แล้ว error ไปเลยน่ะครับ
ไม่รู้จะเขียนยังไงดีครับ


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-02-02 15:26:55 By : choked
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยดู scrpit ดึงข้อมูลหลายตาราง แบบมีเงื่อนไข ให้บ้างนะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่