-2

我正在尝试使用管理器类来获取查询结果并对信息进行格式化,这样我就不必对我的视图有逻辑了。我搜索了一些解决方案,并认为这会起作用,但我的 Key=>values 数组返回空白。有人可以看看它,看看我做错了什么吗?谢谢!

我已经验证 $results 包含我试图格式化并放回数组的值。

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;
    }
4

4 回答 4

1

那是相当...重复的代码...修改查询以获取所需的行并直接在数据库中进行日期格式化不是更容易吗?

例如

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

进而

while($row = mysql_fetch_assoc($result)) {
   $pets[] = $row;
}
于 2012-09-30T23:40:15.930 回答
0

嗯....好吧,我不确定您想要什么,但是正如您现在拥有的那样,您的数组的键和值将是相同的,这使得它毫无用处。

即宠物名称 $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);
}
于 2012-09-30T22:51:05.143 回答
0

这将在每次循环记录时重写 $pets[KEY]。你要..

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

等等

于 2012-09-30T22:51:55.580 回答
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 作为第一个键,其详细信息在第二个键中。您也可以使用数组推送。

于 2012-09-30T22:56:52.210 回答