If array returns value 1 display online badge

im currently making a game server page, i have a badge next to each game server which displays online how could i make it work real time, so it displays when online display

<td><label class="badge badge-success">Online</label></td>

         when offline display 

<td><label class="badge badge-danger">Offline</label></td>

the query returns back gq_online 1 if the value is 0 how can i make this set the badge 1 online for online badge 0 for offline badge?

<tbody>
                <tr scope="row">
                </tr>
    <tr>
    <?php $counter = 1; ?>
      <?php foreach ($results as $server) : ?>
          <th scope="row"><?php echo $counter++; ?></th>
          <td><?php echo $server['gq_address'] ?? 'N/A'; ?></td>
          <td><?php echo $server['gq_port_client'] ?? 'N/A'; ?></td>
          <td><?php echo $server['gq_hostname'] ?? 'N/A'; ?></td>
          <td><?php echo $server['gq_mapname'] ?? 'N/A'; ?></td>
          <td><?php echo $server['gq_name'] ?? 'N/A'; ?></td>
          <td><?php echo $server['gq_numplayers'] ?? 0; ?></td>   
         <td><label class="badge badge-success">Online</label></td>
        </tr>
      <?php endforeach; ?>
              </tbody>

im confused with how to if echo for an array that has text and a number at the end

<td>
    <?php if ($server['gq_online']): ?>
        <label class="badge badge-success">Online</label>
    <?php else: ?>
        <label class="badge badge-danger">Offline</label>
    <?php endif; ?>
</td>

thanks that did it

1 Like

You can tighten up the code like so:

<label class="badge <?= $server['gq_online'] ? 'badge-success' : 'badge-danger'; ?>">
    <?= $server['gq_online'] ? 'Online' : 'Offline'; ?>
</label>
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.