Hi,
I need to fetch output from a database which can return any number of results depending on what's available - then output results so that a 4x4 div grid is created, ie. like this:
<div class="grid">
<div class="box_left">
<img src="photo1.jpg" />
</div>
<div class="box_center">
<img src="photo2.jpg" />
</div>
<div class="box_center">
<img src="photo3.jpg" />
</div>
<div class="box_right">
<img src="photo4.jpg" />
</div>
<div class="separator"></div>
<div class="box_left">
<img src="photo5.jpg" />
</div>
<div class="box_center">
<img src="photo6.jpg" />
</div>
<div class="box_center">
<img src="photo7.jpg" />
</div>
<div class="box_right">
<img src="photo8.jpg" />
</div>
<div class="separator"></div>
<div>...and so forth..</div>
</div>
If I do a query and want to output it, I normally create a 'while' loop to get all available results.
Essentially, if the result set contains, say, 15 rows, I would want the output to be styled like so, ie every first result of a batch of four is styled with the 'box_left' class, and and every fourth result of a batch of four is styled with the 'box_right' class:
row 1: box_left
row 2: box_center
row 3: box_center
row 4: box_right
row 5: box_left
row 6: box_center
row 7: box_center
row 8: box_right
row 9: box_left
row 10: box_center
row 11: box_center
row 12: box_right
row 13: box_left
row 14: box_center
row 15: box_center
I've looked at a number of different while loops and tried altering them to suit my needs but I can't get it working right:
So if I have:
$query = $db->Query( "SELECT somedata FROM sometable" ); // Run query
$resultrows = $db->num_rows; // Return number of results
$i=1; // Start from row 1
while( $row = $db->Fetch( $query ) ) {
print( $row[ "content" ] );
if(++$i == 1) { // If first row
// Style with box_left
}
if(++$i == 4) { // If fourth row
// Style with box_right
// Insert separator <div>
}
$i=0; // Reset back to zero
}
I'm clearly going wrong somewhere but I can't figure out where. I've tried a number of different ways, using else and elseif. I'm running a brainfreeze and just can't think what's going wrong..
Thank you so much in advance!