1
<?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() 删除循环中的数组内容,但是这只允许每个循环中的数组中有一个元素。

4

3 回答 3

2

之后立即添加$products = array();

   foreach ($prodGroups as $aGroup) // CYCLE THROUGH PRODUCT GROUPS
    {
于 2013-04-29T18:21:00.257 回答
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 的评论。

于 2013-04-29T18:23:05.460 回答
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>";
    }
}
?>
于 2013-04-29T18:18:29.520 回答