<?php
$query = "SELECT name FROM prodGroups";
$result = mysql_query($query);
$prodGroups = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$prodGroups[] = $row['name'];
}
if (count($prodGroups) == 0) // IF NO PRODUCT GROUPS EXIST
for ($j=1 ; $j<4 ; $j++)
{
echo "<li><a href='#'><span>Empty product group " . $j . "</span></a></li>";
}
else // FOR WHEN PRODUCT GROUPS DO EXIST
foreach ($prodGroups as $aGroup) // CYCLE THROUGH PRODUCT GROUPS
{
echo "<li class='submenu'><a href='#'><span>" . $aGroup . "</span></a>";
$query = "SELECT name FROM products WHERE prodGroup='$aGroup'";
$result = mysql_query($query);
for ($j=0 ; $j<count($prodGroups) ; ++$j)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) // PLACE PRODUCTS INTO AN ARRAY
**{
$products[] = $row['name'];
}**
if (!isset($products)) // IF THERE ARE NO PRODUCTS INSIDE A PRODUCT GROUP
echo "<ul><li><a href='#'><span>No products</span></a></li></ul></li>";
else // FOR WHEN PRODUCT(S) DO EXIST INSIDE A PRODUCT GROUP
{
echo "<ul>";
if(isset($products))
foreach ($products as $item) // CYCLE THROUGH PRODUCTS
{
echo "<li><a href='#'><span>" . $item . "</span></a>";
}
echo "</ul></li>";
}
}
}
?>
どこで$products[] = $row['name']
; それはすべての要素を運んでいますが、製品名をprodGroup名と一致する場所に運ぶだけですか? unset() を使用してループ内の配列の内容を削除しようとしましたが、これはサイクルごとに配列内の 1 つの要素しか許可しません。