0

Im having trouble trying to modify this code so it will display duplicate data. Right now the array $playerPicks looks like 'array A', I would like it to pull the data from the database and display like 'array B'. The data is in the database. but not showing it like i would like. This is probably something simple for the more advanced, im still learning. Thank you.

Code

$sql = "select distinct weekNum from " . $db_prefix . "schedule order by weekNum;";
$query = mysql_query($sql);
$i = 0;
while ($result = mysql_fetch_array($query)) {
if ($i > 0) $weekNav .= ' | ';
if ($week !== (int)$result['weekNum']) {
    $weekNav .= '<a href="results.php?week=' . $result['weekNum'] . '">' . $result['weekNum'] . '</a>';
} else {
    $weekNav .= $result['weekNum'];
}
$i++;
}

$playerPicks = array();
$playerTotals = array();
$sql = "select p.userID, p.gameID, p.pickID ";
$sql .= "from " . $db_prefix . "picks p ";
$sql .= "inner join " . $db_prefix . "users u on p.userID = u.userID ";
$sql .= "inner join " . $db_prefix . "schedule s on p.gameID = s.gameID ";
$sql .= "where s.weekNum = " . $week . " and u.userName <> 'admin' ";
$sql .= "order by p.userID, s.gameTimeEastern, s.gameID";
$query = mysql_query($sql);
$i = 0;
while ($result = mysql_fetch_array($query)) {
$playerPicks[$result['userID']][$result['gameID']] = $result['pickID'];
if (!empty($games[$result['gameID']]['winnerID']) && $result['pickID'] == $games[$result['gameID']]['winnerID']) {
    //player has picked the winning team
    $playerTotals[$result['userID']] += 1;
} else {
    if ( $playerTotals[$result['userID']] += 0);
}
$i++;
}

echo '<pre>';
print_r($playerPicks);
echo '<pre>';

Output

Array A
(
[2] => Array
    (
        [433] => GB
    )

[924] => Array
    (
        [435] => PIT
    )

[934] => Array
    (
        [434] => OAK
    )

 )



Array B
(
[2] => Array
    (
        [433] => GB
        [433] => GB
    )

[924] => Array
    (
        [435] => PIT
        [435] => PIT
    )

[934] => Array
    (
        [434] => OAK
        [434] => OAK
    )

)
1

1 Answer 1

0
[2] => Array
    (
        [433] => GB
        [433] => GB
    )

This isn't possible. They need to have unique keys You need to find a better way to structure your data. Why do you need the duplicate info?

Here is one example of how you could keep your data

[2] => Array
        (
            [433] => Array
                            (
                                [type] => GB
                                [amount] => 2
                            )
            [435] => Array
                            (
                                [type] => PIT
                                [amount] => 5
                            )
        )

EDIT:

its not that big a change, if you change

$playerPicks[$result['userID']][$result['gameID']] = $result['pickID'];

to

$playerPicks[$result['userID']][$result['gameID']][] = $result['pickID'];

you would get

[123] => Array
        (
            [456] => Array
                (
                    [0] => GB
                    [1] => GB
                )

            [454] => Array
                (
                    [0] => PIT
                )

        )
Sign up to request clarification or add additional context in comments.

5 Comments

I need this info to populate into my form. but as you said they are unique keys ['gameID']. I thought there would be a easy way of doing this other then restructuring the data.
added a change, maybe it will help
although this looks good, but it modifies the $playerPicks to much and the rest of the code on other pages cant read it correctly. im still trying.
I accepted your answer because you basically answered what i asked for, although the outcome didn't work like i expected. im sure if i had a more basic understanding of the fundamental workings of arrays then this would be a breeze to figure out.
Thanks, i am sorry that i its giving you so much trouble. I did find some videos by Jeffrey Way, a very good teacher with lots of good videos on tutsplus.com. They may turn useful: tutsplus.com/lesson/arrays-part-1 & tutsplus.com/lesson/arrays-part-2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.