1

I have a FOR loop getting all of the achievements from the database to put them on a page that shows all of the achievements. There are five different "types" of achievements that you can get, and each type has its own header. Right now I am only trying to get one type of achievement, the "common" type. Here is the code I have for getting the achievements:

$achievement_info_fetch = $db->prepare("SELECT `achievement_name`,`achievement_desc`,`image`,`type` FROM `achievement_names`");
$achievement_info_fetch->execute();
foreach($achievement_info_fetch->fetchAll(PDO::FETCH_NUM) as $achievement_info => $value){
    $achievement_information[$value[3]] = array('name'=>$value[0],'desc'=>$value[1],'image'=>$value[2]);
    unset($value);
}

That FOR loop right there is only putting one achievement into each array, like so:

array(5) { ["common"]=> array(3) { ["name"]=> string(11) "TrendSetter"
["desc"]=> string(25) "Invite 6 different people" ["image"]=>
string(23) "sm__0167_megaphone-.png" }...

There are three different achievements with the type of "common" and as you can see, like I said, it is only getting one and I have no idea why. It probably has something to do with the way I have it putting the information into the array.

The other problem after that would be outputting the information. I should be able to figure out how to do that if I can get this array situated.

4

1 回答 1

4

It does have to do with how you put it in the array:

$achievement_information[$value[3]]

This will overwrite $achievement_information['common'] every time it is reached in the loop. I wouldn't use numbers, so I'll write it with strings:

if (!isset($achievement_information[$value['type']]) {
    $achievement_information[$value['type']] = array();
}
$achievement_information[$value['type']][] = array(/* your code*/)
于 2013-05-29T23:43:14.120 回答