简体   繁体   English

有没有更好的方法来构建具有搜索功能的PHP数组?

[英]Is there a better way to build my PHP array with search capabilities?

I operate a website which allows hundreds of clients to sell products online. 我经营一个网站,允许数百个客户在线销售产品。 When a customer visits one of my client's websites, they do so in the form 当客户访问我的客户的一个网站时,他们会在表单中这样做

www.site.com?userid=12345 www.site.com?userid=12345

On the landing page, we do a mysql lookup for information about client with userid 12345 along with a list of products he/she can sell. 在登录页面上,我们使用用户ID 12345进行mysql查找以获取有关客户端的信息以及他/她可以销售的产品列表。 We only offer a suite of ~30 products and we define them so we know what they look like but not all clients can offer at the same price. 我们只提供一套约30种产品,我们对它们进行定义,以便我们了解它们的外观,但并非所有客户都能以相同的价格提供。 The mysql table for this information looks like this: 此信息的mysql表如下所示:

userid | category | itemid | price
=================================
12345  |    1     |  1     | 1.00
12345  |    1     |  2     | 2.00
12345  |    1     |  3     | 3.00
12345  |    2     |  4     | 4.00
12345  |    2     |  5     | 5.00
67890  |    1     |  1     | 2.25
67890  |    2     |  4     | 8.00

When the customer visits www.site.com?userid=12345, a lookup is done with the following: 当客户访问www.site.com?userid=12345时,将使用以下内容进行查找:

SELECT category, itemid, price FROM tablename WHERE userid = 12345

*Note - yes I prevent sql injection. *注意 - 是的,我阻止了sql注入。

The issue I run into is how to build the subsequent array in PHP. 我遇到的问题是如何在PHP中构建后续数组。 My initial thought is: 我最初的想法是:

<?php
...

while ($row = $stmt->fetch()) {
  $products[]['category'] = $row['category'];
  $products[]['itemid'] = $row['itemid'];
  $products[]['price'] = $row['price'];
}


//the array will end up looking like this:
Array
(
    [0] => Array
        (
            [category] => 1
            [itemid] => 1
            [price] => 1
        )
    [1] => Array
        (
            [category] => 1
            [itemid] => 2
            [price] => 2
        )
    [2] => Array
        (
            [category] => 1
            [itemid] => 3
            [price] => 3
        )
    [3] => Array
        (
            [category] => 2
            [itemid] => 4
            [price] => 4
        )
    [4] => Array
        (
            [category] => 2
            [itemid] => 5
            [price] => 5
        )
)

?>

The reason I need to capture information like this: On the actual site itself, there are two main categories (Food and Drinks). 我需要捕获这样的信息的原因:在实际网站本身,有两个主要类别(食品和饮料)。 If a client has any products in $products with category 1, show the Food html section on the page. 如果客户在$ product的类别为1的产品中有任何产品,请在页面上显示Food html部分。 If a client has any products in $products with category 2, show the Drinks html section on the page. 如果客户在$ product的类别为2的产品中有任何产品,请在页面上显示Drinks html部分。

Then, there is a predefined set of itemids available to all categories. 然后,所有类别都有一组预定义的itemid。 In the case of food, there are 4: 在食物的情况下,有4:

  1. pepperoni 意大利辣味香肠
  2. sausage 香肠
  3. cheese 起司
  4. vegetable 蔬菜

If a user does not have one of these itemids, he/she cannot sell that product and that product does not appear within the Food html section. 如果用户没有这些itemid之一,他/她不能销售该产品,并且该产品不会出现在Food html部分中。 If they are able to sell that product, I want to be able to reference that section/item's price and display it next to the actual item. 如果他们能够销售该产品,我希望能够参考该部分/项目的价格并将其显示在实际项目旁边。

I know my $products array will contain all of the information I need but I am not sure if the way I am structuring it is best or how to really reference it when building out the page as described. 我知道我的$ products数组将包含我需要的所有信息,但我不确定我构建它的方式是最好还是如何在构建页面时真正引用它。

For example (I know this code is not right but saying what I am trying to accomplish): 例如(我知道这段代码不对,但说的是我想要完成的事情):

  • "If value of 1 exists in the $products category array, show the Food section." “如果$ products类别数组中存在值1,请显示”食品“部分。”
  • "If value of 1 exists in the $products itemid category array, show Pepperoni pizza as an option on the page and display the price associated with that specific category/itemid combination." “如果$ products itemid类别数组中存在值1,则在页面上显示Pepperoni pizza作为选项,并显示与该特定类别/ itemid组合关联的价格。”

Any guidance is appreciated. 任何指导表示赞赏。

$products[]['category'] = $row['category'];
$products[]['itemid'] = $row['itemid'];
$products[]['price'] = $row['price'];

This will create a new array for each attribute; 这将为每个属性创建一个新数组; use 使用

$products[] = $row;

…to copy the row contents to the products array. ...将行内容复制到products数组。

I'm not sure I follow everything you ask, but as you're looping through the rows, why not do your category checks there. 我不确定我是否按照你要求的所有内容,但是当你在行中循环时,为什么不在那里进行类别检查。

//setup some flags
$food  = false;
$drink = false;

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetch an assoc array
  $products[] = $row;
  if(!$food AND 1 == $row['category']){ //haven't seen food yet, check for it
    $food = true;
  } elseif (!$drink AND 2 == $row['category']) { //same for drink
    $drink = true;
  }
}

//later
if($food){
  //display food
} 

Of course, instead of just tracking those flags explicitly, it seems a more generic approach would be better: 当然,而不是仅仅明确地跟踪这些标志,似乎更通用的方法会更好:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetch an assoc array
  $products[] = $row;
  $categories[$row['category']] = true;
}

//later
if(in_array(1, $categories)){
  //display food
}

But that still leaves a bit to be desired. 但这仍然有点不尽如人意。 How about this: 这个怎么样:

//use some OOP to make this cleaner (class constants)
define('FOOD' , 1);
define('DRINK', 2);

//setup default values
$categories = array(FOOD => false, DRINK = false);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetch an assoc array
  $products[] = $row;
  $categories[$row['category']] = true;
}

//later
if($categories[FOOD]){
  //display food
}

And, if I understand your question correctly, you may need to just check if a product is available (using the product id) - change your product loop to track that as the key: 并且,如果我正确理解您的问题,您可能只需检查产品是否可用(使用产品ID) - 更改产品循环以跟踪该密钥:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { //fetch an assoc array
  $products[$row['itemid']] = $row;
  $categories[$row['category']] = true;
}

//later
if(isset($products[$searchId])){
  //user has this product
}

If you use Zend you can do $products = $query->fetchAssoc with itemid as first column and get the array $products[itemid1] => array(itemid1, 'category', 'price'), $products[itemid2] => array(itemid2, 'category', 'price'). 如果您使用Zend,您可以使用itemid作为第一列$ products = $ query-> fetchAssoc并获取数组$ products [itemid1] => array(itemid1,'category','price'),$ products [itemid2] = > array(itemid2,'category','price')。

But for category I think better solution is: 但对于类别我认为更好的解决方案是:

$products[$row['category']][$row['itemid']] = $row['price'];

So yo can use it like this: 所以你可以像这样使用它:

if (!empty($products[$foodid])) {
    echo "Food HTML part";
    if (!empty($products[$foodid][$cheeseid])) {
        echo "Cheese price = {$products[$foodid][$cheeseid]}";
    }
}

I hope I understand your question :) Sorry for my English 我希望我理解你的问题:)抱歉我的英语

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

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