Displaying a different colors using while loop

1k views Asked by At

This is a parking lot system.

This is the one of my module. The error in this codes is the status cancel.

I want it the status cancel is will display in my system a color yellow like the status leave but still its not functioning. the reserve , occupied and yellow only us functioning.

<?php include 'dbcon.php';
    $result = mysqli_query($con,"SELECT * FROM `zonenumber` WHERE 1");
    while ($fetch = mysqli_fetch_array($result))
    {
      $name = $fetch['name'];
      $status = $fetch['status'];
      if ($status == 'Cancel') $color = 'yellow';
      if ($status == 'Reserved') $color = 'green';
      if ($status == 'Occupied') $color = 'red';
      if ($status == 'Leave') $color = 'yellow';
      if ($color != 'yellow')
      {
        $print = "javascript:popUp('zonestatus_1.php?id=$name');";
      }
      else
      {
        $print = "javascript:alert('There is NO Information Available')";
      }
      ?>
3

There are 3 answers

0
ScaisEdge On

Just a suggestion instead of severl if you could use a swicth statement

<?php include 'dbcon.php';
    $result = mysqli_query($con,"SELECT * FROM `zonenumber` WHERE 1");
    while ($fetch = mysqli_fetch_array($result))
    {

      $name = $fetch['name'];
      $status = $fetch['status'];
      switch ($status) {
        case 'Cancel':
        case 'Leave':
          $color = 'yellow';
          break;
         case 'Reserved':
          $color = 'green';
          break;
        case 'Occupied':
          $color = 'red';
          break;
        // eventually you can manage default 
        default:
          // your code for default
          break;
      }
      if ($color != 'yellow')
      {
        $print = "javascript:popUp('zonestatus_1.php?id=$name');";
      }
      else
      {
        $print = "javascript:alert('There is NO Information Available')";
      }
    }
?>

and close your while loop

0
Dean Fisher On

Hopefully you'll be good enough to understand the below. It's designed for VBScript...

While there is a result, the color is white. Loop. If the color is white (after the result is shown), the color is now grey. Loop. If the color is grey - change again!

<%
xBg="#cccccc" 
while not rs.eof
%>   

    <tr style="background-color: <% = xBg %>">
        <td>FIELDRESULT</td>
    </tr>

<% if xBg="#cccccc" then xBg="#ffffff" else xBg="#cccccc"
rs.MoveNext 
wend %> 
0
Subhash Shipu On

You can simply use if else conditions here. And check your where condition as well

 <?php 
include 'dbcon.php';
$result = mysqli_query($con,"SELECT * FROM `zonenumber` WHERE 1");
while ($fetch = mysqli_fetch_array($result))
{
  $name = $fetch['name'];
  $status = $fetch['status'];
  if($status == 'Cancel')
  {
     $color = 'yellow'; 
  }
  elseif($status == 'Reserved')
  {
    $color = 'green';  
  }
  elseif($status == 'Occupied')
  {
    $color = 'red';  
  }
  elseif($status == 'Leave')
  {
    $color = 'yellow';  
  }
  else
  {
      $color = '';   // your default color
  }

  if ($color != 'yellow')
  {
    $print = "javascript:popUp('zonestatus_1.php?id=$name');";
  }
  else
  {
    $print = "javascript:alert('There is NO Information Available')";
  }
}

?>