Jump to content
MakeWebGames

Users Online


The Ace

Recommended Posts

I am trying to get my users online into a table. I have got this:

 

<?php
include "globals.php";
print "<h3>Users Online</h3>
<table border='1' width='70%' class='table'>
<tr>
 <th width='15%'>Number</th>
 <th width='35%'>Username</th>
 <td width='20%'>Last Action</th>
</tr>
<tr>
 <td>$cn=0;</td>
 <td>$q=$db->query("SELECT * FROM users WHERE laston>unix_timestamp()-15*60 ORDER BY laston DESC");
while($r=$db->fetch_row($q))</td>
<td>{
$la=time()-$r['laston'];
$unit="secs";
if($la >= 60)
{
$la=(int) ($la/60);
$unit="mins";
}
if($la >= 60)
{
$la=(int) ($la/60);
$unit="hours";
if($la >= 24)
{
$la=(int) ($la/24);
$unit="days";
}
}
$cn++;
print "$cn. [url='viewuser.php?u={$r[']{$r['username']}[/url] ($la $unit)
";</td></tr></table>
}
$h->endpage();
?>

 

But, there is a problem with this line:

 

$q=$db->query("SELECT * FROM users WHERE laston>unix_timestamp()-15*60 ORDER BY laston DESC");

 

Any ideas!?

:-P

Link to comment
Share on other sites

Re: Users Online

try this

<?php
include "globals.php";
print "<h3>Users Online</h3>
<table width=30% cellspacing='1' class='table'>";
$cn=0;
$q=$db->query("SELECT * FROM users WHERE laston>unix_timestamp()-15*60 ORDER BY laston DESC");
while($r=$db->fetch_row($q))
{
$la=time()-$r['laston'];
$unit="secs";
if($la >= 60)
{
$la=(int) ($la/60);
$unit="mins";
}
if($la >= 60)
{
$la=(int) ($la/60);
$unit="hours";
if($la >= 24)
{
$la=(int) ($la/24);
$unit="days";
}
}
$cn++;
print "<tr><td>$cn. [url='viewuser.php?u={$r[']{$r['username']}[/url] ($la $unit)
</td></tr>";
}
print "</table>";
$h->endpage();
?>
Link to comment
Share on other sites

  • 2 weeks later...

Re: Users Online

I forgot, whenever there are multiple users online, it shows this:

usersonlineproban2.jpg

Here is my code: (same as above, from Tonka)

 

<?php
include "globals.php";
print "<h3>Users Online</h3>
<table width=30% cellspacing='1' class='table'>";
$cn=0;
$q=$db->query("SELECT * FROM users WHERE laston>unix_timestamp()-15*60 ORDER BY laston DESC");
while($r=$db->fetch_row($q))
{
$la=time()-$r['laston'];
$unit="secs";
if($la >= 60)
{
$la=(int) ($la/60);
$unit="mins";
}
if($la >= 60)
{
$la=(int) ($la/60);
$unit="hours";
if($la >= 24)
{
$la=(int) ($la/24);
$unit="days";
}
}
$cn++;
print "<tr align='center'><th>No.</th><th>User</th><th>Time Since Last Click</th></tr><tr align='center'><td>$cn.</td> <td>[url='viewuser.php?u={$r[']{$r['username']}[/url]</td> <td>($la $unit)</td></tr>";
}
print "</table>";
$h->endpage();
?>

 

I am so confuzzled!!! :?

Link to comment
Share on other sites

Re: Users Online

 

<?php
include "globals.php";
print "<h3>Users Online</h3>
<table width=30% cellspacing='1' class='table'><tr align='center'><th>No.</th><th>User</th><th>Time Since Last Click</th></tr>";
$cn=0;
$q=$db->query("SELECT * FROM users WHERE laston>unix_timestamp()-15*60 ORDER BY laston DESC");
while($r=$db->fetch_row($q))
{
$la=time()-$r['laston'];
$unit="secs";
if($la >= 60)
{
$la=(int) ($la/60);
$unit="mins";
}
if($la >= 60)
{
$la=(int) ($la/60);
$unit="hours";
if($la >= 24)
{
$la=(int) ($la/24);
$unit="days";
}
}
$cn++;
print "<tr align='center'><td>$cn.</td> <td>[url='viewuser.php?u={$r[']{$r['username']}[/url]</td> <td>($la $unit)</td></tr>";
}
print "</table>";
$h->endpage();
?>
Link to comment
Share on other sites

Re: Users Online

Now just to go into a bit more depth of what happened here(a common mistake), and to help others experiencing this problem...

What happened there is a simple programming error, something was in the while loop that wasn't meant to, thus the output was not what we intended for the script.

The column headings(th elements), were included within the loop, and was repeated each time the loop executed, but we only want the table cells to be repeated with different(dynamic) values each time.

Here is a quicker example of what happened here:

<?php

echo '<table>';
while(condition) {
    echo '<tr>
                 <th>Heading 1</th>
                 <th>Heading 2</th>
            </tr>
            <tr>
                 <td>Cell 1</td>
                 <td>Cell 2</td>
            </tr>';
} 
echo '</table>';

?>

 

To correct this, you need to remove the table headings(th elements) from the loop.

This will stop from the table headings being repeated again.

 

<?php

echo '<table>
            <tr>
                 <th>Heading 1</th>
                 <th>Heading 2</th>
            </tr>';
while(condition) {
    echo '<tr>
                 <td>Cell 1</td>
                 <td>Cell 2</td>
            </tr>';
} 
echo '</table>';

?>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...