tags:

views:

62

answers:

3

Hi

I am currently working on a project where i am pulling out data using while($r = mysql_fetch_array($the_data))

i want to make the first, say 3-4 results a different background color and then leave the rest as already styled, i am sure there is some simple option here but i just don't know where to look really...

Hope you can help, thanks !

A: 
<?php    
$i = 0;
$r = mysql_fetch_array($the_data);
foreach($r as $row) {
    if($i <= 4) {
        // Do special styling...
    } else {
        // Do normal styling.
    }
    $i++;
}
?>

Or did I misunderstand?

KyleFarris
don't forget to increment?
meder
Haha, yep, I was editing it as you said that, I guess. Got in too much of a hurry.
KyleFarris
Hi, it does the styling right but unless i'm missing something it isn't cyclying through the data in the array just giving me the same one again and again
David
A: 

Are you looking for something like:

#specialResult {background-color: #fff; }
#normalResult {background-color: #000; }

So when you are looping through your while statement, you will want to keep track of what result number you are on:

$i = 0;
while (...)
{
    if ($i < 4) echo "<div class='specialResult'>";
    else echo "<div class='normalResult'>"; 

    .... rest of your code

    $i++;
}

for shorter looking code you could do:

$i = 0;
while (...)
{
    ?><div class='<?php echo ($i++ < 4 ?"specialResult":"normalResult); ?>'><?php

    .... rest of your code
}
statikfx
Thanks, got it working like a treat now.
David
A: 

You can also try something like :

$i = 0;
while ( ( $row = mysql_fetch_assoc ( $result ) ) && $i < 4 ) {
    /* Your first 4 rows */
    echo 'Special : ' . $row['title'];
    ++$i;   // Don't forget to increment
} while ( $row = mysql_fetch_assoc () ) {
    /* Normal way */
    echo $row['title'] . ' is already outdated';
}

And prefer mysql_fetch_assoc() instead of mysql_fetch_array() ;)

swordofpain
thanks for the input, will use mysql_fetch_assoc - seems to make more sense
David