Jump to content

do I need to select into an array?


ICEcoffee

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.

Link to comment
https://forums.phpfreaks.com/topic/12471-do-i-need-to-select-into-an-array/
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"

Archived

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

×
×
  • Create New...

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.