如何从搜索中显示多个结果

时间:2014-06-24 20:27:29

标签: php html mysql

我正在尝试显示搜索的所有结果,但无论我做什么,它仍然只显示一个,我也希望从每个结果链接到产品。有什么建议?感谢

我的 search_engine.php 代码

include("storescripts/init.php"); //connect to DB
$button = $_GET ['submit'];
$search = $_GET ['search'];

if ($search === 'Search Products'){
    echo "Please enter a value!";   
} else {
    if(strlen($search)<=1) echo "Search term too short";
    else {
    echo "<br />You searched for <b>$search</b> <hr size='1'></br>";

    $search_exploded = explode (" ", $search);

    foreach($search_exploded as $search_each) {
        if($search_each)
        $construct .="product_name LIKE '%$search_each%' OR  brand LIKE '%$search_each%' OR category LIKE '%$search_each%'";
    }

    $constructs ="SELECT * FROM products WHERE $construct LIMIT 100 ";
    $run = mysql_query($constructs);

    $foundnum = mysql_num_rows($run);

    if ($foundnum==0) echo "Sorry, there are no matching result for <b>$search</b>.<br /> Please check your spelling";
    else {
        echo "$foundnum results found !<p>";
        $getquery = mysql_query("SELECT * FROM products WHERE $construct  LIMIT 100 ");
        while($runrows = mysql_fetch_assoc($getquery)) {
            $product_name = $runrows ['product_name'];
            $brand = $runrows ['brand'];
            $category = $runrows ['category'];
        }
        echo "
            <a href=#>$product_name</a>  &nbsp;
            $brand &nbsp; $category<p> ";
    }
}

我的表单代码是

<form action="product_search.php" method="GET">
    <input type="text" onclick="this.value='';" value="Search Products" name="search" size="18" maxlength="60"/>
    <input type="submit" style=" background-color:orange; font-size:17px; border-radius:10px;" value="Go!" name="submit"/>
</form>

2 个答案:

答案 0 :(得分:1)

移动在while循环中回显结果的行。

while($runrows = mysql_fetch_assoc($getquery))
{

    $product_name = $runrows ['product_name'];
    $brand = $runrows ['brand'];
    $category = $runrows ['category'];

    echo "
    <a href=#>$product_name</a>  &nbsp;
    $brand &nbsp; $category<p> ";
}

答案 1 :(得分:1)

您需要更改以下内容

while($runrows = mysql_fetch_assoc($getquery))
    {

    $product_name = $runrows ['product_name'];
    $brand = $runrows ['brand'];
    $category = $runrows ['category'];
    }
    echo "
    <a href=#>$product_name</a>  &nbsp;
    $brand &nbsp; $category<p> ";

while($runrows = mysql_fetch_assoc($getquery))
{

    $product_name = $runrows ['product_name'];
    $brand = $runrows ['brand'];
    $category = $runrows ['category'];

    echo "
    <a href=#>$product_name</a>  &nbsp;
    $brand &nbsp; $category<p> ";
}

您正在循环外显示数据,因此在循环结束后显示了最后一个数据。

相关问题