2017-02-09 210 views
0

我試圖改編我的一個類來處理存儲在JSON文件中的事件的標記。你可以創建標籤,刪除它們,恢復它們,查看它們等等。在下面這個庫的代碼中,你可以看到我在構造函數期間從文件中檢索數組,所以我使用它並在整個類的函數中操作它。參數類構造函數

class tagHandler { 
private $tagsFile = "/home/thomassm/public_html/functions/php/tags.json"; 
private $LstTags; 
private $LstReturn; 

function __construct() { 
    $this->LstTags = array(); 
    if(!file_exists ($this->tagsFile)){ 
     $fHND = fopen($this->tagsFile, "w"); 
     $tmpArray = array(array("EID","EName","EColor", "EDel")); 
     fwrite($fHND, json_encode($tmpArray)); 
     fclose($fHND); 
    } 

    $encodedInput = file ($this->tagsFile); 
    $this->LstTags = json_decode($encodedInput[0], true); 
    if(!$this->LstTags) $this->LstTags = array(); 
} 

function __destruct(){ 
    $this->update(); 
} 
public function update(){ 
    $this->LstTags = array_values($this->LstTags); 

    $fHND = fopen($this->tagsFile, "w"); 
    fwrite($fHND, json_encode($this->LstTags)); 
    fclose($fHND); 

    //empty memory region 
    $this->LstTags = array(); 
    $encodedInput = file ($this->tagsFile); 
    $this->LstTags = json_decode($encodedInput[0], true); 
} 
//More functions that use the collected array here. 

我正在嘗試修改班級以處理註冊我的活動的人。每個事件在我的數據庫中都有一個記錄,這個記錄將存儲一個註冊男性和註冊女性的陣列。我希望構造函數類能夠從記錄中獲取數組,以便像前一類那樣操作它們。問題是要獲得數組,我必須用事件ID(EID)搜索數據庫中的記錄,這將需要一個傳遞給構造函數的變量。更糟的是,這個參數必須能夠在循環中改變。例如,列出所有事件的頁面必須在遍歷每條記錄的循環中使用此類,因此它可以檢索數組來處理它,然後在表/ fullcalendar中顯示它,然後重複該過程以獲取下一個事件。我已經把我到目前爲止的代碼放在下面。它不完整(有些變量沒有被重新命名爲男性和女性等),可能是完全錯誤的,但它會給你一個基礎來解釋。

class signupHandler { 
private $LstMaleS; 
private $LstFemaleS; 
private $LstReturn; 

function __construct($IntEID) { 
    $this->LstTags = array(); 

    $StrQuery = "SELECT MaleS, FemaleS FROM tblEvents WHERE EID = ?"; 

    if ($statement = TF_Core::$MySQLi->DB->prepare($StrQuery)) { 
     $statement->bind_param('s',$IntEID); 
     $statement->execute(); 
     $results = $statement->get_result(); 
    } 

    $this->LstTags = json_decode($encodedInput[0], true); 
    if(!$this->LstTags) $this->LstTags = array(); 
} 

謝謝, 湯姆

+1

我會做的是在'signupHandler'的構造函數中傳遞數據庫結果數組。您可以在課程外查詢並初始化課程。這樣您也可以使用該類來保存尚未存入數據庫的新條目,然後使用該方法保存該條目。這就是MVC框架通常所做的事情,所以您可以從使用MVC框架中受益。 – apokryfos

+0

如果我這樣做,我會需要構造/毀滅類?或者我可以使用destruct來返回我想要更改的字符串。這聽起來像沒有課程的痛苦,如果我這樣做,我可能不會使用課程,因爲我認爲代碼不會重複太多。 –

+0

這個問題正在變成「到OOP或不OOP」。這真的取決於你。我只是說通常做什麼。 – apokryfos

回答

0
function decodeNames($StrNames){ 
    $this->LstNames = array(); 
    $this->LstNames = json_decode($StrNames, true); 
    if(!$this->LstNames) $this->LstNames = array(); 
    $this->LstNames = array_values($this->LstNames); 
} 

function update(){ 
    $this->LstNames = array_values($this->LstNames); 
    return json_encode($this->LstNames); 
} 

public function addSignUp($StrNames, $StrUsername, $StrStatus){ 
    $this->decodeNames($StrNames); 

    $BlnPresent = false; 

    for($i = 0; $i < count($this->LstNames); $i++){ 
     if($this->LstNames[$i][0] == $StrUsername){ 
      $this->LstNames[$i][1] = $StrStatus; 
      $BlnPresent = true; 
     } 
    } 
    if($BlnPresent == false){ 
     array_push($this->LstNames, array($StrUsername, $StrStatus, date("Y-m-d H:i:s"))); 
    } 
    return $this->update(); 
} 

我已決定每個I調用一個函數從它的時間編碼的JSON數組傳遞給類。在每個函數被解碼之前,它將被轉換爲一個數組,然後重新編碼並返回到調用它的文件。現在我不再有任何構造函數或破壞函數。