desperate cry for help (2nd question)

A

Anonymous

Guest
hi again,

if u need a little bit of info about well, everything, read the first post.

Anyway this is my second head scratcher.

I have a search function that searches my database (not the customer table this time, but a product table/database) and returns my results for me, whether i search for the product, the country of origin, or thickness, using the follwing code;

Code:
<?

	require_once("timbmet_function.php");
	session_start();
	
	// get html header
	do_html_header("Search");
	
	// provide form to search items 
    echo "<form method=post action=\"results.php\">";
    echo "<table>";
    echo "<tr><td>Choose Search Type:</td>";
    echo "<td><select name=\"searchtype\">";
	echo "<option value=\"species\">Species"; 
    echo "<option value=\"size\">Thickness"; 
    echo "<option value=\"country\">Country of Origin"; 
    echo "<option value=\"productid\">Product Code"; 
    echo "</Select></td></tr>";
    echo "<tr><td>Enter Search Term:</td>";
    echo "<td><input name=\"searchterm\" type=text></td></tr>";
    echo "<tr><td colspan=2 align=center>";
    echo "<input type=submit value=\"Search\"></td></tr>";
    echo "</table></form>";
	
	// get html footer
	display_user_menu();
	do_html_footer();

?>

this gives me a little search form to specify search parameters etc....which then leads me to the results table when i press the submit button.

Anyway this is where my headscratching starts.

It returns the results fine, using the following code

Code:
<?

	require_once("timbmet_function.php");
	session_start();
	
	// get html header
	do_html_header("Results");
	
	// check entered details from search.php
	if (!$searchtype || !$searchterm)
  	{
     	echo "You have not entered search details.  Please go back and try again.";
     	exit;
  	}
  
  	$searchtype = addslashes($searchtype);
  	$searchterm = addslashes($searchterm);
	
	$conn = db_connect(); // connect to database
	if (!$conn)
		return "Could not connect to database server - please try again later.";
		
	mysql_select_db('timbmets');
  	$query = "select * from products where ".$searchtype." like '%".$searchterm."%'";
  	$result = mysql_query($query);
	
	$num_results = mysql_num_rows($result);

  	echo "<p>Number of products found: ".$num_results."</p>";
	
	// display search results
  	for ($i=0; $i <$num_results; $i++)
  	{
     	$row = mysql_fetch_array($result);
     	echo "<p><strong>".($i+1).". Species: ";
     	echo stripslashes($row["species"]);
     	echo "</strong><br>Thickness: ";
     	echo stripslashes($row["size"]);
     	echo "<br>Country of Origin: ";
     	echo stripslashes($row["country"]);
     	echo "<br>Product Code: ";
     	echo stripslashes($row["productid"]);
     	echo "</p>";
  	}
	
	// get html footer
	display_user_menu();
	do_html_footer();
	
?>

However, it displays them in the format

Species: Sapele
Thickness: 25mm
Country of Origin: Africa

etc........

What i really, really, really want is for the results to be placed into a results table instead which would have the column headings of "No", "Species", "Size", "Porduct Code" etc..., then displaying each of the results in turn underneath the relevant column.

Again i know this sounds relatively easy, but when u do this at the end of every eveining once u've been to work from 7-5 everyday when i should be at home revising and doing other work, whilst doing other reports, and coursework, u kinda dont see the woods through the trees (or is that trees through the woods??).

Anyway, again if anyone can help, id be soooooooooooooo grateful. I would go out and buy another book or search the web more, but i have limited web access, and books cost a whopping £30-£40 over here, and i aint forking out that for a book that i'll only need for the next month.

Thanks awfully

Markie :lol:
 
Like so:
Code:
$result = mysql_query($query);

// start your table, etc...
while ($row = mysql_fetch_assoc($result)) {
   echo "\n<tr>";
   echo "\n<td>$row[species]</td>";
   echo "\n<td>$row[size]</td>";
   echo "\n<td>$row[country]</td>";
   echo "\n<td>$row[productid]</td>";
   echo "\n<\tr>";
}
// end your table..


Hope that helps!
 
Back
Top