如何从DB中获取重复的条目,而不是只获取一次?

时间:2020-11-28 00:16:06

I am pulling info about items from a database.

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

Each different item gets its own index number:

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

$product[$x]

产品美元($ x)

If I list the item twice (See banana in code), Only one index number will be created.

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

How can I get an index number to be created for each item, independent of whether that item was already called?

如何为每个项创建索引号,而不依赖于该项是否已被调用?

<?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/>";
}

With current code, $product[3] (which I would like to be banana) doesn't exist, Since the code seems to remove duplicates automatically. I don't want it to remove them.

对于当前代码,$product[3](我希望它是banana)不存在,因为代码似乎自动删除重复的代码。我不想让它移除它们。

My motivation is that later in the code, I might want to display the same item in two variations, and when I display items I'm using a loop that is running on the items index number. therefore, the loop will have nothing to run on for any item that currently appears in my $item_array more than once, unless the code will be changed so that it will get duplicates according to what I placed inside my array.

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

EDIT:

编辑:

Updated working code:

更新的工作代码:

<?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/>";
}

1 个解决方案

#1


2  

First of all - there's no need to search for duplicate items. Your query currently is:

首先,不需要搜索重复的项目。查询目前是:

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

This is redundant

这是多余的

Do an array_unique while creating $item_implode:

创建$ item_内爆时执行array_unique:

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

This will leave $item_array untouched, but reduces your query.

这将保持$item_array不变,但减少查询。

Next, if I got you right you want one item from db for your query. This means that if you have several bananas in db only one record should be placed in $product. Okay, do this:

接下来,如果我让你猜对了,你想要从数据库中查询一个项目。这意味着,如果在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";
}

Update. SO okay - you want fruit occurrences in products to be equal it's occurrences in $item_array. Try this:

更新。你希望产品中水果的数量相等它在$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'];
    }
}

#1


2  

First of all - there's no need to search for duplicate items. Your query currently is:

首先,不需要搜索重复的项目。查询目前是:

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

This is redundant

这是多余的

Do an array_unique while creating $item_implode:

创建$ item_内爆时执行array_unique:

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

This will leave $item_array untouched, but reduces your query.

这将保持$item_array不变,但减少查询。

Next, if I got you right you want one item from db for your query. This means that if you have several bananas in db only one record should be placed in $product. Okay, do this:

接下来,如果我让你猜对了,你想要从数据库中查询一个项目。这意味着,如果在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";
}

Update. SO okay - you want fruit occurrences in products to be equal it's occurrences in $item_array. Try this:

更新。你希望产品中水果的数量相等它在$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'];
    }
}