Change color of a td / td from html table according to php condition

0

In a table generated from data in a database, you would need to change the for certain colors of a <td></td> if the value of the ipaymentstatus field in the database is as: completed, pending, Canceled_Reversal.

Code php:

header('Content-Type: text/html; charset=utf-8');

// Conexão ao banco

$link = mysql_connect('localhost','paypalapi','123456');

// Seleciona o Banco de dados através da conexão acima

$conexao = mysql_select_db('paypalbd',$link); if($conexao){

$sql = "SELECT iname,iemail,ipaymentstatus,itransaction_date FROM ibn_table ORDER by itransaction_date DESC"; //Exibir últimos 10 registros, DESC

$consulta = mysql_query($sql);

echo '<table class="table">'; //table table-hover

echo '<tr>';

echo '<td><b>Cliente</b></td>';

echo '<td><b>Email</b></td>';

echo '<td><b>Status</b></td>';

echo '<td><b>Data</b></td>';

echo '</tr>';

// Armazena os dados da consulta em um array associativo

while($registro = mysql_fetch_assoc($consulta)){

echo '<tr>';

echo '<td>'.$registro["iname"].'</td>';

echo '<td>'.$registro["iemail"].'</td>';

//echo '<td>'.$registro["ipaymentstatus"].'</td>';

// Se o pagamento é aprovado seleciona cor verde

if ($registros["ipaymentstatus"] == 'Completed') {
  echo '<td style="background: #222; color: #fff;">'.$registro["ipaymentstatus"].'</td>';
 } else {
 echo '<td style="background: #ccc; color: #222;">'.$registro["ipaymentstatus"].'</td>';

 }


echo '<td>'.$registro["itransaction_date"].'</td>';

echo '</tr>';

}

echo '</table>';

}
    
asked by anonymous 17.07.2015 / 02:51

1 answer

3

Good evening. Having made the connection to the database and the listing of all the records, we call the completPayment() function to control the CSS background colors.

Listing with the function mentioned:

header('Content-Type: text/html; charset=utf-8');

// Conexão ao banco
$link = mysql_connect('localhost','paypalapi','123456');

// Seleciona o Banco de dados através da conexão acima
$conexao = mysql_select_db('paypalbd',$link); if($conexao){
$sql = "SELECT iname,iemail,ipaymentstatus,itransaction_date FROM ibn_table ORDER by itransaction_date DESC"; //Exibir últimos 10 registros, DESC

$consulta = mysql_query($sql);
    echo '<table class="table">'; //table table-hover
    echo '<tr>';
    echo '<td><b>Cliente</b></td>';
    echo '<td><b>Email</b></td>';
    echo '<td><b>Status</b></td>';
    echo '<td><b>Data</b></td>';
    echo '</tr>';

// Armazena os dados da consulta em um array associativo
while($registro = mysql_fetch_assoc($consulta)){
    echo '<tr>';
    echo '<td>'.$registro["iname"].'</td>';
    echo '<td>'.$registro["iemail"].'</td>';
    //echo '<td>'.$registro["ipaymentstatus"].'</td>';
    // Se o pagamento é aprovado seleciona cor verde

        $color = completePayment($registro["ipaymentstatus"]);
        echo "<td style='background: {$color}; color: #fff;'>".$registro["ipaymentstatus"]."</td>";

    echo '<td>'.$registro["itransaction_date"].'</td>';
    echo '</tr>';
}

    echo '</table>';
}


function completePayment($cod){

    switch($cod){
        default:
            $color = "#FFFFFF";
            break;

        case 'completed':
            $color = "red";
            break;

        case 'two':
            $color = "black";
            break;
    }

    return $color;

}

Do not forget to change $ color inside the function and within case {retorno.Tabela} to do the full scan. Hope this helps!

    
17.07.2015 / 03:04