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
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. 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. The mysql table for this information looks like this:
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:
SELECT category, itemid, price FROM tablename WHERE userid = 12345
*Note - yes I prevent sql injection.
The issue I run into is how to build the subsequent array in 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. If a client has any products in $products with category 2, show the Drinks html section on the page.
Then, there is a predefined set of itemids available to all categories. In the case of food, there are 4:
- pepperoni
- sausage
- cheese
- 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. 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.
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."
- "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."
Any guidance is appreciated.