Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search with two queries showing one result 3

Status
Not open for further replies.
Jan 28, 2003
18
US
I am trying to have this search return information from one of the two queries.
So, if the first query does not return any results then it would run the second query and show those results.
And, if no results were found from either query, it would just say nothing found.
The records that I am searching for are in history or active tables, so, if it does not find it in the active table it searches the history table.
I am using SQL2008r2 and PHP 5.4.3

Thanks in advance.
Mario

Below is my current search:

Code:
<?php
	$conn = odbc_connect('db','user','pw') or die ('DB Connection Failed.');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search</title>
</head>
<body>
<form method="GET" action="search.php" id="frm" name="frm" >
<table width="500" border="0">
  <tr>
    <td>Search</td>
    <td>
      <input type="text"  name="fullname" id="fullname"/>    </td>
  </tr>
  <tr>
    <td>
      <input type="submit" name="Submit" value="Search"/>   </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>

<?php
if(!isset($_GET['fullname'])) die("Search query not found");
$var = $_GET['fullname'];

if($_REQUEST["Submit"]=="Search")
		if($conn)
		{
			$QUERYA = 	"SELECT
				db.ship_via_cd AS shipper,
				db.oe_po_no AS custpo, 
				convert(varchar,db.shipping_dt,107) AS Ship_Dt,
				db.cmt_1 AS PRO_No,
				db.phone_number AS phone3,
	//...snip
				convert(varchar,db.inv_dt,107) AS invdate
			
			FROM db JOIN dblin ON db.ord_no = dblin.ord_no 
				JOIN item ON item.item_no = dblin.item_no 
						
			WHERE  (oe_po_no like '%$var%' OR db.user_def_fld_3 LIKE '%$var%' OR db.cmt_1 LIKE '%$var%' OR db.ship_to_name LIKE '%$var%')
		";
		
		$result = odbc_exec($conn,$QUERYA);
		
			{
			$QUERYB = 			"SELECT
				history.ship_via_cd AS shipperb,
				history.oe_po_no AS custpob, 
				convert(varchar,history.shipping_dt,107) AS Ship_Dtb,
				history.cmt_1 AS PRO_Nob,
				history.phone_number AS phone3b,
	//...snip
				convert(varchar,history.inv_dt,107) AS invdateb
			
			FROM history JOIN history2 ON history.ord_no = history2.ord_no 
				JOIN item ON item.item_no = history2.item_no 
						
			WHERE  (oe_po_no like '%$var%' OR history.user_def_fld_3 LIKE '%$var%' OR history.cmt_1 LIKE '%$var%' OR history.ship_to_name LIKE '%$var%')
		";
			$result1 = odbc_exec($conn,$QUERYB);

?>
<table width="500" border="1">
<caption><B>Ship Info</B></caption>
<tr>
<th>shipper</th>
<th>item</th>
<th>description</th>
<th>qtyord</th>
<th>DSPO</th>
<th>customer</th>
<th>PRO_No</th>
<th>custpo</th>
<th>shipname</th>
<th>Ship_Dt</th>
<th>shipcity</th>
<th>shipstate</th>
<th>shipzip</th>
<th>phone</th>
<th>invoice</th>
<th>invdate</th>
  <?php

while($row = odbc_fetch_array($result)){
  ?>
  <tr>
    <td><?php echo $row['shipper']; ?></td>
    <td><?php echo $row['item']; ?></td>
    <td><?php echo $row['description']; ?></td>
    <td><?php echo $row['qtyord'];?></td>
    <td><?php echo $row['DSPO']; ?></td>
    <td><?php echo $row['customer'];?></td>
    <td><?php echo $row['PRO_No'];?></td>
    <td><?php echo $row['custpo'];?></td>
    <td><?php echo $row['shipname'];?></td>
    <td><?php echo $row['Ship_Dt'];?></td>
    <td><?php echo $row['shipcity'];?></td>
    <td><?php echo $row['shipstate'];?></td>
    <td><?php echo $row['shipzip'];?></td>	
    <td><?php echo $row['phone3'];?></td>	
    <td><?php echo $row['invoice'];?></td>
    <td><?php echo $row['invdate'];?></td>	
  </tr>

 <?PHP
 }
   }
  }
 ?>
</table>
</body>
</html>
 
Code:
$result = odbc_exec($conn,$QUERYA);
$nullResult = false;
if(odbc_num_rows($result) == 0 ):
$QUERYB = 			"SELECT
				history.ship_via_cd AS shipperb,
				history.oe_po_no AS custpob, 
				convert(varchar,history.shipping_dt,107) AS Ship_Dtb,
				history.cmt_1 AS PRO_Nob,
				history.phone_number AS phone3b,
	//...snip
				convert(varchar,history.inv_dt,107) AS invdateb
			
			FROM history JOIN history2 ON history.ord_no = history2.ord_no 
				JOIN item ON item.item_no = history2.item_no 
						
			WHERE  (oe_po_no like '%$var%' OR history.user_def_fld_3 LIKE '%$var%' OR history.cmt_1 LIKE '%$var%' OR history.ship_to_name LIKE '%$var%')
		";
			$result = odbc_exec($conn,$QUERYB);
                        $nullResult = odbc_num_rows($result) == 0) ? true : false;
endif;


if($nullResult):
 echo "Nothing found for that search term";
else:
$fields = array('shipper','item','description','qtyord','DSPO','customer','PRO_No', 'custpo','shipname','Ship_Dt', 'shipcity','shipstate','shipzip','phone3','invoice','invdata');
?>
<table width="500" border="1">
<caption><B>Ship Info</B></caption>
<tr>
<?php foreach($fields as $field): ?>
<th><?php echo $field;?> </th>
<?php endforeach; ?>
<?php while($row = odbc_fetch_array($result)): ?>
<tr>
<?php  foreach($fields as $field): ?>
    <td><?php echo $row[$field]; ?></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
 
jpadie,
Thanks for the help, I inserted your changes, but now I am getting an Parse error: syntax error, unexpected end of file on line 122.
I have looked through it and I don't see anything missing, but I am nowhere near the advanced level.
The entire file is below, the only thing that is different than from running code is the username/pw and db name.
Thanks again for the help.
MV
Code:
<?php
	$conn = odbc_connect('db','user','pw') or die ('DB Connection Failed.');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search</title>
</head>
<body>
<form method="GET" action="search.php" id="frm" name="frm" >
<table width="500" border="0">
  <tr>
    <td>Search</td>
    <td>
      <input type="text"  name="fullname" id="fullname"/>    </td>
  </tr>
  <tr>
    <td>
      <input type="submit" name="Submit" value="Search"/>   </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>

<?php
if(!isset($_GET['fullname'])) die("Search query not found");
$var = $_GET['fullname'];

if($_REQUEST["Submit"]=="Search")
		if($conn)
		
			$QUERYA = 	"SELECT
				oehdrhst_sql.ship_via_cd AS shipper,
				oehdrhst_sql.oe_po_no AS custpo, 
				convert(varchar,oehdrhst_sql.shipping_dt,107) AS Ship_Dt,
				oehdrhst_sql.cmt_1 AS PRO_No,
				oehdrhst_sql.phone_number AS phone3,
				oehdrhst_sql.ship_to_addr_3 AS phone1,
				oehdrhst_sql.ship_to_addr_2 AS phone2, 
				oehdrhst_sql.ship_to_name AS shipname, 
				oehdrhst_sql.ship_to_addr_1 AS shipaddy1, 
				oehdrhst_sql.ship_to_addr_2 AS shipaddy2, 
				oehdrhst_sql.ship_to_addr_4 AS shipaddy4, 
				oehdrhst_sql.ship_to_city AS shipcity, 
				oehdrhst_sql.ship_to_state AS shipstate, 
				oehdrhst_sql.ship_to_zip AS shipzip, 
				oelinhst_sql.ord_no AS ordnum, 
				oelinhst_sql.item_no AS item,
				oelinhst_sql.item_desc_1 AS description, 
				oehdrhst_sql.cus_no AS customer,				
				FLOOR(qty_ordered) AS qtyord,
				oehdrhst_sql.user_def_fld_3 AS DSPO,
				oehdrhst_sql.inv_no AS invoice,
				convert(varchar,oehdrhst_sql.inv_dt,107) AS invdate
			
			FROM oehdrhst_sql JOIN oelinhst_sql ON oehdrhst_sql.ord_no = oelinhst_sql.ord_no 
				JOIN imitmidx_sql ON imitmidx_sql.item_no = oelinhst_sql.item_no 
						
			WHERE  (oe_po_no like '%$var%' OR oehdrhst_sql.user_def_fld_3 LIKE '%$var%' OR oehdrhst_sql.cmt_1 LIKE '%$var%' OR oehdrhst_sql.ship_to_name LIKE '%$var%')
		";
	
		$result = odbc_exec($conn,$QUERYA);
$nullResult = false;
if(odbc_num_rows($result) == 0 ):
			$QUERYB = 			"SELECT
				oeordhdr_sql.ship_via_cd AS shipperb,
				oeordhdr_sql.oe_po_no AS custpob, 
				convert(varchar,oeordhdr_sql.shipping_dt,107) AS Ship_Dtb,
				oeordhdr_sql.cmt_1 AS PRO_Nob,
				oeordhdr_sql.phone_number AS phone3b,
				oeordhdr_sql.ship_to_addr_3 AS phone1b,
				oeordhdr_sql.ship_to_addr_2 AS phone2b, 
				oeordhdr_sql.ship_to_name AS shipnameb, 
				oeordhdr_sql.ship_to_addr_1 AS shipaddy1b, 
				oeordhdr_sql.ship_to_addr_2 AS shipaddy2b, 
				oeordhdr_sql.ship_to_addr_4 AS shipaddy4b, 
				oeordhdr_sql.ship_to_city AS shipcityb, 
				oeordhdr_sql.ship_to_state AS shipstateb, 
				oeordhdr_sql.ship_to_zip AS shipzipb, 
				oeordlin_sql.ord_no AS ordnumb, 
				oeordlin_sql.item_no AS itemb,
				oeordlin_sql.item_desc_1 AS descriptionb, 
				oeordhdr_sql.cus_no AS customerb,				
				FLOOR(qty_ordered) AS qtyordb,
				oeordhdr_sql.user_def_fld_3 AS DSPOb,
				oeordhdr_sql.inv_no AS invoiceb,
				convert(varchar,oeordhdr_sql.inv_dt,107) AS invdateb
			
			FROM oeordhdr_sql JOIN oeordlin_sql ON oeordhdr_sql.ord_no = oeordlin_sql.ord_no 
				JOIN imitmidx_sql ON imitmidx_sql.item_no = oeordlin_sql.item_no 
						
			WHERE  (oe_po_no like '%$var%' OR oeordhdr_sql.user_def_fld_3 LIKE '%$var%' OR oeordhdr_sql.cmt_1 LIKE '%$var%' OR oeordhdr_sql.ship_to_name LIKE '%$var%')
		";
			$result = odbc_exec($conn,$QUERYB);
                        $nullResult = (odbc_num_rows($result) == 0) ? true : false;
endif;


if($nullResult):
 echo "Nothing found for that search term";
else:
$fields = array('shipper','item','description','qtyord','DSPO','customer','PRO_No', 'custpo','shipname','Ship_Dt', 'shipcity','shipstate','shipzip','phone3','invoice','invdata');
?>

<table width="500" border="1">
<caption><B>Ship Info</B></caption>
<?php foreach($fields as $field): ?>
<th><?php echo $field; ?> </th>
<?php endforeach; ?>
<?php while($row = odbc_fetch_array($result)): ?>
<tr>
<?php  foreach($fields as $field): ?>
    <td><?php echo $row[$field]; ?> </td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>

</table>
</body>
</html>
 
Not to jump in on jpadie's thread...but I think the code is missing an "endif;" towards the end of the script.

Remember- It's nice to be important,
but it's important to be nice :)
 
petrosky / jpadie
I found the missing endif and all is great now.
jpadie
Thanks for the help, this is exactly what I was looking for.
mario
 
You've got to love Tek-Tips.

Remember- It's nice to be important,
but it's important to be nice :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top