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

Beginner trying to learn PHP 1

Status
Not open for further replies.

scriggs

IS-IT--Management
Jun 1, 2004
286
GB
I have PHP accessing the data held in my database and can run a query and display results - that much is learnt.

How do I use the database to fill a listbox and then use the listbox to filter the results to a specific record.

i.e. LISTBOX of customer names, when selected:

Display - customer address, phone, etc.
 
This is a two step process in PHP.
1) Display the form
2) After the user press the "Submit" button (it doesn't have to say "Submit", display the record.

Sample code follows.


Code:
<?php
if (!isset($_POST['submit'])) { // Step 1: fill initial form and present it
   $tmp = array();
   $tmp[] = '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
   $tmp[] = '<select name="custname" size=1>';
   $q = 'select customer_name from your_database_table';
   $rs = @mysql_query($q) or die('Something is wrong with the query: ' . $q . '<br>' . mysql_error());
   while ($rw = mysql_fetch_assoc($rs))
       $tmp[] = '<option>' . $rw['customer_name'] . </option>;
   $tmp[] = '</select>';
   $tmp[] = '<input type="submit" name="submit" value="Display Customer Information">';
   $tmp[] = '</form>';
   echo implode("\n",$tmp)."\n";
}
else { // Step 2: Display data
   $tmp = array();
   $valid_cust = validate($_POST['custname']); //make sure that input is in the format you are expecting
   $q = "select * from your_database_table where customer_name = '" . $valid_cust . "'";
   $rs = @mysql_query($q) or die('Something is wrong with the query: ' . $q . '<br>' . mysql_error());
   $rw = mysql_fetch_assoc($rs);
   foreach ($rw as $k=>$v)
      $tmp[] = ucwords(str_replace('_',' ',$k) . ': ' . $v . '<br>'; // if you make your field names like 'customer_name', then this would produce 'Customer Name: '
   echo implode("\n",$tmp)."\n";
}
?>

Hope this is helpful to you.

Ken
PS -- I started this response an hour ago and just got off the phone after a phone interview that lasted an hour!
 
Thanks Ken, I will have a look later and get back to you. Seems like a nice concise bit of code for me to understand and adapt.
 
Hi Ken

I have tried this and adapted for my database, etc.

I get an error when I try to run the script

Code:
Parse error: parse error, unexpected '<' in c:\inetpub\[URL unfurl="true"]wwwroot\php\fees.php[/URL] on line 16

I think line 16 is
Code:
$tmp[] = '<option>' . $rw['Clients.ClientKey'] . </option>;
but dont' know what is wrong. Something to do with the final option, does it need a quote round it somewhere?
 
I have enclosed it in quotes and it seems to work now

Code:
$tmp[] = '<option>' . $rw['Clients.ClientKey'] . '</option>';

Now I get an error on line 29, which I think is this line

Code:
foreach ($rw as $k=>$v)

The error reads "Parse error: parse error, unexpected ';' in c:\inetpub\ on line 29". I'm not sure what this one is caused by?
 
Hi Ken

I have worked round your code as I take it your code would produce a generic table with headings. I am also using ODBC so have changed the recrodsets to reflect that. Just for your interest here is my current working code as a test, now I just need to tidy it up and display exactly what I want.

Thanks for the help

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Select a client and see their data!</title>
<!-- 
This coding constructs an option select box
pre populated with client codes from an ODBC
database.

On selecting the correct client code the database
is queried and displays all related information.

-->
<style type="text/css">
<!--
body {font-family:verdana; font-size:11px; font-style:normal; font-weight:normal;}
th {color:white; background-color:gray; font-size:11px;}
td {background-color: silver; font-size:11px;}
-->
</style>

</head>
<body>
<?php

// odbc_query.php
$odbc_dsn = "timeandfees_db";
$odbc_userid = "";
$odbc_password = "";

if (!isset($_POST['submit'])) { // Step 1: fill initial form and present it
   $tmp = array();
   $tmp[] = '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
   $tmp[] = '<select name="custname" size=1>';

$query = "SELECT Clients.ClientKey FROM clients ORDER BY Clients.ClientKey";

if(!($odbc_db = odbc_connect($odbc_dsn, $odbc_userid, $odbc_password)))
	die ("Could not connect ot ODBC data source $odbc_dsn");
  
if(!($odbc_rs = odbc_do($odbc_db, $query)))
	die("Error executing query $query");
  
$num_cols = odbc_num_fields($odbc_rs);
if($num_cols < 1) die("Query returned an empty set");


while(odbc_fetch_row($odbc_rs)) $tmp[] = '<option>' . odbc_result($odbc_rs,'ClientKey') . '</option>';
$tmp[] = '</select>';
$tmp[] = '<input type="submit" name="submit" value="Display Time and Fees Information">';
$tmp[] = '</form>';
echo implode("\n",$tmp)."\n";

}

else { // Step 2: Display data
   $tmp = array();
//   $valid_cust = validate($_POST['custname']); //make sure that input is in the format you are expecting
   $valid_cust = $_POST['custname'];
   $query = "SELECT Clients.ClientKey, Clients.CoyName, Addresses.* FROM Addresses INNER JOIN Clients ON Addresses.AddrKey = Clients.AddrKey where Clients.ClientKey = '" . $valid_cust . "'";
if(!($odbc_db = odbc_connect($odbc_dsn, $odbc_userid, $odbc_password)))
	die ("Could not connect ot ODBC data source $odbc_dsn");
  
if(!($odbc_rs = odbc_do($odbc_db, $query)))
	die("Error executing query $query");
  
$num_cols = odbc_num_fields($odbc_rs);
if($num_cols < 1) die("Query returned an empty set");

?>

<table summary="" style="{width:100%;}">
	<tr>
  <?php
  	for($a=1;$a<=$num_cols;$a++)
    {
  ?>
  	<th style="{text-align:left;}">
    	<b>
    <?php
    	echo odbc_field_name($odbc_rs, $a);
    ?>
    	</b>
    </th>
  <?php
  	}
  ?>
  </tr>
<?php
	while(odbc_fetch_row($odbc_rs))
  {
?>
	<tr>
  <?php
  	for($a=1;$a<=$num_cols;$a++)
    {
    	$data = odbc_result($odbc_rs, $a);
      echo "<td>$data</td>";
    }
  ?>
  </tr>
<?php
	}
}
?>
</table>    

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top