Jump to content

array_push issue from within foreach


dodgeitorelse3
 Share

Go to solution Solved by dodgeitorelse3,

Recommended Posts

I have a foreach statement that contains 20 values. with each value I run a query on tables to return results. I can get it to create an array but it only shows last foreach values returned results. How do I get all 20 results to be in $all_data array.

my code so far is
 

$all_data = array();			
					
		foreach($id as $idkey => $iduse){
			global $all_data, $all_player_data, $temparr, $data;
			$all_player_data = array();
			$temparr = array();
			
				$result_time = $conn->query("SELECT * FROM ".$iduse." order by timedate Asc");
				$temptdarray = array();
				
				while ($rowtime = $result_time->fetch_row()) {
					$timedate   = $rowtime[0]; // row 0
					$numplayers = $rowtime[1]; // row 1 
					$maxplayers = $rowtime[2]; // row 2
					$status     = $rowtime[3]; // row 3
					$players    = $rowtime[4]; // row 4
					$mapname    = $rowtime[5]; // row 5 
					$maptype    = $rowtime[6]; // row 6 
				
					$data = array("timedate" => $timedate, "numplayers" => $numplayers, "maxplayers" => $maxplayers, "status" => $status, "players" => $players, "mapname" => $mapname, "maptype" => $maptype);
					
					array_push($temparr, $data);
				}
			$all_player_data[] = array("id" => $idonly[$idkey], "comment" => $comment[$idkey], "data" => $temparr);
			array_push($all_data[], array($all_player_data));
		}

if I print_r($all_data) I get

all_data
Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => 
    [10] => 
    [11] => 
    [12] => 
    [13] => 
    [14] => 
    [15] => 
    [16] => 
    [17] => 
    [18] => 
    [19] => 
)

if I print_r($all_player_data) I get
 

all_player_data
Array
(
    [0] => Array
        (
            [id] => 80
            [comment] => FooFire Attack Team
            [data] => Array
                (
                    [0] => Array
                        (
                            [timedate] => 2021-03-17 16:34
                            [numplayers] => 0
                            [maxplayers] => 10
                            [status] => 1
                            [players] => 
                            [mapname] => SF Village Coop
                            [maptype] => COOP
                        )

                    [1] => Array
                        (
                            [timedate] => 2021-03-20 07:12
                            [numplayers] => 1
                            [maxplayers] => 10
                            [status] => 1
                            [players] => --=P-51=--
                            [mapname] => SF Village Coop
                            [maptype] => COOP
                        )

                    [2] => Array
                        (
                            [timedate] => 2021-03-20 07:26
                            [numplayers] => 2
                            [maxplayers] => 10
                            [status] => 1
                            [players] => --=P-51=--, [FFAT]herbstfuerst
                            [mapname] => SF Village Coop
                            [maptype] => COOP
                        )

                    [3] => Array
                        (
                            [timedate] => 2021-03-20 07:28
                            [numplayers] => 3
                            [maxplayers] => 10
                            [status] => 1
                            [players] => --=P-51=--, [FFAT]herbstfuerst, YAKIR
                            [mapname] => SF Village Coop
                            [maptype] => COOP
                        )


		.................blah blah blah

                [7223] => Array
                        (
                            [timedate] => 2021-12-05 06:26
                            [numplayers] => 0
                            [maxplayers] => 10
                            [status] => 1
                            [players] => 
                            [mapname] => SF Village Coop
                            [maptype] => COOP
                        )

                    [7224] => Array
                        (
                            [timedate] => 2021-12-05 07:26
                            [numplayers] => 0
                            [maxplayers] => 10
                            [status] => 1
                            [players] => 
                            [mapname] => SF Village Coop
                            [maptype] => COOP
                        )

                    [7225] => Array
                        (
                            [timedate] => 2021-12-05 08:26
                            [numplayers] => 0
                            [maxplayers] => 10
                            [status] => 1
                            [players] => 
                            [mapname] => SF Village Coop
                            [maptype] => COOP
                        )

                )

        )


How do I get all 20 result sets into $all_data?

Edited by dodgeitorelse3
Link to comment
Share on other sites

Wow! So many "Deadly Sins" in a single block of code.

  • Thou shouldst not use global
  • Thou shouldst not run queries inside loops
  • Thou shouldst not split data of the same entity type across multiple tables
  • Thou shouldst not use "SELECT * "

Why don't you use fetch _assoc instead of fetch row, then the loop becomes simply

while ($rowtime = $result_time->fetch_assoc()) {
    $temparray[] = $rowtime;
}

 

  • Haha 1
Link to comment
Share on other sites

Ok I am aware of the sins. The database I am using was created 18 years ago. The gsreaders database has a table labeled lgsl. This table houses all the details for each game server we track. There is an ID column for each server. Gsreaders database also has several other tables. 3 for each server. This is where the design issue comes into play. These tables have no columns in them to be able to use joins. The only way I know which 3 tables go to each server is by table name. So if for example a game server has ID 80 then the 3 corresponding tables use that I'd in the table name such as scoreboard_80_time. I am working on a page to allow users to look up data for a single server or all servers. This is why I get a list of all active servers and put the id's in an array. Then, for lack of knowledge I use another sin by running queries in the foreach. The globals were only there to see if it was an issue if not being able to pass data. Global will not be used. I used fetch row because each query result array must have the I'd of the server in final array so I know what data goes to which server as in the all_player_data array. Using fetch row as you suggested does not give me this.

So my original question is still being asked. There are currently 20 active servers so there will be 20 all_player_data arrays. As each is made how do I push each to all_data array that is defined outside the foreach?

Edited by dodgeitorelse3
Link to comment
Share on other sites

  • Solution

I ended up with this however I suppose a php memory issue will arise when enough data is retrieved.

 

	$all_data = array();				
		foreach($id as $idkey => $iduse){
			$temparr=array();
				$result_time = $conn->query("SELECT * FROM ".$iduse." order by timedate Asc");
					while ($rowtime = $result_time->fetch_row()) {
						$temparr[] = $rowtime;
					}
						array_push($all_data, array("id" => $idonly[$idkey], "comment" => $comment[$idkey], "data" => $temparr));
		}				
			

Also thank you Barand for the fetch_row replacement.

Edited by dodgeitorelse3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

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