ICEcoffee Posted June 20, 2006 Share Posted June 20, 2006 Hi allI 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 egrow1 = redif row number is between 0 & 30% of max rows then yelllowif 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. Quote Link to comment https://forums.phpfreaks.com/topic/12471-do-i-need-to-select-into-an-array/ Share on other sites More sharing options...
zq29 Posted June 20, 2006 Share Posted June 20, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/12471-do-i-need-to-select-into-an-array/#findComment-47730 Share on other sites More sharing options...
mainewoods Posted June 20, 2006 Share Posted June 20, 2006 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" Quote Link to comment https://forums.phpfreaks.com/topic/12471-do-i-need-to-select-into-an-array/#findComment-47741 Share on other sites More sharing options...
ICEcoffee Posted June 20, 2006 Author Share Posted June 20, 2006 Thanks mainewoods, that looks like what I need, I will give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/12471-do-i-need-to-select-into-an-array/#findComment-47755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.