doschni
doschni

Reputation: 43

how to close a div in a dynamic group?

With mysql left-join I have a result of 3 tables. Every group of machine-types has special requirements. Some machine-types have the same requirements, some not.

My problem is, to list them grouped and clear adressable for css-styling. I want to show all groups - with its requirements - but the group as header only one time.

How can I output and place a closing div to the right position?

$lastDate = ''; 


while($r = $result_all->fetch(PDO::FETCH_OBJ)) {

    $aktualisiert = $r->MTypID;


    if ( $aktualisiert != $lastDate ){ 


    echo '<div class="machine-type">'.$r->mtype'</div>';
    echo '<div class="requirements">';


    $lastDate = $aktualisiert; 


    }  


    //if ( $aktualisiert = $lastDate ){ 
    echo $r->baustand." ";

    //}

 }//endwhile

Output should be like this (see *</div>*):

<div class="machine-type">Model 1</div><div class="requirements">Softwareupdate 2.4.0 Valve M4 Tube 20cm *</div>*
<div class="machine-type">Model 2</div><div class="requirements">Softwareupdate 2.4.0 *</div>*

Upvotes: 0

Views: 25

Answers (2)

doschni
doschni

Reputation: 43

With adding of a separator (also a bit tricky), it's more useful. Here the final code - maybe interesting to somebody...

$lastDate = ''; 
$separator ='+';
$space =' ';

while($r = $result_all->fetch(PDO::FETCH_OBJ)) {

    $aktualisiert = $r->MTypID;

    if ( $aktualisiert == $lastDate ) echo $separator.$space;

    if ( $aktualisiert !== $lastDate ){ 

    if ( $lastDate !== '' ) echo '</div>'; //<!-- end requirements 1/2 --> 

    echo '<div class="machine-type">'.htmlspecialchars($r->mtype)'</div>';;
    echo '<div class="requirements">';

    $lastDate = $aktualisiert; 

    }

    echo htmlspecialchars($r->baustand).' ';

}//endwhile
echo '</div>'; //<!-- end requirements 2/2 -->

Upvotes: 0

Nick
Nick

Reputation: 147196

You just need to close the div when $lastDate changes, but not when it is the first row in the output. So after

if ( $aktualisiert != $lastDate ){ 

add

if ( $lastDate != '' ) echo "</div>";

and then after your loop add

echo "</div>";

Upvotes: 1

Related Questions