0

Hi guys,

I have one simple logical question that I seem not to figure it out, sadly...

We have a list of objects and I loop trough them, list of products.
We need to create anchor maps for each category, so I did a comparison with temp variables to check the previous and current element value.

How can I display only the first occurence of a series of values for each category ?

Desired result:

Category 1
- product 1
- product 2
- product 3
Category 2
- product 1
- product 2
- product 3

    $previous = '';
    if(!empty($promotii)) {
        foreach($promotii as $promotie) {
            $previous = $promotie->Marca;
        }
        $previous = $promotie->Marca;
     }

Edited by Szabi Zsoldos

2
Contributors
2
Replies
14
Views
2 Years
Discussion Span
Last Post by Szabi Zsoldos
1

Something like this:

$previous = '';
foreach($promotii as $promotie) 
{
    if ($previous != $promotie->Marca)
    {
        if (!empty($previous))
        {
            echo '</ul>';
        }

        $previous != $promotie->Marca;

        echo $promotie->Marca . '<ul>';
    }

    echo "<li>$promotie->Product</li>";
}

if (!empty($previous))
{
    echo '</ul>';
}
0

Thanks pritaeas for the heads up, it was a great start :)
This is my final implementation and it works great.

        echo '<table width="600" cellpadding="0" cellspacing="0" border="0" class="table table-condensed">';    
        echo '<tbody>';        
        $previous = '';
        foreach($promotii as $promotie) {            
            if ($previous != $promotie->Marca) {
                if (!empty($previous)) {
                } else {
                    $previous = $promotie->Marca;
                }

                echo '<tr>';
                    echo '<td>';
                        echo $promotie->Marca;
                    echo '</td>';
                echo '</tr>';                  
            }

            echo '<tr>'; // start product stuff
            echo '</tr>'; // end of product stuff

            $previous = $promotie->Marca;
        }
        echo '</tbody>';
        echo '</table>';      
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.