PHP Table color background

-2
$aux = mysql_num_rows($sql2);

 $html = '';
 $html .= '<table border="1">';
 $html .= '<tr>';
 $html .= '<td colspan="3"><b><Center>'.$row[0].' </center></b></td></tr>';

 $html .= '<tr><td align="center"><b>Instalador</b></td>';
 $html .= '<td bgcolor="#A9A9A9" align="center"><b>Morada</b></td>';
 $html .= '<td align="center"><b>Email</b></td>'

 </tr>';


 for($i=1; $i<=$aux; $i ++){
 $sql = mysql_query("SELECT *
 from Tabela WHERE id = $id = ".$i) or die(mysql_error());
 $row=mysql_fetch_array($sql);
 $html .= '<tr><td>'.$row[0].'</td>
 <td>'.$row[1].'</td>

 <td>'.$row[2].'</td>

 <td>'.$row[3].'</td>


 </tr>';


 }
 $html .= '</table>';

I have a problem putting background color on these two lines:

 $html .= '<td bgcolor="#A9A9A9" align="center"><b>Morada</b></td>';

Here in this one I like to put a rule because it will receive dates. If the date is already past the rectangle is red.

 <td>'.$row[2].'</td>
    
asked by anonymous 02.06.2014 / 18:50

3 answers

1

Assuming your $row[2] is a date in the format 07/08/2014

if( time() > strtotime( $row[2] ) )
    ...

. I recommend using PDO
. Contrary to popular belief, tables should not be abolished. Tables must TABELAS . Tableless does not mean not to use tables, but rather uses them for their purpose.

Applying to your code:

for($i=1; $i<=$aux; $i ++)
{
    $sql = mysql_query("SELECT * from Tabela WHERE id = $id = ".$i) or die(mysql_error());
    $row=mysql_fetch_array($sql);

    $style = null;

    if( time() > strtotime( $row[2] ) )
    $style = 'class = "data_vencida"';

    $html .= '<tr><td>' . $row[0] . '</td>
    <td>' . $row[1].'</td>
    <td ' . $style . '>' . $row[2] . '</td>
    <td>' . $row[3] . '</td>
    </tr>';
}


<style>
.data_vencida{background:#FF0000}
</style>
    
09.08.2014 / 01:33
-1

Use CSS instead of the bgcolor attribute. In HTML 5 this attribute is no longer supported for <td> , <tr> , and <table> as reported on the W3Schools own site ( ).

  

Compatibility Notes: The bgcolor attribute of is not supported in   HTML5. Use CSS instead.

For your case the code could look like this:

$html .= '<td style="background-color: #A9A9A9" align="center"><b>Morada</b></td>';
    
08.08.2014 / 21:44
-1

I think you could make it more organized, think of PDO and bootstrap, I tried to save your script, but I think it still needs adjustments to fit your need.

<?php
/* ok, mais acho que talvez um pdo fosse mais seguro. */
$aux = mysql_num_rows($sql2);

/* acho q se tu fizer o select antes de formar a tabela fica mais simples
 * com pdo tu poderia inclusive utilizar um try catch nas consultas, mais enfim...
 * define a consulta antes, loop de consultas é deselegante, 
 * fica mal para o programador apresentar algo deste tipo a um cliente,
 * ou até pra um cachorro...
 */

$query = mysql_query("SELECT * from TABELA WHERE id in(select id from TABELA where ...)") or die(mysql_error());

?>

<!-- mais uma dica, pesquisa sobre bootstrap,
    vale a pena por varios motivos, como acessibilidade, 
    padrão e profissionalismo -->

<table border="1">
    <tr>
        <td colspan="3">
            <b><Center>???</center></b>
        </td>
    </tr>
    <tr>
        <td align="center">
            <b>Instalador</b>
        </td>
        <td bgcolor="#A9A9A9" align="center">
            <b>Morada</b>
        </td>
        <td align="center">
            <b>Email</b>
        </td>
    </tr>
    <?php while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { ?>
    <tr>
        <td><?php print $row["nome"]; //passa o nome da coluna da sua tabela?></td>
        <td <?php if (!empty($row["data"])) { ?>style="background-color:#fff;"<?php } ?> ><?php print $row["data"]; //passa o nome da coluna da sua tabela ?></td>
        <td><?php print $row["blablabla"]; //passa o nome da coluna da sua tabela ?></td>
    </tr>
    <?php } ?>
</table>
    
08.08.2014 / 22:06