php
  • html
  • colors
  • html-table
  • 2016-10-16 5 views 0 likes 
    0

    I "m den folgenden PHP-Code mit verschiedenen Zellen in meiner HTML-Tabelle zu färben, aber frag dich, ob es ein besserer Weg, dies zu tun.Wie Tabellenzelle basierend auf dem Inhalt Farbe

    <?php if($crew['status'] == 'OUT') { ?> 
        <td style='text-align:center;font-size:80%;color:red' ><?php echo $crew['status'] ?></td> 
    <?php } else if($crew['status'] == 'OPEN') { ?> 
        <td style='text-align:center;font-size:80%;color:blue' ><?php echo $crew['status'] ?></td> 
    <?php } else if($crew['status'] == 'CONFIRMED') { ?> 
        <td style='text-align:center;font-size:80%;color:green' ><?php echo $crew['status'] ?></td> 
    <?php } else if($crew['status'] == 'WAITLIST') { ?> 
        <td style='text-align:center;font-size:80%;color:purple'><?php echo $crew['status'] ?></td> 
    <?php } else { ?> 
        <td style='text-align:center;font-size:80%;color:orange'>TIMESPAN</td> 
    <?php } ?> 
    

    Antwort

    1

    viel ... Sie sollten vermeiden viele Inline-CSS

    Sie so etwas wie das folgende tun könnte.

    <style> 
    #yourtable tr td { 
        text-align:center; 
        font-size:80%; 
    } 
    </style> 
    
    <?php 
    $colors = [ 
        'OUT' => 'red', 
        'OPEN' => 'blue', 
        'CONFIRMED' => 'green', 
        'WAITLIST' => 'purple' 
    ]; 
    ?> 
    
    <?php if (array_key_exists($crew['status'], $colors)): ?> 
    <td style="color:<?= $colors[$crew['status']]?>"><?= $crew['status'] ?></td> 
    <?php else: ?> 
    <td style="color:orange">TIMESPAN</td> 
    <?php endif ?> 
    
    +0

    Ich mag dies, aber warum die Zeile $ crew ['Status'] = 'OUT'; ? Ich glaube nicht, dass das notwendig ist – DCR

    +0

    Ihr Recht, ließ es in Test .. –

    1

    Dies ist, wie ich würde es tun:

    <td class="status-<?php echo $crew['status']; ?>><?php echo $crew['status'] ?></td> 
    
        ... 
    
        <style type="text/css"> 
        td.status { 
         text-align: center; 
         font-size: 80%; 
         color: orange; 
        } 
        td.status-OUT { color: red; } 
        td.status-OPEN { color: blue; } 
        td.status-CONFIRMED { color: green; } 
        td.status-WAITLIST { color: purple; } 
        </style> 
    
    Verwandte Themen