【问题标题】:How to obtain Duplicate entries from DB, instead of just once?如何从数据库中获取重复条目,而不是一次?
【发布时间】:2016-04-29 07:50:06
【问题描述】:

我正在从数据库中提取有关项目的信息。

每个不同的项目都有自己的索引号:

$product[$x]

如果我两次列出该项目(参见代码中的banana),只会创建一个索引号。

如何获取要为每个项目创建的索引号,而与该项目是否已被调用无关?

<?php
include 'connect.php'; //

// Choose Relevant Items, and turn them into an array
$item_array = array(
'apple',
'banana',
'banana'
);

//implode items, turn into string
$item_implode = join("','", $item_array);

//declare an overall array for result
$product = array();

$productList = array();
$result = $mysqli->query("SELECT Name, Status as item_status from item_table where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode');");

if ($result->num_rows > 0) {
    $x = 1;
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $product[$x]["Name"] = $row['Name'];
        $product[$x]["item_status"] = $row['item_status'];

        $x = $x + 1;
    }
} else {
    echo "0 results";
}


// test print all avaialable database values
for ($i=1; $i <= count($product); $i++) { 
    echo $product[$i]["Name"] . " - ";
    echo $product[$i]["item_status"] . "<br/>";
}

使用当前代码$product[3](我想成为banana不存在,因为代码似乎会自动删除重复项。 我不希望它删除它们。

我的动机是,稍后在代码中,我可能希望以两种变体显示相同的项目,并且当我显示项目时,我使用的是在项目索引号上运行的循环。因此,对于当前多次出现在我的$item_array 中的任何项目,循环都不会运行,除非代码将被更改,以便根据我放置在我的数组中的内容获得重复。

编辑:

更新的工作代码:

<?php

include 'connect.php';

// Choose Relevant items, and turn them into an array
$item_array = array(
'apple',
'banana',
'carrot',
'banana',
'carrot',
'berry'
);

//implode items, turn into string
$item_implode = join("','", $item_array);

//declare an overall array for result
$product = array();

$result = $mysqli->query("SELECT Name, Status as item_status from item_table where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode');");

// New Code:

while($row = $result->fetch_assoc()) {


    $fruit_name = $row['Name'];
    // next you need to find all keys in $item_array which value is the fruit
    $keys = array_keys($item_array, $fruit_name);
    foreach ($keys as $key) {

        // add values for these keys
        $product[$key+1]['Name'] = $row['Name'];
        $product[$key+1]['item_status'] = $row['item_status'];

    }
}

/* REPLACED CODE: //////////////////////////

if ($result->num_rows > 0) {
    $x = 1;
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $product[$x]["Name"] = $row['Name'];
        $product[$x]["item_status"] = $row['item_status'];

        $x = $x + 1;
    }
} 


else {
    echo "0 results";
}*/

/////////////////////////////////////////////

// test print all avaialable database values
for ($i=1; $i <= count($product); $i++) { 
    echo $product[$i]["Name"] . " - ";
    echo $product[$i]["item_status"] . "<br/>";
}

【问题讨论】:

  • array_uniquejoin 之前?
  • @u_mulder 你的意思是我应该在代码中实现array_unique?不知道如何从文档中的示例中做到这一点。简单地在 join 之前插入它会导致语法错误。此外,文档说 array_unique 假设 remove 重复,而我想 keep 重复。
  • 上面的sql有错误SELECT Name, Status from item")"table~一个假的)
  • 是的,它只是一个错字,我会修复它。
  • @RyanVincent 我不想让它独一无二,我只想创建 2 个变量/索引而不是一个。两者都应该是相同的,只要索引号是根据它们在我的数组中的顺序。

标签: php mysql arrays database mysqli


【解决方案1】:

首先 - 无需搜索重复项。 您当前的查询是:

SELECT Name, Status from item)table where Name IN ('apple', 'banana', 'banana')

这是冗余的

在创建$item_implode 时执行array_unique

$item_implode = join("','", array_unique($item_array));

这将使$item_array 保持不变,但会减少您的查询。

接下来,如果我没听错的话,您需要 db 中的一项来进行查询。这意味着如果您在 db 中有多个香蕉,则应将一条记录放在 $product 中。好的,这样做:

$founded_fruits = array();
// special array to track founded fruits
if ($result->num_rows > 0) {
    $x = 1;
    // output data of each row
    while($row = $result->fetch_assoc()) {
        // I suppose that `$row['Name']` is name of a fruit
        if (!in_array($row['Name'], $founded_fruits)) {
            $founded_fruits[] = $row['Name'];
            $product[$x]["Name"] = $row['Name'];
            $product[$x]["Status"] = $row['Status'];
        }

        $x = $x + 1;
    }
} else {
    echo "0 results";
}

更新。好吧-您希望products 中的水果出现次数与$item_array 中的出现次数相同。试试这个:

$product = array();
while($row = $result->fetch_assoc()) {
    $fruit_name = $row['Name'];
    // next you need to find all keys in $item_array which value is the fruit
    $keys = array_keys($item_array, $fruit_name);
    foreach ($keys as $key) {
        // add values for these keys
        $products[$key]['Name'] = $row['Name'];
        $products[$key]['item_status'] = $row['item_status'];
    }
}

【讨论】:

  • 我为这个问题简化了我的 SELECT,实际上我正在重命名字段 (Status as item_status) 等等。
  • 不 - 我确实想要重复。我在第一条评论中澄清了这一点。
  • 想要在哪里重复?
  • 现在,$product[2]banana$product[3] 不存在,因为它是重复的,即使我将它作为数组中的第三个条目放置。我希望创建$product[3],并获取banana 的值,就像$product[2] 一样。
  • 好吧,好吧,看起来有点像,请稍等。
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
相关资源
最近更新 更多