CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2014
    Posts
    2

    MySQL table rows colors

    here it is my code to print headers with rows from MySQL table:

    PHP Code:
    while($row mysql_fetch_row($result))
    {
        echo 
    "<tr class='tr4'>";

        
    // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        
    foreach($row as $cell)
            echo 
    "<td>$cell</td>";

        echo 
    "</tr>\n";

    I would like to ask how could i change header "Baigtis" cell to green (if WIN), red (if LOSE), gray (if DRAW).

    Name:  BLdYmt.jpg
Views: 404
Size:  15.9 KB

  2. #2
    Join Date
    Nov 2014
    Posts
    2

    Re: MySQL table rows colors

    Full Script:

    PHP Code:
    <?php
    $db_host 
    'localhost';
    $db_user 'root';
    $db_pwd '';
    $database 'test';
    $table 'progs';

    if (!
    mysql_connect($db_host$db_user$db_pwd))
        die(
    "Can't connect to database");

    if (!
    mysql_select_db($database))
        die(
    "Can't select database");

    // sending query
    $result mysql_query("SELECT * FROM {$table}");
    if (!
    $result) {
        die(
    "Query to show fields from table failed");
    }

    $fields_num mysql_num_fields($result);

    echo 
    "<table cellpadding='2' align='center' width='100%'><tr class='tr3'>";
    // printing table headers
    for($i=0$i<$fields_num$i++)
    {
        
    $field mysql_fetch_field($result);
        echo 
    "<td>{$field->name}</td>";
    }
    echo 
    "</tr>\n";
    // printing table rows
    while($row mysql_fetch_row($result))
    {
        echo 
    "<tr class='tr4'>";

        
    // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        
    foreach($row as $cell)
            echo 
    "<td>$cell</td>";

        echo 
    "</tr>\n";
    }
    mysql_free_result($result);
    ?>

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: MySQL table rows colors

    Since you're using a foreach loop, just calculate the last $cell and then compare the string output of WIN or LOSE.

    PHP Code:
    foreach($row as $k => $cell) {
      
    $bg '#ffffff';
      if (
    $k == count($row) - 1) { // only alter if last cell in row
        
    $bg = ($cell == 'WIN') ? '#00ff00' '#ff0000';
      }
      echo 
    '<td style="background: ' $bg ';">' $cell '</td>';

    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured