user5626896
user5626896

Reputation:

CSS, PHP, mySQL Loop - change td colour depending on variable

Having an issue with assigning a td style class, depending on a variable from mySQL. I have a field called 'Avail' so when a seat is available, the seat should turn green. However, nothing is happening so far when I run it.

Have created the simple classes on the same html sheet but it doesn't seem to apply to any of the data.

CSS Class Definitions >>

<style>
.available {
    background-color: green;
}

.unavailable {
    background-color: red;  
}
</style>

Code to show when style will be applied >>

<table>
    <tr>
    <th>Seats</th>
    <tr>
    <tr>
        <?php
        $c = 0; // Our counter
        foreach($res as $row) {
            $Avail = 'Avail';
            $seatColour = null;
            if($Avail == 0)
            {
                $seatColour =  '.available';
            } else
            {
                $seatColour = '.unavailable';
            }
            if($c % 20 == 0 && $c != 0) // If $c is divisible by $n...
            {
                // New row
                echo '</tr><tr>';
            }
            $c++;
            ?>
        <td style="$seatColour">
            <form method="POST" name="Seating" action="SWG.php">         
                <?php echo $row['Seat']; ?> <?php echo $row['Avail']; ?> 
                <input type="checkbox" name="Seat[]" value="<?php echo $row['Seat'];?>"/> 
        </td>
        <?php 
        } 
        ?>
    </tr>
</table>

Would really appreciate it if someone could point out where I'm wrong / how I'm assigning it incorrectly.

Upvotes: 0

Views: 259

Answers (3)

ANSHUL GERA
ANSHUL GERA

Reputation: 306

instead of null try -1 . or you put name/value for th.

Upvotes: -1

Sam
Sam

Reputation: 519

There seem to be several issues with your code:

$Avail = 'Avail';
$seatColour = null;
    if($Avail == 0)
  1. In the above code, you assign $Avail a string, and then you check whether it's zero. I'm assuming this is not deliberate. Maybe you want to assign the database value here (instead of a string)?

    $seatColour = '.available';

  2. You probably want to do $seatColour = "available". We're not in jQuery so no need for the dot.

    td style="$seatColour"

  3. You probably want to do <td class="<?php echo $seatColour ?>"> over here

Upvotes: 3

Rav
Rav

Reputation: 300

Use class attribute instead of style in td and remove . from $seatColour as follows:

<table>
    <tr>
          <th>Seats</th>
    <tr>
    <tr>
         <?php
        $c = 0; // Our counter
        foreach($res as $row) {
            $Avail = 'Avail';
            $seatColour = null;
            if($Avail == 0)
            {
                $seatColour =  'available';
            } else {
                $seatColour = 'unavailable';
            }

            if($c % 20 == 0 && $c != 0) // If $c is divisible by $n...
            {
               // New row
               echo '</tr><tr>';
            }
            $c++;
        ?>
        <td class="$seatColour"><form method="POST" name="Seating" action="SWG.php">         
        <?php echo $row['Seat']; ?> <?php echo $row['Avail']; ?> 
        <input type="checkbox" name="Seat[]" value="<?php echo $row['Seat'];?>"/> 
        </td>
        <?php } ?>
    </tr>
</table>

Upvotes: 0

Related Questions