2011-02-28 9 views
0

OK、私は作成した二つのクラス、イベント:phpオブジェクトインスタンスをAjaxで再構成せずに操作する

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

文書の読み込み。

現在(私のリンクリストの実装せずに、直接イベントクラスとインターフェイス)、イベントを取り込み、このPHPファイルに投稿されたユーザID 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オブジェクトの同じインスタンスを維持したいので、すべてのイベントは日付順に検索可能で、私はそのように出力することができます。

答えて

1

メモリ内のPHPオブジェクトと変数は、リクエスト間で失われます。 Memcacheなどを使用してEventListオブジェクトをキャッシュすることはできますが、複数のリクエストで同時実行性の問題が発生する可能性があります。

PHPはこの種のもののために実際には設計されておらず、このように使用することは可能ですが、わかりません。

+0

お勧めしますか? –

0

私はsussedしました、私は$ _SESSIONスーパーグローバルで私のイベントリストのインスタンスのシリアル化されたバージョンを保存しています。

+0

各ユーザーが独自のEventListを持っている場合、このソリューションは正常に動作します。すべてのユーザーが同じEventListを共有する場合、同時実行性の問題が発生します。運が良かった! – Finbarr

0

は、私はあなたがオブジェクトの同じインスタンスを必要としないPHP

にeventlistオブジェクトの同じインスタンス を維持したいです。この方法では動作しません。すべてのリクエスト(開始ページ)は、eventListオブジェクトフォームデータベースを抽出します(または、サーバー上のファイルにシリアライズすることができます)。そのオブジェクトに変更があった場合、データベース(またはファイル)に保存されます。すべてのユーザーは同じオブジェクトで動作しますが、オブジェクトの同じインスタンスでは動作しません。私たちは異なるインスタンスを持っているテレビセットのようですが、我々は同じ時間内に同じムービーを見ることができます。

関連する問題