Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a better way to build my PHP array with search capabilities?
    primarykey
    data
    text
    <p>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 </p> <blockquote> <p>www.site.com?userid=12345</p> </blockquote> <p>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:</p> <pre><code>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 </code></pre> <p>When the customer visits www.site.com?userid=12345, a lookup is done with the following:</p> <pre><code>SELECT category, itemid, price FROM tablename WHERE userid = 12345 </code></pre> <p>*Note - yes I prevent sql injection.</p> <p>The issue I run into is how to build the subsequent array in PHP. My initial thought is:</p> <pre><code>&lt;?php ... while ($row = $stmt-&gt;fetch()) { $products[]['category'] = $row['category']; $products[]['itemid'] = $row['itemid']; $products[]['price'] = $row['price']; } //the array will end up looking like this: Array ( [0] =&gt; Array ( [category] =&gt; 1 [itemid] =&gt; 1 [price] =&gt; 1 ) [1] =&gt; Array ( [category] =&gt; 1 [itemid] =&gt; 2 [price] =&gt; 2 ) [2] =&gt; Array ( [category] =&gt; 1 [itemid] =&gt; 3 [price] =&gt; 3 ) [3] =&gt; Array ( [category] =&gt; 2 [itemid] =&gt; 4 [price] =&gt; 4 ) [4] =&gt; Array ( [category] =&gt; 2 [itemid] =&gt; 5 [price] =&gt; 5 ) ) ?&gt; </code></pre> <p>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.</p> <p>Then, there is a predefined set of itemids available to all categories. In the case of food, there are 4:</p> <ol> <li>pepperoni</li> <li>sausage</li> <li>cheese</li> <li>vegetable</li> </ol> <p>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.</p> <p>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.</p> <p>For example (I know this code is not right but saying what I am trying to accomplish):</p> <ul> <li>"If value of 1 exists in the $products category array, show the Food section."</li> <li>"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."</li> </ul> <p>Any guidance is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload