Jump to content

SET Color of top rows (this is not the alternate rows question)


grrraaagh

Recommended Posts

Please note this is not the frequent 'how do i colour rows alternately' question that you see across the internet.

 

Upon displaying a sports league table i wish for the top two rows to have a certain background colour. The next two rows to be a different shade again.

 

This would help highlight, for instance, that the top two teams qualify direct to the finals of a coompetition, whilst the third and fourth placed teams go into a play-off match for the third qualifying slot.

 

The rest of the table would just be the default colour (white).

 

I've Googled for hours and exhaustively tried every function i could find, all without success.

 

<html><body>
TABLES TEST<BR>
<H3>Group A</H3>
<?php
$conn=mysql_connect("localhost", "server", "password") or die ("could not connect");
$rs=mysql_select_db("database", $conn)or die ("could not connect to database");
$sql=stripslashes("SELECT `stage`,`team`,`p`,`pts`,`w`,`d`,`l`,`gf`,`ga`, (gf - ga) AS 'gd' FROM `lgtables` WHERE `stage` LIKE 'GP A' ORDER BY 'pts' desc, 'gd' desc");
$rs=mysql_query($sql,$conn) or die("could not execute query because ".mysql_error());
$list="<table border=\"1\" cellpadding=\"2\">";
$list.="<tr><th>Team</th>";
$list.="<th>P</th>";
$list.="<th>PTS</th>";
$list.="<th>W</th>";
$list.="<th>D</th>";
$list.="<th>L</th>";
$list.="<th>GF</th>";
$list.="<th>GA</th>";
$list.="<th>GD</th></tr>";
while($row=mysql_fetch_array($rs))
{
$list.="<tr bgcolor=\"#ffcc99\">";
$list.="<td><b>".$row["team"]."</b></td>";
$list.="<td><center><b>".$row["p"]."</b></center></td>";
$list.="<td><center><b>".$row["pts"]."</b></center></td>";
$list.="<td><center>".$row["w"]."</center></td>";
$list.="<td><center>".$row["d"]."</center></td>";
$list.="<td><center>".$row["l"]."</center></td>";
$list.="<td><center>".$row["gf"]."</center></td>";
$list.="<td><center>".$row["ga"]."</center></td>";
$list.="<td><center><b>".$row["gd"]."</b></center></td>";
$list.="</tr>";
}
$list.="</table>";
echo($list); 
?>

</body></html>

 

I've tried various formulas around firstly stating that

$row_color1 = "#ffcc99";
$row_color2 = "#ffcc66";

 

Thereafter, i've tried a bewildering array of tailoring suggestions for 'alternate rows' from the internet. These have used $row_count, $mysql_num_rows, 'if' statements, 'else if' statements etc.

 

I finally gave up when the 'mysql_if_this_does_not_work_i'll_jump_off_a_very_high_building' function also failed to work.

Link to comment
Share on other sites

Give this method a try:

 

<html><body>
TABLES TEST<BR>
<H3>Group A</H3>

<?php

$conn=mysql_connect("localhost", "server", "password") or die("could not connect");
$rs=mysql_select_db("database", $conn)or die("could not connect to database");
$sql=stripslashes("SELECT `stage`,`team`,`p`,`pts`,`w`,`d`,`l`,`gf`,`ga`, (gf - ga) AS 'gd' FROM `lgtables` WHERE `stage` LIKE 'GP A' ORDER BY 'pts' desc, 'gd' desc");
$num = mysql_num_rows($sql);
$rs=mysql_query($sql,$conn) or die("could not execute query because ".mysql_error());

$list="<table border=\"1\" cellpadding=\"2\">";
$list.="<tr><th>Team</th>";
$list.="<th>P</th>";
$list.="<th>PTS</th>";
$list.="<th>W</th>";
$list.="<th>D</th>";
$list.="<th>L</th>";
$list.="<th>GF</th>";
$list.="<th>GA</th>";
$list.="<th>GD</th></tr>";

for ($i=1; $i<$num; $i++) {
    $row = mysql_fetch_assoc($sql);
    
    if ($i == 1 || $i == 2) $bg = '#ffcc99';
    else if ($i == 3 || $i == 4) $bg = '#ffcc66';
    else $bg = 'white';
    
    $list.="<tr bgcolor=\"$bg\">";
    $list.="<td><b>".$row["team"]."</b></td>";
    $list.="<td><center><b>".$row["p"]."</b></center></td>";
    $list.="<td><center><b>".$row["pts"]."</b></center></td>";
    $list.="<td><center>".$row["w"]."</center></td>";
    $list.="<td><center>".$row["d"]."</center></td>";
    $list.="<td><center>".$row["l"]."</center></td>";
    $list.="<td><center>".$row["gf"]."</center></td>";
    $list.="<td><center>".$row["ga"]."</center></td>";
    $list.="<td><center><b>".$row["gd"]."</b></center></td>";
    $list.="</tr>";
}
$list.="</table>";
echo($list);
?>

</body></html>

Link to comment
Share on other sites

Your reply looked really promising, and gave me food for thought in trying new angles on the problem. The logic of it reads to me - a novice - as seeming to be just the job.

 

Unfortunately, upon trying your solution it just offered up the table headings with a white background. I then inserted a section of code that was removed from your sample namely,

while($row=mysql_fetch_array($rs))
{

as it threw me that your code slotted straight between the two Slist sections. However, i was obviously wrong in adding that as i received an error stating 'Parse error: parse error, unexpected $ in ...... on line 45

'

Give this method a try:

 

<html><body>
TABLES TEST<BR>
<H3>Group A</H3>

<?php

$conn=mysql_connect("localhost", "server", "password") or die("could not connect");
$rs=mysql_select_db("database", $conn)or die("could not connect to database");
$sql=stripslashes("SELECT `stage`,`team`,`p`,`pts`,`w`,`d`,`l`,`gf`,`ga`, (gf - ga) AS 'gd' FROM `lgtables` WHERE `stage` LIKE 'GP A' ORDER BY 'pts' desc, 'gd' desc");
$num = mysql_num_rows($sql);
$rs=mysql_query($sql,$conn) or die("could not execute query because ".mysql_error());

$list="<table border=\"1\" cellpadding=\"2\">";
$list.="<tr><th>Team</th>";
$list.="<th>P</th>";
$list.="<th>PTS</th>";
$list.="<th>W</th>";
$list.="<th>D</th>";
$list.="<th>L</th>";
$list.="<th>GF</th>";
$list.="<th>GA</th>";
$list.="<th>GD</th></tr>";

for ($i=1; $i<$num; $i++) {
    $row = mysql_fetch_assoc($sql);
    
    if ($i == 1 || $i == 2) $bg = '#ffcc99';
    else if ($i == 3 || $i == 4) $bg = '#ffcc66';
    else $bg = 'white';
    
    $list.="<tr bgcolor=\"$bg\">";
    $list.="<td><b>".$row["team"]."</b></td>";
    $list.="<td><center><b>".$row["p"]."</b></center></td>";
    $list.="<td><center><b>".$row["pts"]."</b></center></td>";
    $list.="<td><center>".$row["w"]."</center></td>";
    $list.="<td><center>".$row["d"]."</center></td>";
    $list.="<td><center>".$row["l"]."</center></td>";
    $list.="<td><center>".$row["gf"]."</center></td>";
    $list.="<td><center>".$row["ga"]."</center></td>";
    $list.="<td><center><b>".$row["gd"]."</b></center></td>";
    $list.="</tr>";
}
$list.="</table>";
echo($list);
?>

</body></html>

Link to comment
Share on other sites

Triumphantly, i declare that the solution to the problem is as follows;

 

<html><body>
TABLES TEST<BR>
<H3>Group A</H3>
<?php
$conn=mysql_connect("localhost", "server", "password") or die ("could not connect");
$rs=mysql_select_db("database", $conn)or die ("could not connect to database");
$sql=stripslashes("SELECT `stage`,`team`,`p`,`pts`,`w`,`d`,`l`,`gf`,`ga`, (gf - ga) AS 'gd' FROM `lgtables` WHERE `stage` LIKE 'GP A' ORDER BY 'pts' desc, 'gd' desc");
$rs=mysql_query($sql,$conn) or die("could not execute query because ".mysql_error());
$list="<table border=\"1\" cellpadding=\"2\">";
$list.="<tr><th>Team</th>";
$list.="<th>P</th>";
$list.="<th>PTS</th>";
$list.="<th>W</th>";
$list.="<th>D</th>";
$list.="<th>L</th>";
$list.="<th>GF</th>";
$list.="<th>GA</th>";
$list.="<th>GD</th></tr>";
$counter = 1; 
while($row=mysql_fetch_array($rs)) 
{ 
   switch($counter++) 
   { 
      case 1: 
      case 2: 
         $color = '#ffcc99'; 
         break; 
      case 3: 
      case 4: 
         $color = '#ffcc66'; 
         break; 
      default: 
         $color = '#ffffff'; 
   } 
$list.="<tr style='background-color: $color'>"; 
$list.="<td><b>".$row["team"]."</b></td>";
$list.="<td><center><b>".$row["p"]."</b></center></td>";
$list.="<td><center><b>".$row["pts"]."</b></center></td>";
$list.="<td><center>".$row["w"]."</center></td>";
$list.="<td><center>".$row["d"]."</center></td>";
$list.="<td><center>".$row["l"]."</center></td>";
$list.="<td><center>".$row["gf"]."</center></td>";
$list.="<td><center>".$row["ga"]."</center></td>";
$list.="<td><center><b>".$row["gd"]."</b></center></td>";
$list.="</tr>";
}
$list.="</table>";
echo($list); 
?>

</body></html>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.