PHP:避免两次显示相同的产品?

时间:2015-02-26 09:30:54

标签: php mysql

我使用以下代码使用PHP / MYSQL为我的网站创建搜索功能。

如果选中了复选框,此代码将搜索尺寸和颜色,并将其显示在我的页面上。

示例:

checkbox 1 = small
checkbox 2 = large
checkbox 3 = xxlarge
checkbox 4 = red
checkbox 5 = black
etc etc ......

如果用户选择small and large as the size and red as the color,我的PHP将返回具有凭据的产品。

这是PHP代码:

<?php
error_reporting(-1);
ini_set('display_errors', 'On'); 
include "config/connect.php";
$searchList = "";
$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['submit'])){
    if(isset($_POST['keyword'])){
        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $sql .= $clause . " (ATTRIBUTES.sizes LIKE BINARY '$c' OR ATTRIBUTES.colors LIKE BINARY '$c')";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }
        //print "SQL Query: $sql<br />"; //<-- Debug SQl syntax.
        // Run query outside of foreach loop so it only runs one time.
        $query = mysqli_query($db_conx, $sql);
        //var_dump($query); //<-- Debug query results.
        // Check that the query ran fine.
        if (!$query) {
            print "ERROR: " . mysqli_error($db_conx);
        } else {
            // Use $query inside this section to make sure $query exists.
            $productCount = mysqli_num_rows($query);
            $i=0; // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
                   $id = $row["id"];
                   $product_name = $row["product_name"];
                   $searchList .= ''.$product_name.'';
                }
            }
        }
    }
}
?> 

我遇到的问题:

如果产品的小型和大型(两者)作为凭据,并且用户搜索的是小型和大型,我的PHP将返回相同的项两次

如何在上述情况下停止php返回相同的产品两次?

任何建议都将不胜感激。

我按照建议使用了GROUP BY:

$sql="SELECT *
FROM `yt` GROUP BY yt.id
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id"

但是会引发以下错误:

ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id WHERE (ATTRIBUTES.sizes LIKE BIN' at line 3

1 个答案:

答案 0 :(得分:0)

您需要使用SQL&#34; GROUP BY&#34;功能。如果yt.id是您的产品ID,则该&#34; GROUP BY yt.id&#34;附加到您的查询。

尝试:

<?php

error_reporting(-1);
ini_set('display_errors', 'On');
include "config/connect.php";
$searchList = "";
$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['submit'])){
    if(isset($_POST['keyword'])){
        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $sql .= $clause . " (ATTRIBUTES.sizes LIKE BINARY '$c' OR ATTRIBUTES.colors LIKE BINARY '$c')";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }

        //append "GROUP BY"
        $sql .= " GROUP BY yt.id ";

        //print "SQL Query: $sql<br />"; //<-- Debug SQl syntax.
        // Run query outside of foreach loop so it only runs one time.
        $query = mysqli_query($db_conx, $sql);
        //var_dump($query); //<-- Debug query results.
        // Check that the query ran fine.
        if (!$query) {
            print "ERROR: " . mysqli_error($db_conx);
        } else {
            // Use $query inside this section to make sure $query exists.
            $productCount = mysqli_num_rows($query);
            $i=0; // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
                    $id = $row["id"];
                    $product_name = $row["product_name"];
                    $searchList .= ''.$product_name.'';
                }
            }
        }
    }
}
?>