gpt4 book ai didi

php - 表内搜索功能结果

转载 作者:行者123 更新时间:2023-11-29 13:27:34 24 4
gpt4 key购买 nike

我试图将搜索页面的结果控制在 <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 |
|--------------------------------

我已经有近四五年没有接触过 PHP 了,我什至还没有达到你所说的中级水平。我真的不知道在哪里以及如何在代码中执行此操作。这段代码是我找到的许多不同的片段,并将它们拼凑在一起,以像现在一样工作。

最佳答案

我喜欢将我的 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++; } ?>

关于php - 表内搜索功能结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com