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 && (!isset($products))) // 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>";
    }
for ($k=0 ; $k<3 ; ++$k)
{
    $query = "SELECT name FROM products WHERE prodGroup='$prodGroups[$k]'";
    $result = mysql_query($query);

    for ($j=0 ; $j<count($prodGroups) ; ++$j)
    {
        while ($row2 = mysql_fetch_array($result, MYSQL_ASSOC)) // PLACE PRODUCTS INTO AN ARRAY
        {
            $products[] = $row2['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>";
    }
}
?>

For some reason, I can only get products to display on the last iterate of the loop, and not all of them. A variable is obviously being over written but I cannot identify which one nor can I over come it?

EDIT Now I've incorporated appropriate indentation and curly braces, I'm experiencing issues where ALL products are now showing in only the last prodGroup rather than their relevant/appropriate ones

3
  • Your initial query selects name. Then in your loop, you're using that value in another query, SELECT name FROM products WHERE prodGroup='$prodGroups[$j] - why do a second query when you already have the name? I suspect that the second query will always come up blank. And this code would be much, much easier to read if you indented it. Commented Apr 29, 2013 at 16:45
  • In the loop; I'm selecting "name" from "products", whereas my initial query is selecting "name" from "prodGroups" Commented Apr 29, 2013 at 16:48
  • Mea culpa; so you are. Commented Apr 29, 2013 at 16:49

1 Answer 1

3

Use brackets in your loop, and for your own good, everywhere else.

  for ($j=0 ; $j<count($prodGroups) ; ++$j)
        $query = "SELECT name FROM products WHERE prodGroup='$prodGroups[$j]'";
        $result = mysql_query($query); // <- this won't be included in your loop
Sign up to request clarification or add additional context in comments.

6 Comments

+1 I can only get products to display on the last iterate of the loop Strange its the only one with curly brackets eh!
Which wouldn't be included? The result or result+query? I beg to differ because there are a total of three product groups (for example), therefore I need to retrieve products from all product groups. As a consequence it is in my loop because "prodGroup='$prodGroups[$j]'" is vital.
I meant that you're just setting your $query variable in your loop, but do not actually make an actual query (you do, but only once). I don't know about other conditions/loops. That code should not only be indented (we're not using Python here), but the brackets should be place accordingly, if there's more that one operation after the loop/condition
You're right I am too familiar with Python in some aspects, of which seems to be the most important aspects as now I've implemented curly braces into my loops the outcome is different and sadly not how I intend. Thank you for pointing this out to me.
@Waygood FYI, single liner loops/statements do not require curly braces :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.