Forum Moderators: coopster

Message Too Old, No Replies

Seperating MySQL Results During Loop

         

Tom_Cash

10:34 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



Hi there,

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.

janharders

11:47 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess the easiest way to do that would just be to use two strings for your results, like

<?

$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?

milocold

12:36 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Hi,

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

Tom_Cash

2:44 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Thanks fellas but both of those statements do what I have already acheived. However, I did figure it out eventually but not with PHP.

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.

Mahabub

3:29 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Dear Tom_Cash,

No doubt div is better than table. I think janharders solution is good one.When I read your post i saw janharders gave the solutions which i thought in my mind..:d

Thanks
Mahabub

milocold

11:33 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Hi,

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

Tom_Cash

9:36 am on Dec 19, 2008 (gmt 0)

10+ Year Member



Sorry fellas, I don't seem to come across ungreatful. :(

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

janharders

9:50 am on Dec 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe you misread the code I gave ;)
that's exactly how it works, two divs, each with multiple lines.

Tom_Cash

4:37 pm on Dec 21, 2008 (gmt 0)

10+ Year Member



Sorry, maybe I did. I'll give it a wiz when I next get chance to work on my site and get back to you.

I've got some of my final exams coming up after Christmas - busy times!