How to organize two mysql tables into one large multidimensional array?

I find it hard to understand this ...

I have two tables ... ticket_winners and tickets

in the ticket_winners table , user / profile information ...

Ticket table - this is all the tickets that these users have for one user ID, in each table there can be 10+ tickets for each user.

Question: How can I scroll the second iteration of data when the bits of the table have more than 1 row for each user

function pullTickets() {
    $sql = $this->mysql->retrieve("SELECT * FROM ticket_winners ORDER BY id DESC LIMIT 5");
    $sql2 = $this->mysql->retrieve("SELECT id, userId, ticketId FROM tickets ORDER BY id ASC LIMIT 5");
    while($row = mysql_fetch_array($sql)) {
        $results[$row['id']]['user'] = $row['userId'];

        while($row2 = mysql_fetch_array($sql2)) {
            if($results[$row['id']]['user'] == $row2['userId']) {
                $results[$row['id']]['tickets'][$row2['id']] = $row2['ticketId'];
            } else {
            }
        }

    }
    return $results;
}

PHP Page Example : Works Great

$data = $obj->pullTickets();
foreach($data as $user) {
   echo $user['username'];
   foreach($data['ticket'] as $ticket) {
       echo $ticket['ticketId'];
   }
}

What the array looks like:

[1] => Array
    (
        [batch] => 1
        [userId] => 200
        [userName] => Craig
        [tickets] => Array
            (
                [1] => GH7JNm72hN
                [2] => JudM3rT3is
                [3] => KiLPoXCmDF
            )

    )
[2] => Array
    (
        [batch] => 1
        [userId] => 100
        [userName] => Hewbie
        needs to continue looping
    )

the ticket table does not loop through each user, as in the array [1]. It skips all tickets of other users.

+5
2

ticket_winners, SELECT *, , .

SELECT tw.id,tw.userid,t,userName,t.id AS tick_id, t.ticketId 
    FROM `ticket_winners` AS tw
LEFT JOIN `tickets` AS t
    ON tw.userid = t.userid 
ORDER BY tw.id DESC LIMIT 5

t.id( tick_id), ticketId userName.

, , , , , .

$results = array();

while ($sql = mysql_fetch_array($query, MYSQL_ASSOC) {
    $results[$sql['userid']]['userName'] = $sql['user'];
    $results[$sql['userid']]['tickets'][] = $sql['tick_id'];
}

:

[1] => array 
    (
    ['userName'] => 'Craig',
    ['tickets'] => array 
        (
        [0] => 'GH7JNm72hN',
        [1] => 'JudM3rT3is'
        )
    )
[2] => array 
    (
    ['userName'] => 'Adam',
    ['tickets'] => array 
        (
        [0] => 'GfdT24sDgC'
        )
    )

: a) b) while while, .

, :

  • .
  • , - .
+3

, , $result - , ticket_winner. ? , .

0

All Articles