OK, so I’ve created two classes, event:
class Event {
public $id;
public $u_id;
public $title;
public $type;
public $e_date;
public $e_time;
public $allday;
public $location;
public $dueby;
public $notes;
public $e_next = NULL;
function __construct($result = false, $row = false){
$this->id=mysql_result($result,$row,"id");
$this->u_id=mysql_result($result,$row,"u_id");
$this->title=mysql_result($result,$row,"title");
$this->type=mysql_result($result,$row,"type");
$this->e_date=mysql_result($result,$row,"date");
$this->e_time=mysql_result($result,$row,"time");
$this->allday=mysql_result($result,$row,"allday");
$this->location=mysql_result($result,$row,"location");
$this->dueby=mysql_result($result,$row,"dueby");
$this->notes=mysql_result($result,$row,"notes");
}
}
and EventList, which is my attempt at a linked list:
class EventList {
//public properties
public $e_count = 0;
public $first_event = NULL;
//public methods
private $e_array;
private const $hostname="#################";
private const $username="#######";
private const $password="#######";
private const $database="#######";
private const $table="events";
function __construct($u_id = NULL){
/*Connect To Database*/
mysql_connect($this->hostname,$this->username,$this->password);
@mysql_select_db($this->database) or die("Error");
private $eventquery="SELECT * FROM ".$this->table." WHERE u_id = '".$u_id."'";
private $eventresult=mysql_query($this->eventquery);
private $num=mysql_numrows($this->eventresult);
mysql_close();
private $i=0;
while ($this->i < $this->num) {
private $cur_event;
private $cur_date;
private $placed=false;
$this->cur_event = new Event($this->eventresult, $this->i);
private $j=0;
private $loop_event = $this->first_event;
private $p_loop_event;
while ($this->placed == false && $this->j < $this->e_count) {
private $loop_date = strtotime($this->loop_event->e_date);
$this->cur_date=strtotime($this->cur_event->e_date);
if ($this->cur_date > $this->loop_date) {
$this->p_loop_event = $this->loop_event;
$this->j++;
} else {
private $cur_next = $this->p_loop_event->e_next;
$this->e_array[$this->e_count] = $this->cur_event;
$this->e_array[$this->e_count]->e_next = $this->cur_next;
$this->p_loop_event->e_next = $e_count;
$this->placed = true;
}
}
if ($this->first_event == NULL) {
$this->e_array[$this->e_count] = $this->cur_event;
$this->first_event = $this->e_array[$this->e_count];
$this->placed = true;
} else if ($this->placed == false) {
$this->e_array[$this->e_count] = $this->cur_event;
$this->loop_event->e_next = $e_count;
$this->placed = true;
}
$this->i++;
}
}
}
I’m making quite a basic calendar app. On the homepage i do this:
populateEvents(<?=$u_id?>);
on document load.
Currently (without my linked list implementation, and directly interfacing with the event class), populate events, calls an ajax function which posts user id to this php file:
include ("class.event.php");
$hostname="############";
$username="#################";
$password="#################";
$database="#############";
$table="events";
$u_id = trim($_GET['u_id']);
/*Connect To Database*/
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die("Error");
/*If there are, throw an exception*/
try
{
$eventquery="SELECT * FROM ".$table." WHERE u_id = '".$u_id."'";
$eventresult=mysql_query($eventquery);
$num=mysql_numrows($eventresult);
mysql_close();
$i=0;
while ($i < $num) {
$userEvents[$i] = new Event($eventresult, $i);
$i++;
}
}
catch(Exception $e)
{
echo $e->getMessage();
mysql_close();
}
$odd = 1;
switch(count($eventlist)){
case 0:
echo '<p>You have no events. Click "New" to add a new event.</p>';
break;
default:
foreach($userEvents as $event){
$odd = $odd * -1;
if ($odd == -1) echo '<div id="eventWrap" class="oddItem">';
else echo '<div id="eventWrap">';
echo '<div id="eventItem">';
echo '<div id="eventIcon">';
switch ($event->type){
case 'event':
echo '<img src="images/event.png"/>';
break;
default:
break;
}
echo '</div>';
echo '<h3>'.$event->title.'</h3>';
echo '<p><span class="eventDate">'.$event->e_date.'</span></p>';
echo '</div>';
echo '</div>';
}
break;
}
Then the javascript puts the output into a div. Anyway, I want to maintain the same instance of the eventlist object in php, so all the events are searchable in date order, and i can output them as such. I have no idea how to start..
PHP objects and variables in memory are lost between requests. You could cache the EventList object using memcache or similar, but you are likely to run into concurrency issues with multiple requests.
PHP isn’t really designed for this kind of thing, and while I’m sure it is possible to use it in this way, I don’t think it will be straightforward.