1

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

4
Contributors
8
Replies
11
Views
5 Years
Discussion Span
Last Post by Hendo
2
$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++;
}

Edited by Szabi Zsoldos

0

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?

0

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++;
    }
}

Edited by broj1

0

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

0

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

Edited by broj1

0

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']);

Edited by broj1

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.