Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Can this code be improved and tested?
Old 05-11-2005, 10:57 AM Can this code be improved and tested?
andrewsco's Avatar
Super Talker

Posts: 130
Trades: 0
Hi. I have some coursework which I have to do...I have pretty much finished it, just wondered if anyone can suggest any changes? I need to connect to a database and query it, and return the results. You will figure out what I need to do by the code: Can anyone suggest any improvements?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml/DTD/xhtml1-stricy.dtd">
  
<html>
  <head>  
   <title>Results</title>
   <meta http-equiv="content-type"content="text/html;charset=iso-8859-1" />
   <link rel="stylesheet" type="text/css" href="?????????????????????.css" />
  </head>
  <body>
  <form method="get" action="results.php">
	<p>Search for a customer: </p>
	<hr />
	<p><input type="text" name="search" /></p>
	<p><input type="submit" name="submit query" /></p>
	<p><input type="reset" name="reset" /></p>
  </form>
	

  </body>  

<?

/**
 * Connect to the mysql database.
 */
$conn = mysql_connect("128.240.2.48", "demo", "netskills") or die(mysql_error());
mysql_select_db('northwind', $conn) or die(mysql_error());

// Get user input
$UserInput = $_GET['search'];

// SQL query statement 
$SqlQuery = "SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE CustomerID LIKE '$UserInput%' ";

// Store query result, return error message if attempt fails and exit
$result = mysql_query ($SqlQuery) or die('Query execution problem: ' . mysql_error());
		
// Number of rowsreturned by SQL query
$number_rows = mysql_number_rows($result);

// Assign the file to a variable
$File_Name = fopen ("query_results.txt", "w");

// Close IP connection
 mysql_close($conn);

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml/DTD/xhtml1-stricy.dtd">
  
<html>
  <head>  
   <title>Output</title>
   <meta http-equiv="content-type"content="text/html;charset=iso-8859-1" />
   <link rel="stylesheet" type="text/css" href="???????????????????????.css" />
  </head>
  <body>

  
    
  <p class="???????????????">Results = </p>
  <table>
   <tr>
    <th>Customer ID</th>
    <th>Company Name</th> 
    <th>Contact Name</th>
    <th>Country</th>
   </tr>
    
   <?
     //Write the reults to the file
     fwrite($RESULTSFILE, "Results of the query:\n");

     fwrite($RESULTSFILE, "Customer ID, Company Name, Contact Name, Country\n"); //Write the column headings to the file
     
     //while the table has rows left do the following
     while($output_row = mysql_fetch_array($result)) 
     {
   ?>  

 	   <tr>
		  <td> <?=htmlentities($output_row[CustomerID]) ?> </td> <!-- Return CompanyID  -->		  
		  <td> <?=htmlentities($output_row[CompanyName]) ?> </td> <!-- Return CompanyName  -->		  
		  <td> <?=htmlentities($output_row[ContactName]) ?> </td> <!-- Return ContactName  -->		  
		  <td> <?=htmlentities($output_row[Country]) ?> </td> <!-- Return Country  -->
	   </tr>
			<!-- Write to file -->
			<? fwrite($query_txt, "$output_row[CustomerID]\t") ?>
			<? fwrite($query_txt, "$output_row[CompanyName]\t") ?>
			<? fwrite($query_txt, "$output_row[ContactName]\t") ?>
			<? fwrite($query_txt, "$output_row[Country]\n") ?>

   <?
     } //End of while statement
     fclose($File_Name); //Close the file after all statements have been written
   ?>
   </table>


   <?
		// Conditional HTML display, dependant on SQL query
		if($number_rows < 1)
		{
	?>		
			<p><a href="php_input.html">No Rows: Return to form</a></p>
	<?
		}
		else
		{	  
	?>
			<p><a href="php_input.html">Return to form</a></p>
			<p><a href="query_results.txt">Show Results</a></p>
	<?
		}

	?>




   </body>  
 </html>  
 </html>
and my html page:

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml/DTD/xhtml1-stricy.dtd">
  
<html>
  <head>  
   <title>Database Output</title>
   <meta http-equiv="content-type"content="text/html;charset=iso-8859-1" />
   <link rel="stylesheet" type="text/css" href="a4Style.css" />
  </head>
  <body>
  <form method="get" action="results.php">
	<p>Search for a customer: </p>
	<hr />
	<p><input type="text" name="search" /></p>
	<p><input type="submit" name="submit query" /></p>
	<p><input type="reset" name="reset" /></p>
  </form>
	

  </body>  
 </html>
I hope that this code will prduce an output text file too.

Thanks
Andy
__________________
My Webpage:
Please login or register to view this content. Registration is FREE
andrewsco is offline
Reply With Quote
View Public Profile Visit andrewsco's homepage!
 
 
Register now for full access!
Reply     « Reply to Can this code be improved and tested?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18646 seconds with 12 queries