Jump to content

Recommended Posts

I've recently been building a site, and have followed the SQL tutorial on searching databases thats on the phpfreaks site.

 

I've tailored it to the needs of the site im designing, and am trying to get the search function to put the results into a table.

 

The example shown below works, and is basically an adaption of the Tutorial on the site..

 

$searchResult = mysql_query($searchSQL) or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");  

if (mysql_num_rows($searchResult) < 1) {  
$error[] = "The search term provided {$searchTerms} yielded no results.";  
}else {  
$results = array(); // the result array  
$i = 1; 

while ($roww = mysql_fetch_assoc($searchResult)) {  
$results[] = "{$i}: {$roww['Make']}<br />{$roww['Model']}<br />{$roww['Part']}<br />{$roww['Product']}<br />{$roww['Year']}<br />{$roww['Fitment']}<br />{$roww['Part']}<br /><br />";  
$i++;  
}  
}  
}  
}  

function removeEmpty($var) {  
return (!empty($var));   
}  
?>  
<html>  
<title>My Simple Search Form</title>  
<style type="text/css">  
#error {  
color: red;  
}  
</style>  
<body>  
<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>  
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">  
Search for your model of car (e.g. Escort): <input type="text" name="search" value="<?php echo isset($searchTerms)?$searchTerms:''; ?>" /><br />   
<input type="submit" name="submit" value="Search!" />  
</form>  
<?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>  
</body>  
</html>  

 

however trying to put the data that appears from a successful search straight into a table on the same page is proving a task.

 

Im trying to put it into a table of 9 columns, each with a seperate heading (Product, Make, Model,

Year, Fitment, Part, Location, OriginalPrice and Price) and the corresponding results shown directly below.

 

So, if there is 3 matches on the search, obviously it would show a row of 9 columns, followed by 3 rows with the search data in...

 

so far this is what i have, giving me an error on line 55:

 

$searchResult = mysql_query($searchSQL) or die("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");  

if (mysql_num_rows($searchResult) < 1) {  
$error[] = "The search term provided {$searchTerms} gave no results. Try shortening the Model (e.g. Escort instead of Escort RS Turbo)";  
}else {  
$results = array(); // the result array  
$i = 1; 

while ($roww = mysql_fetch_assoc($searchResult)) {  
$results[] = {$i}: {$roww ?> <table width="200" border="1">
  <tr>
    <td>Product</td>
    <td>Make</td>
    <td>Model</td>
    <td>Year</td>
    <td>Fitment</td>
    <td>Part</td>
    <td>OriginalPrice</td>
    <td>Price</td>
<td>ToOrder</td>
  </tr>
  <tr>
    <td><?Php {$roww['Product']}?></td>
    <td><?Php {$roww['Make']} ?></td>
    <td><?Php {$roww['Model']} ?></td>
    <td><?Php {$roww['Year']} ?></td>
    <td><?Php {$roww['Fitment']} ?></td>
    <td><?Php {$roww['Part']} ?></td>
    <td><?Php {$roww['OriginalPrice']} ?></td>
    <td><?Php {$roww['Price']} ?></td>
<td><?Php {$roww['ToOrder']} ?></td>
  </tr>
</table> 
<?Php $i++;  
}  
}  
}  
}  

function removeEmpty($var) {  
return (!empty($var));   
}  
?>  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style3 {font-size: 9}
-->
#error {  
color: red;  
}
</style>
</head>

<body>
<br />
<br />
<?php echo (count($error) > 0)?"The following occured:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>  
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">  
Search for your model of car (e.g. Escort): <input type="text" name="search" value="<?php echo isset($searchTerms)?$searchTerms:''; ?>" /><br />   
<input type="submit" name="submit" value="Search!" />  
</form>  
<?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>  
<br />
<p>
  <label></label></p>
<table width="950" height="76" border="1">
  <tr>
    <td width="113" height="42"><div align="center">Product</div></td>
    <td width="111"><div align="center">Make</div></td>
    <td width="116"><div align="center">Model</div></td>
    <td width="89"><div align="center">Year</div></td>
    <td width="101"><div align="center">Fitment</div></td>
    <td width="82"><div align="center">Part</div></td>
    <td width="87"><div align="center">OriginalPrice</div></td>
    <td width="41"><div align="center">Price</div></td>
    <td width="152"><div align="center">To Order</div>
    <div align="center"></div></td>
  </tr>
  
<?php do {  ?>
  <tr>
    <td height="25"><?php echo $row['Product']; ?></td>
    <td><?php echo $row['Make']; ?></td>
    <td><?php echo $row['Model']; ?></td>
    <td><?php echo $row['Year']; ?></td>
    <td><?php echo $row['Fitment']; ?></td>
    <td><?php echo $row['Part']; ?></td>
    <td><?php echo $row['OriginalPrice']; ?></td>
    <td><?php echo $row['Price']; ?></td>
    <td><?php echo $row['ToOrder']; ?></td>
    </tr>
<?php }while ($row = mysql_fetch_array($result));   ?>
</table>
</body>
</html>

 

 

Any help would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/164938-search-results-into-a-table/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.