ОК, поэтому я создал два класса, событие:
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");
}
}
и EventList, который является моей попыткой связать список:
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++;
}
}
}
Я делаю довольно простое приложение-календарь. На главной странице я делаю это:
populateEvents(<?=$u_id?>);
при загрузке документа.
В настоящее время (без реализации моего связного списка и непосредственного взаимодействия с классом событий), заполнение событий, вызывает функцию ajax, которая отправляет идентификатор пользователя в этот файл php:
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;
}
Затем JavaScript помещает вывод в div. В любом случае, я хочу сохранить один и тот же экземпляр объекта списка событий в php, чтобы все события можно было искать в порядке дат, и я могу вывести их как таковые. Понятия не имею, с чего начать ..