Add class to PHP if SQL field is 'Yes'

Viewed 33

I'm creating a site that lists custom userbars in a forum, and am using both PHP and SQL to achieve this easily. My question is: If a field is listed 'active' for the group, how do I go about adding a class specifically for that, to enable a glow around the Bootstrap card?

Here's my current code (forgive me if it's terrible)

     <div class="cards">
      <?php
      include_once("assets/php/db.php");
      $sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
      $resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));      
      while( $record = mysqli_fetch_assoc($resultset) ) {
      ?>
      <div class="col-sm-4 col-card">
                <img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
                <hr>
                <h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
                <span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
      </div>
      <?php } ?>
      </div>
1 Answers

You can use a ternary operator like this:

<div class="cards">
      <?php
      include_once("assets/php/db.php");
      $sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
      $resultset = mysqli_query($conn, $sql);      
      while( $record = mysqli_fetch_assoc($resultset) ) {
      ?>
      <div class="col-sm-4 col-card <?php $record['isActiveGroup'] === 'yes' ? 'ADD-CLASS-HERE' : ''; ?>">
                <img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
                <hr>
                <h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
                <span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
      </div>
      <?php } ?>
      </div>

You can change the value of 'yes' to the value of the active group that is saved on your database.

Related