/    Sign up×
Community /Pin to ProfileBookmark

Multidimentional array not working

Hi all

I’m trying to store some names from an SQL result into a multi array, but when I try looping through the array, all I get is each letter from sequentially from the last name entered:

[code=php]$petsquery = “SELECT pet_name, pet_breed, pet_desc, pet_gender, user_id_fk FROM pets WHERE user_id_fk=’$session_user_id'”;
$petsqueryresult = mysql_query($petsquery);
while($row = @mysql_fetch_array($petsqueryresult, MYSQL_ASSOC)){
if($row){
$hasPets = true;
}
$numOfPets ++;

$pets[‘names’] = $row[‘pet_name’];
$pets[‘breeds’] = $row[‘pet_breed’];
$pets[‘desc’] = $row[‘pet_desc’];
$pets[‘gender’] = $row[‘pet_gender’];

}[/code]

then I try to print out each name:

[code=php]for($i = 0; $i < $numOfPets; $i++){ ?>

Pet Name: <?php echo $pets[‘names’][$i]; ?>
<input type=”checkbox” class=”theCheck” id=”<?php //echo $value ?>” name=”<?php //echo $value ?>” value=”<?php //echo $value ?>”>&nbsp;<?php //echo $value ?><br />
<div class=”petRegBox” id=”<?php echo $names.”Box”; ?>”>

</div>

<?php } ?> [/code]

the result looks like this:

Pet Name: S

Pet Name: t

Pet Name: i

so for each pet (3) it’s listing the first 3 lettery from the last pet in the array (Stinky)

I have no idea what’s going on – please help??

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@sstalderOct 08.2008 — Try this:
[code=php]
<?PHP
$pets = array();
$petsquery = "SELECT pet_name, pet_breed, pet_desc, pet_gender, user_id_fk FROM pets WHERE user_id_fk='$session_user_id'";
$petsqueryresult = mysql_query($petsquery);
if(mysql_num_rows($petsqueryresult) > 0) $hasPets = true;
$x = 0;

while($row = @mysql_fetch_array($petsqueryresult, MYSQL_ASSOC)){
$pets['names'][$x] = $row['pet_name'];
$pets['breeds'][$x] = $row['pet_breed'];
$pets['desc'][$x] = $row['pet_desc'];
$pets['gender'][$x] = $row['pet_gender'];
$x++;
}

// Print pets
for($i = 0; $i < sizeof($pets['names']); $i++){
echo "Pet Name: " . $pets['names'][$i] . "<br />";
}
?>
[/code]
Copy linkTweet thisAlerts:
@JericoauthorOct 08.2008 — that worked great!

Was I just overwriting each entry?

thanks sstalder!!
Copy linkTweet thisAlerts:
@sstalderOct 08.2008 — You were not setting the proper indexes for the array. Glad to help ?
×

Success!

Help @Jerico spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...