Hello all,

I'm trying to have a multi-colored border for a generated table on my website. The table is a 2 column, 18 row table, and I want the top 6 rows to be bordered in green (or cell shading in green), while the rest of the table uses the default color. If this were a standard table, it would be pretty simple...however, the table is generated from MySQL query and I have no clue on how to have that create the different coloring.

Any help would be appreciated.

Thanks,
Hendo

Code is usually helpful. Post some and we should be able to help.

$i = 0;
while($row = mysql_fetch_array($sql))
{
    echo '<tr '.(($i <= 6)?'class="yourClass"':'class="yourOtherClass"').'><td>yourText</td></tr>';
    $i++;
}

or

    $i = 0;
while($row = mysql_fetch_array($sql))
{
    echo '<tr '.(($i <= 6)?'style="border: 1px solid green"':'style="border: 1px solid black"').'><td>yourText</td></tr>';
    $i++;
}

Heh...sorry...here's the code.

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
        mysql_select_db($dbname);
        $sql = "SELECT PlayerID, SUM(Points) AS TotalPoints FROM PlayIN WHERE SeasonID=10 GROUP BY PlayerID ORDER BY TotalPoints DESC";
        $pnts = mysql_query($sql);

        if (!$pnts) {
            printf("NOT ready\n$sql\n".mysql_error());
        } else {
            while ($myrow = mysql_fetch_array($pnts)) {
                $sql = "SELECT * FROM `Player` WHERE `PlayerID` = " . $myrow["PlayerID"];
                //echo $sql;
                $playerar = mysql_query($sql);
                    if ($playerfetch = mysql_fetch_array($playerar)) {
                    $fullname = $playerfetch["FirstName"] . " " . $playerfetch["LastName"];
                }
                printf('<tr><td align="center">%s</td><td align="center">%s</td></tr>', $fullname, $myrow["TotalPoints"]);
            }
        }

This code pulls from my database to show player name and points. I want the top 6 rows listed to be in green. Is that possible?

szabizs gave you excellent example in his post above. I am merely adapting it to your code:

} else {

    // counter for rows
    $currentRow = 1;

    while ($myrow = mysql_fetch_array($pnts)) {
        $sql = "SELECT * FROM `Player` WHERE `PlayerID` = " . $myrow["PlayerID"];
        //echo $sql;
        $playerar = mysql_query($sql);
            if ($playerfetch = mysql_fetch_array($playerar)) {
            $fullname = $playerfetch["FirstName"] . " " . $playerfetch["LastName"];
        }

        // define style depending on which row we are in
        $style = $currentRow <= 6 ? 'border: 1px solid green;' : 'border: 1px solid black;';

        // print the row using appropriate style for cells (or you can use it for rows)
        printf('<tr><td align="center" style="$style">%s</td><td align="center" style="$style">%s</td></tr>', $fullname, $myrow["TotalPoints"]);

        $currentRow++;
    }
}

Would the CSS take presidence over the style in the code? The table came up in it's original format.

The order of applying styles is something like:

  1. the browser default styles
  2. the styles in external css files (if exist) override browser default styles
  3. the styles in head head override previous two (if either exist)
  4. the inline declaration overrides any other (if any exist)
  5. the @important modifier overrides most if not all of others

Sorry, my mistake in the post before your last question. If we want to use $style variable in the printf statement, the parameter should be enclosed in double quotes (and normal double quotes escaped):

printf("<tr><td align=\"center\" style=\"$style\">%s</td><td align=\"center\" style=\"$style\">%s</td></tr>", $fullname, $myrow['TotalPoints']);

One more edit: if the table borders are not collapsed it is better to aplly styles to <tr> tag as szabisz suggested:

printf("<tr style=\"$style\"><td align=\"center\">%s</td><td align=\"center\">%s</td></tr>", $fullname, $myrow['TotalPoints']);

Okay, that works fine. I'd have to go in and change my border sizes or the cell fill color, but the green border comes across good.

I really appreciate the help Broj1 and Szabisz.
Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.