Jump to content

[SOLVED] If Statment help.


Lamez

Recommended Posts

I want to filter out all the online people, I have been messing around with this:

<?php /* Display table contents */
   $query = mysql_query("SELECT u.username FROM users u INNER JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND     u.username='$uname'")or die(mysql_error());
if (mysql_num_rows($query) > 0){
//somthing to filter out online peeps
} else {

  
   echo "<table width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n"; //Table's Specs
   echo "<tr>\n";
   echo "<td valign=\"bottom\"><strong>Username</strong></td>\n"; //Username Column
   echo "<td width=\"80\" valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Email</strong></td>\n"; //Email Column
   echo "<td valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Status</strong></td>\n"; //Status Column
   echo "</tr>\n";
   echo "<tr>\n";
   echo "<td height=\"1\" colspan=\"5\" align=\"center\" valign=\"top\"><div class=\"dot\"></div></td>\n"; //Dotted Line
   echo "</tr>\n";

   for($i=0; $i<$num_rows; $i++){
      $uname  = mysql_result($result,$i,"username");
   $email  = /*mysql_result($result,$i,"email");*/ "$uname";
      $time   = mysql_result($result,$i,"timestamp");
  
   echo "<tr>\n"; 
   echo "<td><a href=\"userinfo.php?user=$uname\">$uname</a><br></td>\n";
   echo "<td> </td>\n";
   echo "<td>$email</td>\n";
   echo "<td> </td>\n";
   echo "<td align=\"left\">\n";

/* Tells the if they are online or offline */	
$query = mysql_query("SELECT u.username FROM users u INNER JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND     u.username='$uname'")or die(mysql_error());
if (mysql_num_rows($query) > 0){
echo "<font color=\"#04db04\">Online</font>";
} else {
echo "<font color=\"#FF0000\">Offline</font>";
  }
}
   echo "</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
}
}
?>

 

I am not sure on how to filter them out. Any help?

 

also if I wanted to pull username, and timestamp from one table and add email for users from another table, would this be right?

MySql query:

$q = "SELECT username, timestamp "
."FROM".TBL_ACTIVE_USERS. "INNER JOIN".TBL_USERS."ON" .TBL_ACTIVE_USERS.username = .TBL_USERS.email "ORDER BY" timestamp DESC,username

 

 

Link to comment
https://forums.phpfreaks.com/topic/82220-solved-if-statment-help/
Share on other sites

Does active_users not have a username column? If it does, there is no reason to JOIN the two tables since that is the only field you are retrieving. Otherwise, I think you can do it without join. Something like:

"SELECT u.username FROM users u, active_users a WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND a.username='$uname' AND u.username=a.username";

 

I think that should only select the username from users that match the criteria presented to active_users.

it worked, but now it is not showing my table, ever thing is smooshed together.

http://www.lamezz.com/user/viewmembers.php?sort=offline

 

did I put my query in the wrong spot?

 

/*Sorts by Offline*/
function displayOffline(){
global $database;
$q = "SELECT username,email,timestamp "
       ."FROM ".TBL_USERS." ORDER BY timestamp DESC,username";
   

   $result = $database->query($q);
   /*Error occurred, return given name by default */
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
  
   /* Display table contents */
   echo "<table width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n"; //Table's Specs
   echo "<tr>\n";
   echo "<td valign=\"bottom\"><strong>Username</strong></td>\n"; //Username Column
   echo "<td width=\"80\" valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Email</strong></td>\n"; //Email Column
   echo "<td valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Status</strong></td>\n"; //Status Column
   echo "</tr>\n";
   echo "<tr>\n";
   echo "<td height=\"1\" colspan=\"5\" align=\"center\" valign=\"top\"><div class=\"dot\"></div></td>\n"; //Dotted Line
   echo "</tr>\n";

      for($i=0; $i<$num_rows; $i++){
      $uname  = mysql_result($result,$i,"username");
  $email  = mysql_result($result,$i,"email");
      $time   = mysql_result($result,$i,"timestamp");
  
	      $query = mysql_query("SELECT u.username FROM users u, active_users a WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND a.username='$uname' AND u.username=a.username")or die(mysql_error());
		  
if (mysql_num_rows($query) > 0){
//somthing to filter out online peeps
} else {  
//show offline peeps
    
   echo "<tr>\n"; 
   echo "<td><a href=\"userinfo.php?user=$uname\">$uname</a><br></td>\n";
   echo "<td> </td>\n";
   echo "<td>$email</td>\n";
   echo "<td> </td>\n";
   echo "<td align=\"left\">\n";
   /*displayStatus();*/
   echo "</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
}
}
}

 

also is it possible to call a function inside of a function?

I don't understand.

 

here is my latest code

/*Sorts by Offline*/
function displayOffline(){
global $database;
$q = "SELECT username,email,timestamp "
       ."FROM ".TBL_USERS." ORDER BY timestamp DESC,username";
   

   $result = $database->query($q);
   /*Error occurred, return given name by default */
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
  
   /* Display table contents */
   echo "<table width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n"; //Table's Specs
   echo "<tr>\n";
   echo "<td valign=\"bottom\"><strong>Username</strong></td>\n"; //Username Column
   echo "<td width=\"80\" valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Email</strong></td>\n"; //Email Column
   echo "<td valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Status</strong></td>\n"; //Status Column
   echo "</tr>\n";
   echo "<tr>\n";
   echo "<td height=\"1\" colspan=\"5\" align=\"center\" valign=\"top\"><div class=\"dot\"></div></td>\n"; //Dotted Line
   echo "</tr>\n";

      for($i=0; $i<$num_rows; $i++){
      $uname  = mysql_result($result,$i,"username");
  $email  = mysql_result($result,$i,"email");
      $time   = mysql_result($result,$i,"timestamp");
   	  $query = mysql_query("SELECT u.username FROM users u, active_users a WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND a.username='$uname' AND u.username=a.username")or die(mysql_error());
		  
if (mysql_num_rows($query) > 0){
//somthing to filter out online peeps
} else {  
//show offline peeps and hide online

    
   echo "<tr>\n"; 
   echo "<td><a href=\"userinfo.php?user=$uname\">$uname</a><br></td>\n";
   echo "<td> </td>\n";
   echo "<td>$email</td>\n";
   echo "<td> </td>\n";
   echo "<td align=\"left\">\n";
   
$online = mysql_query("SELECT u.username FROM users u INNER JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND     u.username='$uname'")or die(mysql_error());
if (mysql_num_rows($online) > 0){
echo "<font color=\"#04db04\">Online</font>";
} else {
echo "<font color=\"#FF0000\">Offline</font>";
}
   echo "</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
}
}
}

 

I have the ending table tags

Yeah, at the very bottom, change:

//...
}
   echo "</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
}
}
}

 

to:

}
   echo "</td>\n";
   echo "</tr>\n";
}
}
echo "</table>\n";
}

 

I think that is where you want it, just take it out of your for loop (I said while loop, but I meant for.).

Sorry, but your code is incredibly hard to read if simply for the fact that there is no consistant endentation.

 

One thing I did notice is this....

 

$num_rows = mysql_numrows($result);

 

outght be....

 

$num_rows = mysql_num_rows($result);

 

I also don't understand why you have so many queries within the one function, but then again, like I said, your code is pretty hard to read / follow.

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.