0

This is my mysql script

$result = mysql_query("SELECT users.*,games.game_names,games.id AS games_id
FROM users
LEFT JOIN games ON FIND_IN_SET(games.id,users.games)") or die(mysql_error());   
while($user = mysql_fetch_array($result, MYSQL_ASSOC)) {        
    $array = $user['games'];
    $name   =   $user['game_names'];
    $pieces = explode(",", $name);          
    $users[] = array(
                'users'=>array(
                    'id' => $user['id'],
                    'user' => $user['user'],
                    'games' =>  $pieces
                )
    );      

}   
$output = json_encode(array('statics' => $users));
echo $output;

I have 2 users. user1 have 4 games. User2 have 3 games.

I am getting

{
statics: [
    {
        users: {
            id: "1",
            user: "user1",
            games: [
                "football"
            ]
        }
    },
    {
        users: {
            id: "1",
            user: "user1",
            games: [
                "cricket"
            ]
        }
    },
    {
    users: {
        id: "1",
            user: "user1",
            games: [
                "athletics"
            ]
        }
    },
    {
        users: {
            id: "1",
            user: "user1",
            games: [
                "badminton"
            ]
        }
    },
    {
        users: {
            id: "2",
            user: "user2",
            games: [
                "athletics"
            ]
        }
    },
        {
        users: {
            id: "2",
            user: "user2",
            games: [
                "badminton"
            ]
        }
    },
    {
        users: {
            id: "2",
            user: "user2",
            games: [
                "basketball"
            ]
        }
    }
]
}

But i have to sort and need in following format

    {
statics: [
    {
        users: {
            id: "1",
            user: "user1",
            games: [
                "football",
                "cricket",
                "athletics",
                "badminton"
            ]
        }
    },        
    {
        users: {
            id: "2",
            user: "user2",
            games: [
                "athletics",
                "badminton",
                "basketball"
            ]
        }
    }

]
}

how should i prepare my query to get this result?

4
  • can you print the $users array in your question? Commented Sep 27, 2013 at 7:41
  • @plainjane ..u can see that array result in "I am getting" part Commented Sep 27, 2013 at 7:59
  • that is json i want an array so that i can test it.. Commented Sep 27, 2013 at 8:51
  • @plainjane I don't know how to create array...i am trying Commented Sep 30, 2013 at 5:36

1 Answer 1

0

try this code:

$result = mysql_query("SELECT users.*,games.game_names,games.id AS games_id
FROM users
LEFT JOIN games ON FIND_IN_SET(games.id,users.games)") or die(mysql_error());   
$result_array=array();
while($user = mysql_fetch_array($result, MYSQL_ASSOC)) {        
    $name   = $user['game_names'];
    $pieces = explode(",", $name);
    array_push($result_array, array(
        'users'=>array(
            'id' => $user['id'],
            'user' => $user['user'],
            'games' =>  $pieces
        )
    ));
}   
$output = json_encode(array('statics' => $result_array));
echo $output;

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.