When there is a number < 6 turn a cell in another table red

Viewed 52

I am making an bike reservation system. It will take information from a form and puts this in database (done) After database the information is going in to a table (done). The information will also be deleted when the lending time is over (not done) In another table 2 i keep track on the bikes which we are lending (done). Now i want my code to read which bike is lent and turn the cell with its number red. When the cell with its number is empty is needs to get green.

I don't know how I can change cells in table 2 when it reads things that happen in the responsive table. Hopefully somebody can help me with this.

<!DOCTYPE html>
<html>
<head>

<title>Bike Waiver</title>
<!-- <link rel="stylesheet" type="text/css" href="mtb.css"> -->
<!- head elements (Meta, title, etc) -->
<!-- Link your php/css file -->
<link rel="stylesheet" href="mtb.php" media="screen">

</head>
<body>


<br>

<?php



echo "<table style='border: solid 1px black;'>";
 echo "<tr>
        <th>bike1</th> 
        <th>bike2</th> 
        <th>bike3</th> 
        <th>bike4</th> 
        <th>bike5</th> 
        <th>bike6</th>
        <th>Firstname</th> 
        <th>Lastname</th> 
        <th>hotel</th> 
        <th>roomNumber</th> 
        <th>email</th> 
        <th>startDate</th>
        <th>startTime</th>
        <th>returnDate</th>
        <th>endTime</th>
        <th>returnPlace</th>
        <th>numberBike</th> 
        <th>numberHelmet</th> 
        <th>numberPumps</th> 
 </tr>";

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "\n";
    }
}

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mtb";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT bike1, bike2, bike3, bike4, bike5, bike6, first_name, last_name, hotel, roomNumber, email, startDate, startTime, returnDate, endTime, returnPlace, numberBike, numberHelmet, numberPumps FROM mtbtable");
    $stmt->execute();
    

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";



?>

<br>

<?php



echo "<table style='border: solid 1px black;'>";
 echo "<tr>
 <th>bike1</th> 
 <th>bike2</th> 
 <th>bike3</th> 
 <th>bike4</th> 
 <th>bike5</th> 
 <th>bike6</th>
 <th>Firstname</th> 
 <th>Lastname</th> 
 <th>hotel</th> 
 <th>roomNumber</th> 
 <th>email</th> 
 <th>startDate</th>
 <th>startTime</th>
 <th>returnDate</th>
 <th>endTime</th>
 <th>returnPlace</th>
 <th>numberBike</th> 
 <th>numberHelmet</th> 
 <th>numberPumps</th>  
 </tr>";

class TableRows2 extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "\n";
    }
}



$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mtb";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT bike1, bike2, bike3, bike4, bike5, bike6, first_name, last_name, hotel, roomNumber, email, startDate, startTime, returnDate, endTime, returnPlace, numberBike, numberHelmet, numberPumps FROM mtbtable");
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}

$conn = null;
echo "</table>";

?>

<br>

<table>
<tr>
    <th>Number of bike</th> 
    
</tr>
<tr>
    <td class="td<?php echo $openColor; ?>" >1</td>
</tr>
<tr>
  <td>2</td>
  
</tr>
<tr>
  <td>3</td>
  
</tr>
<tr>
  <td>4</td>
  
</tr>
<tr>
  <td>5</td>
  
</tr>
</table>




</body>
</html>

1 Answers

You can use the ternary operator for this.

<td class="<?=  $isBikeLent ? 'bg-danger' : 'bg-success' ?>" >1</td>

if $isBikeLent is a 'truthy' value the first part will run 'bg-danger', else 'bg-success'.

The shorthand <?= ... ?> will automatically echo the string out, it's shorthand for <?php echo ... ?>

Related