简体   繁体   English

如何消除重复的结果

[英]How to eliminate repeated results

I am doing a query but it returns repeated dates我正在做一个查询,但它返回重复的日期

<select name="option_date">
        <?php
            $sql_product_id = "SELECT * FROM `pat_order_product` WHERE `product_id` = $id_product";
            $result_product_id = mysql_query($sql_product_id,$link);
            while($row_product_id = mysql_fetch_array($result_product_id)){
                $product_options = str_replace('\\','', $row_product_id["product_options"]);
                $data = $product_options;
                $books = json_decode($data, true);
                $product_option_date = $books[0]['value']['name'];
        ?>
                <option value="<?php echo $product_option_date; ?>"><?php echo $product_option_date; ?></option>
        <?php } ?></select>

And when I execute the query, it results in these repeated dates当我执行查询时,它会导致这些重复的日期


<select name="option_date">
    <option value="26 Oct 2019">26 Oct 2019</option>
    <option value="26 Oct 2019">26 Oct 2019</option>
    <option value="26 Oct 2019">26 Oct 2019</option>
    <option value="09 Nov 2019">09 Nov 2019</option>
    <option value="09 Nov 2019">09 Nov 2019</option>
</select>

What I'm looking for is that I only get one result from each date我正在寻找的是每个日期我只得到一个结果

26 Oct 2019 2019 年 10 月 26 日

09 Nov 2019 2019 年 11 月 9 日

You can loop over the results, and generate an array of values.您可以遍历结果,并生成一个值数组。 Using array_unique() on that array, you are ensured to only get unique values.在该数组上使用array_unique() ,可以确保您只获得唯一值。

You should also be using a prepared statement when dealing with user-input in a query (this means using mysqli_ instead of the highly outdated and insecure mysql_ API).在处理查询中的用户输入时,您还应该使用准备好的语句(这意味着使用mysqli_而不是高度过时且不安全的mysql_ API)。

<?php 
// Create a MySQLi connection and set exception handling mode
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli("localhost", "user", "pass", "database");

// Get the product-options and put the dates in an array using a prepared statement
$result = [];
$sql = "SELECT product_options FROM pat_order_product WHERE product_id = ?";
$stmt = $link->prepare($sql);
$stmt->bind_param("s", $id_product);
$stmt->execute();
$stmt->bind_result($product_options);
while ($stmt->fetch()) {
    $data = str_replace('\\', '', $product_options);
    $books = json_decode($data, true);
    $result[] = $books[0]['value']['name'];
}

// Loop the results with array_unique() to get unique results, and print it
?>
<select name="option_date">
    <?php 
    foreach (array_unique($result) as $value) {
        ?>
        <option value="<?php echo $value; ?>"><?php echo $value; ?></option>
        <?php
    }
    ?>
</select>

Useful reading material有用的阅读材料

The date you're displaying is embedded in unstructured data in the product_options field, so it will be difficult to accurately get the response you're hoping for using SQL.您显示的日期嵌入在product_options字段的非结构化数据中,因此使用 SQL 很难准确获得您希望的响应。

Instead, in your PHP you'll need to iterate through the dates to provide a list of distinct dates.相反,在您的 PHP 中,您需要遍历日期以提供不同日期的列表。

One way to do this is to use the value you need to be distinct as the key in a dictionary.一种方法是使用您需要区分的值作为字典中的键。 The value is irrelevant.价值无关紧要。 Because of how dictionaries work, you will only get the values once.由于字典的工作方式,您只能获得一次值。

$distinct_dates = [];
while($row_product_id = mysql_fetch_array($result_product_id)){
    $product_options = str_replace('\\','', $row_product_id["product_options"]);
    $data = $product_options;
    $books = json_decode($data, true);
    $product_option_date = $books[0]['value']['name'];
    $distinct_dates[$product_option_date] = 1;
}

Then, when you're ready to emit the select box, you can do this:然后,当您准备好发出 select 框时,您可以执行以下操作:

<select name="option_date">
<?php foreach(array_keys($distinct_dates) as $date) { ?>
    <option value="<?php echo $date; ?>"><?php echo $date; ?></option>
<? } ?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM