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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic query

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Hi guys,

I am investigating converting two of our older software programs into web-based applications with a MySQL back-end. I would prefer to do it on PHP than Delphi as it will gives us a neat little Intranet. I've been playing around with PHP a bit and have found it really good.

My main concern is with MySQL query results.

e.g.

Lets say I execute this query:
Select * from customers where cust_code LIKE AB%

Now, with these results, I would display them in a table on the next page. This is where my problem is. Lets say I have 3 rows in the table. They might look something like this:

Customer Code Address
AB11 Address for AB11
AB12 Address for AB12
AB13 Address for AB13

I would like to be able to click on any of these rows above and execute another query, e.g. If I click on line 3, it will get the customer code AB13, then I can execute:
SELECT * FROM INVOICES WHERE CUST_CODE = 'AB13'.

How can I get the customer code for the row I click on? Is this possible?

Any suggestions welcome
Thanks in advance.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
This is fairly standard in the PHP/MySQL world and there are many ways of doing it. Here is my take on it:
Code:
<?
$q = 'Select * from customers where cust_code ';LIKE AB%';
$q .= (isset($_GET['where']))?" ='" . $_GET['where'] ."'":'like AB%';
$rs = mysql_query($q) or die('Something is wrong with the query: '.$q.'<br>'.mysql_error());
while ($rw = mysql_fetch_assoc($rs)) {
   $tmp = array();
   if (!isset($_GET['where']))
      $tmp[] = '<a href="' . $_SERVER['PHP_SELF'] . '?which=' . $rw['customer_code'] . '>' . $rw['customer_code'] . '</a> ' . $rw['address'];
   else {
    // processing for individual customer code. Put each line in the $tmp array
   }
   echo implode("<br>\n",$tmp);
}
?>
What this does is to put out each line like
Code:
<a href="youscript.php?where=cust_code">cust_code</a> cust_address
if the customer_code hasn't been entered in the URL.

Note: I just typed in the code and it hasn't been checked for errors. YMMV

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top