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
    )

)
Constantino Tsarouhas
  • 6,846
  • 6
  • 43
  • 54
Rickos
  • 45
  • 4
  • Possible related question : http://stackoverflow.com/questions/1259407/php-return-only-duplicated-entries-from-an-array –  Nov 26 '13 at 00:10

1 Answers1

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
                )

        )
Lohardt
  • 1,057
  • 1
  • 12
  • 26
  • 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. – Rickos Nov 26 '13 at 01:30
  • added a change, maybe it will help – Lohardt Nov 26 '13 at 01:43
  • 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. – Rickos Nov 26 '13 at 03:10
  • 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. – Rickos Nov 26 '13 at 23:15
  • 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: https://tutsplus.com/lesson/arrays-part-1/ & https://tutsplus.com/lesson/arrays-part-2/ – Lohardt Nov 26 '13 at 23:30