Jump to content

How to use the if statement with data from a database?


lavarat

Recommended Posts

Hello everyone

 

This code is used to show my guest list in a table on my website that is pulled from a database. It displays the guest list fine BUT I want it to be more selective to who is shown on the guest list. The following part is where I run into an issue:

if (($line % 2 == 1)&&($row["is_finalized"]=="yes"))

else if ($row["is_finalized"]=="yes")

 

I only want to display the people who are actually finalized on my list. is_finalized is a table in my database that either has a yes or no parameter. Please note that when I remove ($row["is_finalized"]=="yes") all the entries in my database display fine but when I add the condition I get no output but no error either.

 

Please let me know what the correct syntax is to use in an if statement pulling data from a database row.

The entire code is displayed below.

Thanks!

 

 

<?
class guests
{
  /* mysql data */
  var $db_host  = "private";
  var $db_user  = "private";
  var $db_pass  = "private";
  var $db_name  = "private";
  var $db_tbl   = "private";


  /* will be filled in constructor */
  var $fast_mode;
  var $color_line;
  var $url;


  /* constructor - executed on object creation */
  function guests()
  {
    // set to 1 to skip confirmation on status change
    $this->fast_mode = 0;

    // every second row will be filled with this color
    $this->color_line = "#e4c494";

    // links    
    $this->url["list"]    = "guestlist.php";

    // connect to mysql and select database
    $link = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
    mysql_select_db($this->db_name, $link);
  }

  /* general list */
  function show_list()
  {
    $result = mysql_query("SELECT * FROM " . $this->db_tbl . " ORDER BY submission_id");

    echo "<table cellspacing=0 cellpadding=2 border=0>\n";
    echo "  <tr>\n";
    echo "	<td><b>ID</b></td>\n";
    echo "	<td width=80><b>First Name</b></td>\n";
    echo "	<td width=80><b>Last Name</b></td>\n";
    echo "	<td width=100><b>Alias</b></td>\n";
    echo "	<td width=100><b>Clan</b></td>\n";
    echo "	<td width=100><b>City</b></td>\n";
echo "	<td width=40><b>Paid</b></td>\n";
echo "  </tr>\n";

    $line = 1;
    while ($row = mysql_fetch_assoc($result)) {
      if (($line % 2 == 1)&&($row["is_finalized"]=="yes"))
        echo "  <tr bgcolor=" . $this->color_line . ">\n";
      else if ($row["is_finalized"]=="yes"){
        echo "  <tr>\n";	
      echo "    <td>" . $row["submission_id"] . "</td>\n";
      echo "    <td>" . $row["col_1"] . "</td>\n";
      echo "    <td>" . $row["col_2"] . "</td>\n";
  echo "    <td>" . $row["col_3"] . "</td>\n";
  echo "    <td>" . $row["col_4"] . "</td>\n";
  echo "    <td>" . $row["col_5"] . "</td>\n";
  echo "    <td>" . $row["col_7"] . "</td>\n";
  echo "  </tr>\n";
      $line++;
  	}
      }
echo "</table>\n";
    }
}
?>

I would set is_finalized to boolean in your database and then re-arrange your if statements a little. That way you can do this

$line = 1;
    while ($row = mysql_fetch_assoc($result)) {
      if ($row["is_finalized"])
      {
         $color_code=""; 
          if($line%2==1) {
           $color_code="bgcolor='".$this->color_line."'"; 
           }
        echo "  <tr ".$color_code. ">\n";

        echo "  <tr>\n";   
      echo "    <td>" . $row["submission_id"] . "</td>\n";
      echo "    <td>" . $row["col_1"] . "</td>\n";
      echo "    <td>" . $row["col_2"] . "</td>\n";
     echo "    <td>" . $row["col_3"] . "</td>\n";
     echo "    <td>" . $row["col_4"] . "</td>\n";
     echo "    <td>" . $row["col_5"] . "</td>\n";
     echo "    <td>" . $row["col_7"] . "</td>\n";
     echo "  </tr>\n";
      $line++;
        }
      }

like this

$line = 1;

    while ($row = mysql_fetch_assoc($result)) {

      if (strtolower($row["is_finalized"])=="yes")    // just in case it's Yes or YES

      {

        $color_code="";

          if($line%2==1) {

          $color_code="bgcolor='".$this->color_line."'";

          }

        echo "  <tr ".$color_code. ">\n";

 

        echo "  <tr>\n"; 

      echo "    <td>" . $row["submission_id"] . "</td>\n";

      echo "    <td>" . $row["col_1"] . "</td>\n";

      echo "    <td>" . $row["col_2"] . "</td>\n";

    echo "    <td>" . $row["col_3"] . "</td>\n";

    echo "    <td>" . $row["col_4"] . "</td>\n";

    echo "    <td>" . $row["col_5"] . "</td>\n";

    echo "    <td>" . $row["col_7"] . "</td>\n";

    echo "  </tr>\n";

      $line++;

        }

      }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.