PHP Table background color

Asked

Viewed 2,040 times

-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’m having a problem putting background color on these two lines:

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

Here on this I would like to put a rule because you will receive dates. If the date is already passed the rectangle is red.

 <td>'.$row[2].'</td>
  • I don’t understand what the doubt is.

  • The syntax of your code is wrong...

  • Wrong because? This data will be exported to Excel

  • Rephrase the question when possible because it is complicated to have to guess what you meant. You cite that you have "a problem with putting background color on these two lines" and on the next line you place the code where you enter the tag of a spine in a PHP variable. Where are the two lines? <td> is different from line.

3 answers

1

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

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

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


Applying in 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>

-1

Use CSS instead of the attribute bgcolor. In HTML 5 this attribute is no longer supported for <td>, <tr>, and <table> as reported on W3schools' own website (http://www.w3schools.com/tags/att_td_bgcolor.asp).

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

-1

I think you could make it more organized, think about PDO and bootstrap, I tried to save your script, but I think you’ll still need 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.