<?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()来删除循环中的数组内容,但是这只允许每个循环中的一个元素。
答案 0 :(得分:2)
添加$products = array();
紧接着:
foreach ($prodGroups as $aGroup) // CYCLE THROUGH PRODUCT GROUPS
{
答案 1 :(得分:1)
我试图重写你的循环,修复了几个问题,其中大部分是次要问题。我评论了我的变化。看看这是否适合你。
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 loop here has been taken out
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>";
else // FOR WHEN PRODUCT(S) DO EXIST INSIDE A PRODUCT GROUP
{
echo "<ul>";
//if(isset($products)) - You already checked this..
foreach ($products as $item) // CYCLE THROUGH PRODUCTS
{
echo "<li><a href='#'><span>" . $item . "</span></a></li>"; //Don't forget to close this li
}
echo "</ul>";
}
echo '</li>'; //close li here
unset($products); //unset $products for next loop iteration
}
当然,请不要使用已弃用的mysql扩展名。请参阅@FreshPrinceOfSO的评论。
答案 2 :(得分:0)
使用mysql_num_rows()
<?php
$query = "SELECT name FROM prodGroups";
$result = mysql_query($query);
if( (!$result) || (mysql_num_rows($result) == 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
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) // CYCLE THROUGH PRODUCT GROUPS
{
echo "<li class='submenu'><a href='#'><span>" . $row['name'] . "</span></a>";
$query2 = "SELECT name FROM products WHERE prodGroup='".mysql_real_escape_string($row['name'])."'";
$result2 = mysql_query($query2);
if( (!$result2) || (mysql_num_rows($result2)==0) )
{
echo "<ul><li><a href='#'><span>No products</span></a></li></ul>";
}
else
{
echo "<ul>";
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) // PLACE PRODUCTS INTO AN ARRAY
{
echo "<li><a href='#'><span>" . $row2['name'] . "</span></a></li>";
}
echo "</ul>";
}
echo "</li>";
}
}
?>