Jump to content

Archived

This topic is now archived and is closed to further replies.

ICEcoffee

do I need to select into an array?

Recommended Posts

Hi all

I have a table of 2 cols, 1 for names and 1 for points. I currently have a select that fetches all rows and sorts them in descending order of points.

I want to colour code on screen, each row depending on its position to the 1st row eg

row1 = red
if row number is between 0 & 30% of max rows then yelllow
if row number is between 31 & 60% of max rows then orange....

You get the idea. I just dont know the best way to get the row number of the returned data.


Any help would be greatful, I'm stuck.

Share this post


Link to post
Share on other sites
You could keep a running count like so...
[code]<?php
$c = 0;
while($row = mysql_fetch_assoc($result)) {
    //Do your stuff here
    echo "Row: $c<br/>";
    $c++;
}
?>[/code]

Share this post


Link to post
Share on other sites
After you execute the sql you'll want to get the number of rows returned:
[code]$totalnumrows = mysql_num_rows ($result );[/code]
as you are looping through the rusults and printing rows, modify the tr your code outputs depending on the current row being worked on:
[code]<?php
    $rowsprinted = 0;
    while($row = mysql_fetch_assoc($result)) {
        $rowsprinted++;
        $currentpercent = ($rowsprinted/$totalnumrows)*100;
        if ($currentpercent < 31)
            echo '<tr style="background-color:#rgbhexcolor1">';
        else if ($currentpercent < 61)
            echo '<tr style="background-color:#rgbhexcolor2">';
        else
            echo '<tr style="background-color:#rgbhexcolor3">';
        //output of all your <td>s and data from database goes here
        echo '</tr>'; //close table row
    }
?>[/code]
--there may be some cross browser compatability problems with that but probably only with older browsers
--if it's not cross browser compatable enought for you, the most compatable would be to use the td instead of the tr and use this:
[code]<td background-color="#rgbhexcolor1">[/code]
--if you did it that way it would have to be repeated for each td used for each row. You can also use the style="background-color:="#rgbhexcolor1" inside the td's as well or use a css class defined in the head of your document: class="first30perc"

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.