Change color of a <td></td> of the html table according to php condition

Asked

Viewed 4,157 times

0

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

Php code:

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

}

1 answer

3


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

Listing with the mentioned Function:

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

Don’t forget to change $color inside the function and inside the case {retorno.Tabela} to make the full check. I hope I’ve helped!

  • Well didactic example, worked perfectly, thank you!

  • Great! Need, the disposition.

Browser other questions tagged

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