Jump to content

[SOLVED] adding more then 1 WHERE clause in a query


runnerjp

Recommended Posts

ok currently i have this

<?php $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=mainforum')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
$onlineuser = $row['user'];
}

if($last_active < 300) {
echo $onlineuser;
}
?>

 

which should get all the users that are viewing the page http://www.runningprofiles.com/members/index.php?page=mainforum'

 

but i really want to get all the users on my profile which has pages suck as

 

http://www.runningprofiles.com/members/index.php?page=forum&forum=general

http://www.runningprofiles.com/members/index.php?page=forum&forum=races

 

but how can i also add these into the query

 

Use "OR"    i.e.

 

mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=mainforum') OR (file='
http://www.runningprofiles.com/members/index.php?page=forum&forum=general') OR ('http://www.runningprofiles.com/members/index.php?page=forum&forum=races'")

 

that should work

 

:)

Something along the lines of

 

SELECT  * FROM useronline WHERE file IN ('http://www.runningprofiles.com/members/index.php?page=mainforum','http://www.runningprofiles.com/members/index.php?page=general')"

 

You can add as many pages to the list as you want

humm ok i need to go back 1 step first... i used the simple part

 

<?php $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=$forum')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
$onlineuser = $row['user'];
}

if($last_active < 300) {
echo $onlineuser;
}
?>

 

but all it displays is 1 user... the 1st user i think on the page not them all

 

i have boith user1 and user2 on the page but its only sayin user 1 is on the page?? :S

http://dev.mysql.com/doc/refman/4.1/en/func-op-summary-ref.html :)

 

Edit to add the rest of the post pre-emptive post click!

 

I would do something like

 

//snip query
$activeTime = 300; //max inactivity time
$onlineUsers = array();

while($row = mysql_fectch_array($result)) {
    if((time() - $row['timestamp']) < $activeTime) {
      $onlineUsers[] = $row['user'];
    }
}

if(count($onlineUsers) > 0) {
    echo join($onlineUsers, " ");
}

humm ok i added the code

 

<?php $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=$forum')");
$activeTime = 300; //max inactivity time
$onlineUsers = array();

while($row = mysql_fectch_array($result)) {
    if((time() - $row['timestamp']) < $activeTime) {
      $onlineUsers[] = $row['user'];
    }
}

if(count($onlineUsers) > 0) {
    echo join($onlineUsers, " ");
}

?>

but for somereason it gets ridd of everythin under it and does not display any users

ok i tired this

 

<?php
include '../settings.php';
$result = mysql_query("SELECT * FROM useronline");

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

			$last_active = time() - $row['timestamp'];
			$onlineuser = $row['user'];				
			if ($last_active < 3600)
			{

    
if(count($onlineuser) > 0) {
    echo join($onlineuser, " ");
}
}
}

?>

 

b8t i get Warning: join() [function.join]: Invalid arguments passed in /home/runningp/public_html/members/test.php on line 15

 

<?php

include '../settings.php';

if ($result = mysql_query("SELECT * FROM useronline")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
      $last_active = time() - $row['timestamp'];				
      if ($last_active < 3600) {
        $onlineuser .= $row['user'] . " ";
      }
    }
  }
}

echo $onlineuser;

?>

ahh yes i see now...

 

ok but how would i make it look like the php version

 

runnerjp, roopurt18, thorpe, jamerson, discomatt, mikenl, SnakeFox, Monk3h, mikelmao, ober, Darkmatter5, Shaba, sarab99, sinista and 105 Guests are viewing this board.

 

but instead of guests its just

 

runnerjp,roopurt18 and thorpe

ok but how would i make it look like the php version

 

Um.. it is php.

 

To get the kind of output you want, one way would be to use an array.

 

<?php

include '../settings.php';

if ($result = mysql_query("SELECT * FROM useronline")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
      $last_active = time() - $row['timestamp'];				
      if ($last_active < 3600) {
        $onlineuser[] = $row['user'];
      }
    }
  }
}

for ($i=0;$i<count($onlineusers);$i++) {
  if ($i == count($onlineusers)-1) {
    echo " and " . $onlineusers[$i];
  } else {
    echo " " . $onlineusers[$i];
  }
}

?>

Oh man... do I really need to write the entire thing?

 

<?php

include '../settings.php';

if ($result = mysql_query("SELECT * FROM useronline")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
      $last_active = time() - $row['timestamp'];				
      if ($last_active < 3600) {
        $onlineuser[] = $row['user'];
      }
    }
  }
}

if (isset($onlineusers)) {
  if (count($onlineusers) == 1) {
    echo $onlineusers[0];
  } else {
    for ($i=0;$i<count($onlineusers);$i++) {
      if ($i == count($onlineusers)-1) {
        echo " and " . $onlineusers[$i];
      } else {
        echo " " . $onlineusers[$i];
      }
    }
  }
}

?>

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.