在表格中搜索功能结果

时间:2013-11-12 06:42:15

标签: php mysql

我正在尝试将搜索页面的结果设置为<table>。 这段代码确实返回了我需要的数据,我只是不知道如何构造它以我想要的方式输出它。

<?php
include('connection.php');

$error = array();
$results = array();

if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms);

if (strlen($searchTerms) < 3) {
  $error[] = "Search terms must be 3 or more characters.";
}else {
  $searchTermDB = mysql_real_escape_string($searchTerms);
}

if (count($error) < 1) {
  $searchSQL = "
  SELECT 
    ITEM_NUMBER,
    ITEM_NAME,
    VENDOR,
    CONCAT('$', FORMAT(WHOLESALE_PRICE, 2)) AS WHOLESALE_PRICE,
    CONCAT('$', FORMAT(RETAIL_PRICE, 2)) AS RETAIL_PRICE,
    CONCAT('$', FORMAT(RETAIL_PRICE - WHOLESALE_PRICE, 2)) AS PROFIT,
    DESCRIPTION FROM PRODUCTS 
WHERE ";

  $types = array();

  if (count($types) < 1)
     $types[] = "`ITEM_NUMBER` LIKE '%{$searchTermDB}%' OR `ITEM_NAME` LIKE '%    {$searchTermDB}%' OR `VENDOR` LIKE '%{$searchTermDB}%'"; 

  $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `ITEM_NUMBER`";

  $searchResult = mysql_query($searchSQL) or trigger_error("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();
     $i = 1;
     while ($row = mysql_fetch_assoc($searchResult)) 
     {
        $results[] = "
            {$i})<br /> 
            Item Number : {$row['ITEM_NUMBER']}<br /> 
            Item Name : {$row['ITEM_NAME']}<br /> 
            Vendor : {$row['VENDOR']}<br /> 
            Wholesale Price : {$row['WHOLESALE_PRICE']}<br /> 
            Retail Price : {$row['RETAIL_PRICE']}<br /> 
            Profit : {$row['PROFIT']}<br /> 
            Description : {$row['DESCRIPTION']}<br /><br />";
        $i++;
     }
  }
}
}

function removeEmpty($var) {
return (!empty($var)); 
}
?>
<html>
<title>Search Products</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 (Item Number, Name or Vendor) : <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br />
     <input type="submit" name="submit" value="Search!" /><input type="reset" value="Reset" name="Reset" />
  </form>
  <?php echo (count($results) > 0)?"Your search term: {$searchTerms} <br /> Returned:<br /><br />" . implode("", $results):""; ?>
</body>
</html>

期望的外观就像这样:

---------------------------------
|Item Number :     | 0000-0001  |
|--------------------------------
|Item Name :       | test       |
|--------------------------------
|Vendor :          | me         |
|--------------------------------
|Wholesale Price : | $10.00     |
|--------------------------------
|Retail Price :    | $20.00     |
|--------------------------------
|Profit :          | $10.00     |
|--------------------------------
|Description :     | Test       |
|--------------------------------

我还没有在接下来的4到5年内完成PHP,我甚至没有接近你所谓的中间版。我真的不知道在哪里这样做以及如何在代码中。这段代码是我发现的许多不同的片段,并且困惑在一起工作,就像现在一样。

1 个答案:

答案 0 :(得分:1)

我喜欢将我的html与PHP分开,但首先你不需要将结果添加到数组中,因为它们已经在数组中,因此你只需要这样做:

while ($row = mysql_fetch_assoc($searchResult)) { ?>
    <?php echo $i; ?><br>
    Item Number : <?php echo $row['ITEM_NUMBER']; ?><br> 
    Item Name : <?php echo $row['ITEM_NAME']; ?><br> 
    Vendor : <?php echo $row['VENDOR']; ?><br> 
      And so on for each row of data
<?php $i++; } ?>