0

我试图让我的搜索页面的结果在<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 了,我什至还没有达到你所说的中级水平。我真的不知道在哪里这样做以及如何在代码中这样做。这段代码是我发现的许多不同的片段,它们一起工作,就像现在一样。

4

1 回答 1

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++; } ?>
于 2013-11-12T07:14:54.623 回答