2012-09-30 38 views
-2

我正在嘗試使用管理器類來獲取查詢的結果並對信息進行格式設置,因此我無需在視圖中添加邏輯。我GOOGLE了一些解決方案,並認爲這將工作,但我的數組的鍵=>價值回到空白。有人可以看看它,看看我做錯了什麼?謝謝!格式化PHP記錄集中的信息並將其設置爲陣列

我已驗證$結果包含我嘗試格式化並放回到數組中的值。

while($row = mysqli_fetch_array($results)){ 
    $pets[$row['petID']] = $row['petID']; 
    $pets[$row['chipID']]=$row['chipID']; 
    $pets[$row['adoptionDate']]=Date($row['adoptionDate'],'m-d-Y'); 
    $pets[$row['pType']]=$row['pType']; 
    $pets[$row['breedName2']]=$row['breedName2']; 
    $pets[$row['breedName1']]=$row['breedName1']; 
    $pets[$row['imageName']]=$row['imageName']; 
    $pets[$row['intakeDate']]= Date($row['intakeDate'],'m-d-Y'); 
    $pets[$row['status']]=$row['status']; 
    $pets[$row['age']]=$row['age']; 
    $pets[$row['ageText']]= $this->getAge($row['age']); 
    $pets[$row['gender']]=$row['gender']; 
    $pets[$row['genderText']] = $this->getGender($row['gender']); 
    $pets[$row['breed2']]=$row['breed2']; 
    $pets[$row['breed']]=$row['breed']; 
    $pets[$row['petType']]=$row['petType']; 
    $pets[$row['petName']]=$row['petName']; 
    $pets[$row['customID']]=$row['customID']; 
} 

這裏是一個不返回任何內容的完整功能:

public function getPets($cond,$orgID){ 

     $pets = array(); 
     $pg = new petsGateway(); 
     $results = $pg->listByQuery($cond, $orgID); 

     while($row = mysqli_fetch_array($results)){ 
      $pets[$row['petID']] = $row['petID']; 
      $pets[$row['chipID']]=$row['chipID']; 
      $pets[$row['adoptionDate']]=Date($row['adoptionDate'],'m-d-Y'); 
      $pets[$row['pType']]=$row['pType']; 
      $pets[$row['breedName2']]=$row['breedName2']; 
      $pets[$row['breedName1']]=$row['breedName1']; 
      $pets[$row['imageName']]= $this->getImageURL($row['imageName']); 
      $pets[$row['intakeDate']]= Date($row['intakeDate'],'m-d-Y'); 
      $pets[$row['status']]=$row['status']; 
      $pets[$row['age']]=$row['age']; 
      $pets[$row['ageText']]= $this->getAge($row['age']); 
      $pets[$row['gender']]=$row['gender']; 
      $pets[$row['genderText']] = $this->getGender($row['gender']); 
      $pets[$row['breed2']]=$row['breed2']; 
      $pets[$row['breed']]=$row['breed']; 
      $pets[$row['petType']]=$row['petType']; 
      $pets[$row['petName']]=$row['petName']; 
      $pets[$row['customID']]=$row['customID']; 
     } 
     return $pets; 
    } 
+0

你曾經返回'$ pets'? –

+0

這個問題是不完整的 –

+0

我確實在函數的底部返回寵物,但它一直保持空白。 –

回答

0

HM ....以及即時消息不知道你想要什麼,但你現在擁有它,關鍵和值你的數組將是相同的,這使得它無用。

即對petName $pets['Fido'] = 'Fido'

也許你想擺脫你的任務左側的$row部分,讓你結束了這個

while($row = mysqli_fetch_array($results)) 
{ 
    $pet = Array(); 

    $pet['petID']  = $row['petID']; 
    $pet['chipID']  = $row['chipID']; 
    $pet['adoptionDate'] = Date($row['adoptionDate'],'m-d-Y'); 
    $pet['pType']  = $row['pType']; 
    $pet['breedName2'] = $row['breedName2']; 
    $pet['breedName1'] = $row['breedName1']; 
    $pet['imageName'] = $row['imageName']; 
    $pet['intakeDate'] = Date($row['intakeDate'],'m-d-Y'); 
    $pet['status']  = $row['status']; 
    $pet['age']   = $row['age']; 
    $pet['ageText']  = $this->getAge($row['age']); 
    $pet['gender']  = $row['gender']; 
    $pet['genderText'] = $this->getGender($row['gender']); 
    $pet['breed2']  = $row['breed2']; 
    $pet['breed']  = $row['breed']; 
    $pet['petType']  = $row['petType']; 
    $pet['petName']  = $row['petName']; 
    $pet['customID']  = $row['customID']; 

    $pets.push($pet); 
} 
+0

謝謝!我正在試圖將鍵設置爲''中的名稱以及記錄集中匹配的值。由於它們有很多行,所以也增加它。我的目標是這個(非常假): 第1行= petID = 1,petName ='蓬鬆',狀態='已採用'等 第2行= petID = 2,petName ='Barker',status ='Available' ,等 第3行= petID = 40,petName ='Meowintons',status ='Available'等 –

+0

通過刪除該行,它是否會一直覆蓋自己? –

+0

是的,它會的,看編輯 – Nacht

0

這將改寫$寵物每次它通過記錄循環時[KEY]。你想..

$pets[]['petID'] = $row['petID']; 
$pets[]['chipID'] = $row['chipID']; 

+0

不,我不想覆蓋,只是添加一個新的行。 :) –

+0

以上據我所見應該解決你的問題。在關閉'}'後加'print_r($ pets);'看結果 – Onimusha

+0

以空數組出現() –

0

我猜,但我明白,你想一些事情,如:

while($row = mysqli_fetch_array($results)){ 
    $pets[$row['petID']]['petID']  = $row['petID']; 
    $pets[$row['petID']]['chipID']  = $row['chipID']; 
    $pets[$row['petID']]['adoptionDate'] = Date($row['adoptionDate'],'m-d-Y'); 
    $pets[$row['petID']]['pType']  = $row['pType']; 
    $pets[$row['petID']]['breedName2'] = $row['breedName2']; 
    $pets[$row['petID']]['breedName1'] = $row['breedName1']; 
    $pets[$row['petID']]['imageName'] = $row['imageName']; 
    $pets[$row['petID']]['intakeDate'] = Date($row['intakeDate'],'m-d-Y'); 
    $pets[$row['petID']]['status']  = $row['status']; 
    $pets[$row['petID']]['age']   = $row['age']; 
    $pets[$row['petID']]['ageText']  = $this->getAge($row['age']); 
    $pets[$row['petID']]['gender']  = $row['gender']; 
    $pets[$row['petID']]['genderText'] = $this->getGender($row['gender']); 
    $pets[$row['petID']]['breed2']  = $row['breed2']; 
    $pets[$row['petID']]['breed']  = $row['breed']; 
    $pets[$row['petID']]['petType']  = $row['petType']; 
    $pets[$row['petID']]['petName']  = $row['petName']; 
    $pets[$row['petID']]['customID']  = $row['customID']; 
} 

這種方式,你有一個數組與所有的寵物ID爲第一把鑰匙及其細節在第二把鑰匙中。您也可以使用陣列推送。

1

這就是...重複的代碼...修改您的查詢以獲取您想要的行的難度不會更容易AND直接在數據庫中進行日期格式設置嗎?

例如

SELECT rowID, ... DATE_FORMAT(adoptionDate, '%m-%d-%Y'), ... 
FROM ... 

然後

while($row = mysql_fetch_assoc($result)) { 
    $pets[] = $row; 
}