Reputation: 23
I'm trying to figure out how to make this code work. Basically i have used MySQL to fetch an associative array containing multiple values.
example database: database name = Products
----------------------------------------------------------------.
| name | overcategory | category | subcategory |
| Talon | null | stud welding | capacitor discharge |
| cdss m3x40 | studs/bolts | cd-studs | stainless steel |
----------------------------------------------------------------.
I used the sentence SELECT name, overcategory, category, subcategory FROM Products WHERE 1 = 1;
In the actual database there are more entries, but they are not relevant for this question.
The result i want to output as a menu on the left hand side of the screen.
<nav>
<h1> "$overcategory" </h1>
<h2> "$category" </h2>
<h3> "$subcategory" </h3>
<a href = "">$productname </a>
<a href = "">$productname3 </a>
<h1> "$overcategory2" </h1>
<h2> "$category2" </h2>
<h3> "$subcategory2" </h3>
<a href = "">$productname2 </a>
</nav>
i was thinking of doing this by creating a multidimensional array that would look something like this:
$testArray = array(
''=>array(
'Boltsveiseapparater'=>array(
'Kondensator'=>array(
'Talon',
'LBS-75'
),
'Arc'=>array(
'LBH-410',
'LBH-800'
)
)
),
'Pinner/bolter'=>array(
'CD-pinner'=>array(
'rustfri'=>array(
'cdss m3x35',
'cdss m3x40'
),
'stål'=>array(
'cdms m3x35',
'cdms m6x35'
),
'Alu'=>array(
'cdal m3x10',
'cdal m8x80'
)
),
'Bossinger'=>array(
'Stål'=>array(
'M6x10 5x8',
'M5x12 4x10'
),
'Alu'=>array(
'M6x10 5x8',
'M5x12 4x10'
),
'Rustfri'=>array(
'M6x10 5x8',
'M5x12 4x10'
)
)
)
);
I have no idea how to do this by looping through the results. is there any way of doing this without making a giant array of doom? if not, can someone assist me in decyphering this conundrum.
Thank you in advance. BM.
Upvotes: 2
Views: 3076
Reputation: 12973
How about -
$res = mysql_query('SELECT name, overcategory, category, subcategory FROM Products WHERE 1 = 1 ORDER BY overcategory,category,subcategory, name;');
$aMenu = array();
while($row = mysql_fetch_assoc($res)) {
$aMenu[$row['overcategory']][$row['category']][$row['subcategory']][] = $row['name'];
}
A more significant issue to be looking into is your table structure. Assuming that the overcategory, category and subcategory are part of a hierarchy then you should only be storing the leaf node to which the product belongs, and that should probably be stored as an integer that is a FK to a categories(id, name, parent_id)
table. I tend to opt for nested sets for product catalogs but it depends on the requirements.
Furthermore, it is common for an individual product to be represented in more than one category, in which case you would need to move the category relationship to a join table products_categories(product_id, category_id)
.
Just a little food for thought.
Upvotes: 1
Reputation: 2107
If you sort each of the columns of your results in your SQL query, you can just loop through each result and compare it with the last entry and adjust your output based on that.
SELECT name, overcategory, category, subcategory FROM Products ORDER BY overcategory, category, subcategory, name;
Otherwise, here is one way of handling your nested array:
<?php
echo "\n";
echo "<nav>\n";
foreach ($testArray as $overcategory_name => $category_data) {
echo "\t<h1>$overcategory_name</h1>\n";
foreach ($category_data as $category_name => $subcategory_data) {
echo "\t\t<h2>$category_name</h2>\n";
foreach ($subcategory_data as $subcategory_name => $product_name_data) {
echo "\t\t\t<h3>$subcategory_name</h3>\n";
foreach ($product_name_data as $product_name) {
echo "\t\t\t\t<a href = \"\">$product_name</a>\n";
}
}
}
}
echo "</nav>\n";
Upvotes: 1
Reputation: 2018
You could let mysql sort the result set:
SELECT name, overcategory, category, subcategory FROM Products ORDER BY overcategory,category,subcategory;
Then you can simply loop over the result. Whenever the overcategory, category, subcategory changes you output a new header.
Upvotes: 2