Jump to content


do I need to select into an array?

  • Please log in to reply
3 replies to this topic

#1 ICEcoffee

  • Members
  • PipPip
  • Member
  • 19 posts

Posted 20 June 2006 - 03:54 PM

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.

#2 Kris

  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 June 2006 - 04:54 PM

You could keep a running count like so...
$c = 0;
while($row = mysql_fetch_assoc($result)) {
    //Do your stuff here
    echo "Row: $c<br/>";

#3 mainewoods

  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 20 June 2006 - 05:30 PM

After you execute the sql you'll want to get the number of rows returned:
$totalnumrows = mysql_num_rows ($result );
as you are looping through the rusults and printing rows, modify the tr your code outputs depending on the current row being worked on:
    $rowsprinted = 0;
    while($row = mysql_fetch_assoc($result)) {
        $currentpercent = ($rowsprinted/$totalnumrows)*100;
        if ($currentpercent < 31)
            echo '<tr style="background-color:#rgbhexcolor1">';
        else if ($currentpercent < 61)
            echo '<tr style="background-color:#rgbhexcolor2">';
            echo '<tr style="background-color:#rgbhexcolor3">';
        //output of all your <td>s and data from database goes here
        echo '</tr>'; //close table row
--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:
<td background-color="#rgbhexcolor1">
--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"

#4 ICEcoffee

  • Members
  • PipPip
  • Member
  • 19 posts

Posted 20 June 2006 - 06:22 PM

Thanks mainewoods, that looks like what I need, I will give it a go.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users