【问题标题】:Ajax manipulation of php object instances without reconstructing them?Ajax 操作 php 对象实例而不重建它们?
【发布时间】:2011-02-28 00:30:08
【问题描述】:

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

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?>);

在文件加载时。

目前(没有我的链表实现,并且直接与事件类交互),填充事件,调用一个将用户 ID 发布到这个 php 文件的 ajax 函数:

    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 中维护相同的 eventlist 对象实例,所以所有事件都可以按日期顺序搜索,我可以这样输出它们。我不知道如何开始..

【问题讨论】:

    标签: php javascript ajax oop


    【解决方案1】:

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

    PHP 并不是真正为这类事情而设计的,虽然我确信可以以这种方式使用它,但我认为它不会很简单。

    【讨论】:

      【解决方案2】:

      我已经解决了,我将我的事件列表实例的序列化版本存储在 $_SESSION 超全局变量中。

      【讨论】:

      • 如果每个用户都有自己的 EventList,那么这个解决方案可以正常工作。如果所有用户共享同一个 EventList,就会遇到并发问题。祝你好运!
      【解决方案3】:

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

      您不需要相同的对象实例。这种方式行不通。每个请求(打开页面)都从数据库中提取 eventList 对象(或者它可以在服务器上的文件中序列化)。如果该对象有任何更改,则将其保存到数据库(或文件)。每个用户都使用相同的对象,但不是使用相同的对象实例。就像电视机一样,我们有不同的实例,但我们可以在同一时间看到同一部电影。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-17
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 2021-11-02
        • 1970-01-01
        相关资源
        最近更新 更多