Jump to content

Displaying Results


MoFish

Recommended Posts

Hi,

 

I have some code which is pulling back the some database information. I would like the array to be structured slightly differently and wondered if anyone could help me in achieving this. I would like each game to be displayed with its attributes and players of each game to be inside. Something like the below:

array (
      [game]
        (
          attributes
            (
              [players]
            )
         )
      [game]
        (
          attributes
            (
              [players]
            )
      )
}

The structure I am currently getting is as follows. I am aware there are some duplicates of data (not sure why)

Array
(
    [0] => Array
        (
            [0] => 1
            [id] => 1
            [1] => 2014-02-11
            [game_date] => 2014-02-11
            [2] => 6:00
            [game_time] => 6:00
            [3] => DISC
            [game_location] => Japan
            [4] => As Many As Possible
            [type_title] => As Many As Possible
            [5] => Awaiting Players
            [status_title] => Awaiting Players
        )

    [players] => Array
        (
            [0] => Array
                (
                    [0] => 4
                    [id] => 4
                    [1] => 10029
                    [player_fb_id] => 10029
                    [2] => 2
                    [game_id] => 2
                )

            [1] => Array
                (
                    [0] => 3
                    [id] => 3
                    [1] => 1027
                    [player_fb_id] => 1027
                    [2] => 2
                    [game_id] => 2
                )

        )

    [1] => Array
        (
            [0] => 2
            [id] => 2
            [1] => 2014-02-18
            [game_date] => 2014-02-18
            [2] => 6:00
            [game_time] => 6:00
            [3] => DISC
            [game_location] => China
            [4] => 5v5
            [type_title] => 5v5
            [5] => Awaiting Players
            [status_title] => Awaiting Players
        )

)

The code I am using to create the array is as follows:

    public function get_games () {
    	$db = new sql();
    	$sql = "SELECT 
				games.`id` , games.`game_date` , games.`game_time` , games.`game_location` , games_types.`type_title`, games_status.`status_title`
				FROM games			
				JOIN games_types ON games.`game_type_id` = games_types.`id`		
				JOIN games_status ON games.`game_status_id` = games_status.`id`	
				ORDER BY games.id ASC";
    	$db->query($sql);
        if($db->num_rows()){
            while($k = $db->fetch_array($db)) {
                $result[] = $k;
                $pm = new playersManager();
				$players=$pm->get_players($k['id']);	
				$result['players'] = $players;
            }
         }
         echo "<pre>";
         print_r($result);
         echo "</pre>";
         return $result;
    }
Link to comment
https://forums.phpfreaks.com/topic/286533-displaying-results/
Share on other sites

I'm not quite sure I understand the question correctly (do you want to create an index called 'attributes' inside the game array and put the players in there, or just add the players array as one of the game attributes?). But here's an example, and from this you can figure it our easily.

$index = 0; // created a variable to keep track of the index number
while($k = $db->fetch_array($db)) {
   $result[$index]['game'] = $k; // using $index number so I can keep track, and added 'game'
   $pm = new playersManager();
   $players=$pm->get_players($k['id']);    
   $result[$index]['players'] = $players; // dumped players in same index as the game data
   $index++; // increment index number
}

Hope this helps

Link to comment
https://forums.phpfreaks.com/topic/286533-displaying-results/#findComment-1470815
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.