3

我经营一个网站,允许数百名客户在线销售产品。当客户访问我客户的一个网站时,他们会以以下形式访问

www.site.com?userid=12345

在登录页面上,我们使用 mysql 查找有关用户 ID 为 12345 的客户的信息以及他/她可以销售的产品列表。我们只提供约 30 种产品的套件,我们对其进行定义,以便我们知道它们的外观,但并非所有客户都能以相同的价格提供。此信息的 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

当客户访问 www.site.com?userid=12345 时,会使用以下内容进行查找:

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

*注意 - 是的,我阻止了 sql 注入。

我遇到的问题是如何在 PHP 中构建后续数组。我最初的想法是:

<?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
        )
)

?>

我需要捕获这样的信息的原因:在实际站点本身上,有两个主要类别(食物和饮料)。如果客户在 $products 中有类别 1 的任何产品,请在页面上显示 Food html 部分。如果客户在 $products 中有类别 2 的任何产品,请在页面上显示 Drinks html 部分。

然后,有一组预定义的 itemid 可用于所有类别。就食物而言,有4种:

  1. 意大利辣香肠
  2. 香肠
  3. 起司
  4. 蔬菜

如果用户没有这些 itemid 之一,他/她将无法销售该产品,并且该产品不会出现在 Food html 部分中。如果他们能够销售该产品,我希望能够参考该部分/项目的价格并将其显示在实际项目旁边。

我知道我的 $products 数组将包含我需要的所有信息,但我不确定我构建它的方式是否最好,或者在构建所描述的页面时如何真正引用它。

例如(我知道这段代码不正确,但说明了我想要完成的事情):

  • “如果 $products 类别数组中存在值 1,则显示食品部分。”
  • “如果 $products itemid 类别数组中存在值 1,则将 Pepperoni Pizza 显示为页面上的一个选项,并显示与该特定类别/itemid 组合相关的价格。”

任何指导表示赞赏。

4

3 回答 3

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

这将为每个属性创建一个新数组;采用

$products[] = $row;

…将行内容复制到产品数组。

于 2012-11-08T17:06:53.240 回答
1

我不确定我是否遵循您提出的所有要求,但是当您遍历行时,为什么不在那里进行您的类别检查。

//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
} 

当然,不仅仅是明确地跟踪这些标志,似乎更通用的方法会更好:

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
}

但这仍然有一些不足之处。这个怎么样:

//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
}

而且,如果我正确理解您的问题,您可能只需要检查产品是否可用(使用产品 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
}
于 2012-11-08T17:25:36.953 回答
1

如果您使用 Zend,您可以使用 itemid 作为第一列的 $products = $query->fetchAssoc 并获取数组 $products[itemid1] => array(itemid1, 'category', 'price'), $products[itemid2] = > 数组(itemid2,'类别','价格')。

但对于类别,我认为更好的解决方案是:

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

所以你可以像这样使用它:

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

我希望我能理解你的问题:) 对不起我的英语

于 2012-11-08T17:34:59.680 回答