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