[eluser]stuffradio[/eluser]
This is as far as I got:
The two tables:
Code:
CREATE TABLE `online` (
`id` bigint(20) NOT NULL auto_increment,
`session_id` varchar(40) NOT NULL,
`username` varchar(50) NOT NULL,
`time` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Code:
CREATE TABLE `users` (
`id` bigint(20) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`time` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
file: onlinelib.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*******
* Author: Carl Wuensche
* Date: June 7 2008
* What it does: It is a class that enables you to easily check if you're online,
* and lists the online users.
*******/
class Onlinelib {
var $username; // The variable that stores your username
var $time; // The variable that stores the time you've been signed on
var $session_id; // The variable that stores the session_id
/**
* This is the function that loads everything. You don't really need to do anything in here,
* however it does check to see if you are online or not
**/
function Onlinelib()
{
$this->CI =& get_instance();
$this->CI->load->library(array('session'));
$this->username = "Guest";
$this->time = time();
$this->session_id = $this->CI->session->userdata('session_id');
if ($this->checkOnline())
{
$this->CI->db->where('session_id', $this->session_id);
$this->CI->db->update('online', $this->time);
} else {
$this->CI->db->set('username', $this->username);
$this->CI->db->set('time', $this->time);
$this->CI->db->set('session_id', $this->session_id);
$this->CI->db->insert('online');
}
}
/**
* Returns the number of people online
**/
function countOnline()
{
$count = $this->CI->db->get('online');
return $this->CI->db->affected_rows();
}
/**
* Checks to see if your session is in the online list.
**/
function checkOnline()
{
$this->CI->db->where('session_id', $this->session_id);
$this->CI->db->where('time', ($this->time - $this->time) > 300);
$query = $this->CI->db->get('online');
if ($query)
{
return TRUE;
} else {
return FALSE;
}
}
/**
* ToDo: Make this a function that lists the usernames in the online table.
**/
function listOnline()
{
}
}
To use it:
Code:
$this->load->library('onlinelib', '', TRUE);
Code:
$data['affected'] = $this->onlinelib->countOnline();
$this->load->view('hello', $data);
In your view file:
Code:
echo "Guests online:" . $affected;
That's as far as I got so far. What do you think? How can you improve it?
Also someone needs to fix this error:
Quote:A Database Error Occurred
Error Number: 1054
Unknown column '1212884761' in 'field list'
UPDATE `online` SET `1212884761` = '' WHERE `session_id` = '2022a8d666cecb1d2e3e41fafd185093'