Ajax操纵php对象实例而不重构它们?

时间:2011-02-28 00:30:09

标签: php javascript ajax oop

好的,所以我创建了两个类,事件:

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函数,将用户id发布到此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中维护事件列表对象的相同实例,因此所有事件都可以按日期顺序搜索,我可以这样输出它们。我不知道如何开始......

3 个答案:

答案 0 :(得分:1)

请求之间的内存中的PHP对象和变量丢失。您可以使用memcache或类似方法缓存EventList对象,但是您可能会遇到多个请求的并发问题。

PHP并不是真的设计用于这种事情,虽然我确信可以这样使用它,但我认为它不会直截了当。

答案 1 :(得分:0)

我已经怀疑了,我在$ _SESSION超全局中存储了我的事件列表实例的序列化版本。

答案 2 :(得分:0)

  

我想维护相同的实例   php中的eventlist对象

您不需要相同的对象实例。它不会这样工作。每个请求(开始页面)都提取eventList对象表单数据库(或者它可以在服务器上的文件中序列化)。如果对该对象有任何更改,则将其保存到数据库(或文件)。每个用户使用相同的对象,但不使用相同的对象实例。它就像电视机我们有不同的实例,但我们可以同时看到相同的moovie。