Jump to content

Searching Nested Arrays


Lucky2710

Recommended Posts

I have an assoc array thats pulled from mysql.

 

Heres the array...

Array ( 
[0] => Array ( [iD] => 288 [user_ID] => 61 [Game_ID] => 12 [Week] => 1 [Pick] => 1 ) 
[1] => Array ( [iD] => 287 [user_ID] => 61 [Game_ID] => 11 [Week] => 1 [Pick] => 2 ) 
[2] => Array ( [iD] => 286 [user_ID] => 61 [Game_ID] => 10 [Week] => 1 [Pick] => 2 ) 
[3] => Array ( [iD] => 285 [user_ID] => 61 [Game_ID] => 9 [Week] => 1 [Pick] => 2 ) 
[4] => Array ( [iD] => 284 [user_ID] => 61 [Game_ID] => 8 [Week] => 1 [Pick] => 1 ) 
[5] => Array ( [iD] => 283 [user_ID] => 61 [Game_ID] => 7 [Week] => 1 [Pick] => 1 ) 
[6] => Array ( [iD] => 282 [user_ID] => 61 [Game_ID] => 6 [Week] => 1 [Pick] => 2 ) 
[7] => Array ( [iD] => 281 [user_ID] => 61 [Game_ID] => 5 [Week] => 1 [Pick] => 1 ) 
[8] => Array ( [iD] => 280 [user_ID] => 61 [Game_ID] => 4 [Week] => 1 [Pick] => 2 ) 
[9] => Array ( [iD] => 279 [user_ID] => 61 [Game_ID] => 3 [Week] => 1 [Pick] => 2 ) 
[10] => Array ( [iD] => 278 [user_ID] => 61 [Game_ID] => 2 [Week] => 1 [Pick] => 1 ) 
[11] => Array ( [iD] => 277 [user_ID] => 61 [Game_ID] => 1 [Week] => 1 [Pick] => 2 ) 
)

 

I need to search the arrays inside the main array, to look for where "Pick = 1" if so then echo Away, and some info out of that array. Or if "Pick = 2" then echo Home and some info from that array. And i need it to do foreach array inside that array.

 

I need to search this array. Heres my code so far...

$user = 61;
$query = "
SELECT *
FROM CollegeFootballPicks
WHERE CollegeFootballPicks.User_ID = $user";
$result = mysql_query($query);
$affected = mysql_num_rows($result);

echo $affected;
echo '<br />';

for($i=0; $i<$affected; $i++){
$picks[] = mysql_fetch_assoc($result);
}

foreach($picks as $pic)
        if($pic['Pick'] == 1)
                echo $pic['User_ID'];echo '<br />';
                echo $pic['Game_ID'];echo '<br />';
                echo $pic['Week'];echo '<br />';
                echo 'Away';echo '<br />';echo '<br />';


foreach($picks as $pic)
        if($pic['Pick'] == 2)
                echo $pic['User_ID'];echo '<br />';
                echo $pic['Game_ID'];echo '<br />';
                echo $pic['Week'];echo '<br />';
                echo 'Home';echo '<br />';echo '<br />';

 

 

But this isn't working. But i have no errors. Does anyone know how i can search the nested array.

Link to comment
Share on other sites

I've been following some of your threads while you attempt to create this application.

 

Storing just an away/home indicator as the pick value makes it extra complicated to associate that back to the actual team. Is there some reason you did not store the team id that they picked as the pick value? You have added an extra level of abstraction by just using an away/home value.

Link to comment
Share on other sites

Because it was quicker to create to begin with, and made the code way shorter, because instead of on the picks page having to specify each team for each game, all i had to do was say 1 = away and 2 = home. At the time it seemed like the best way to go. And now trying to make it user friendly to show them what they picked. Now its gonna be just as complicated, and long. But i'm not re doing all the stuff i've done so far. So im just gonna keep going with how it is.

 

 

 

This thread, i had a code error. Problem solved! my if statements i  forgot the { & }.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.