Forum Moderators: coopster
I am currently trying to run a loop through my database and seperate my information into two seperate lists.
I basically want my results to appear like this:
Result 1 - Result 2
Result 3 - Result 4
Result 5 - Result 6
Unfortunaly, my output is a little more than plain text.
The CSS I wrote consists of many divs holding other divs together. To put it simply - to display the page properly, I would need to generate the following html:
<div id="left">
Result 1
Result 3
Result 5
</div>
<div id="right">
Result 2
Result 4
Result 6
</div>
So now I'm stuck with a logic issue because I need to loop through my database missing all the odd or even numbers, and then come back around until I get a Nth result.
Bearing in mind my database has gaps from deleted/banned users.
Any help would be fantastic!
Kindest regards,
Tom.
<?$count = 1;
$string_even = '';
$string_odd = '';
while(...)
{
if($count % 2)
{
$string_even .= ...
}
else
{
$string_odd .= ...
}
$count++;
}print '<div id="left">' . $string_even . '</div><div id="right">' . $string_odd . '</div>';
?>
or did I misunderstand you?
Why not use tables to hold your data?
<?php$results = array('result 1', 'result 2', '', 'result 3',
'result 4', 'result 5', 'result 6',
'result 7');
$cellCounter = 0;
$tableRows = '';
$rowNotClosed = 0;
foreach($results as $value){
if($value != '' && isset($value)) // Do we have data?
{
switch($cellCounter)
{
case 0: // Start new row
$tableRows .= "<tr><td>" . $value . "</td>";
$cellCounter++;
$rowNotClosed = 1;
break;
case 1:
$tableRows .= "<td>" . $value . "</td></tr>";
$cellCounter = 0; // Reset for next row
$rowNotClosed = 0;
break;
}
}
}
if($rowNotClosed) { $tableRows .= "<td></td></tr>"; }
?>
<table border=1>
<?php print $tableRows; ?>
</table>
-M .Cold
I had to self contain my divs so I could print them one at a time and then had to make two extra classes.
@ milocold: I use divs because they're better HTML than tables I believe. Tables are out of date, so I've read and been told.
Google this: why divs, not tables
You get quite an array of results regarding the topic.
I do know that for designing purposes tables are to be avoided, but we aren't talking about layouts. We're talking about displaying data.
I don't agree that tables themselves are outdated at all. They serve a single purpose, to display tabular data, and they do that well. Table layouts on the other hand are outdated and best to avoid. =)
On that note: Power to the "blink" tag! ;)
Thanks,
-M .Cold
Its just, the approach I had already tried was similar to Janharders soloution. It didn't logically work, because theres several results to be returned. Unless you can increment the variable name?
Otherwise I end up with many containing divs, wheras I only need two - one for left and one for right. Each div, then has many lines.
@ milocold: And Marquee! :D