dynamic - php dynamically create multi dimensional array -


i writing php script extract data mysql db, want take data , store 2 dimensional array. thinking of normally, if use code this

$test = array(); $test[0]=array(); $test[0]['hello']='hello'; $test[1]=array(); $test[1]['hello']='hello'; $test[2]=array(); $test[2]['hello']='hello'; print_r($test); 

the output be:

array ( [0] => array ( [hello] => hello ) [1] => array ( [hello] => hello ) [2] => array ( [hello] => hello ) ) 

which how want output be

so in script

so in database have table standings women's league , columns are

team_name, played, won, drawn, lost, for, against, points 

all connections have been taken care of successfully, below query

$get_ladies_query = "select  `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points`  `standings_ladies` order pos"; 

an important point not before show next code that, there 2 other standings tables, men_senior , men_intermediate same structure data changes, below 2 queries incase

$get_mens_inter_query = "select  `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points`  `standings_men_inter` order pos";      $get_mens_senior_query = "select  `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points`  `standings_men_senior` order pos"; 

now create 3 arrays want hold standings seperately ladies, mens senior, mens intermediate

$standings_ladies = array(); $standings_men_inter = array(); $standings_men_senior = array(); 

the data want displayed in array so

array(0=>array(team_name,wins,drawn,lost,for,against,points) 1=>array(team_name,wins,drawn,lost,for,against,points)) , on 

now since wanted create multidimensional arrays of standings 3 categories, have run queries in 3 separate while loops altough thought, accomplish same result in 1 , felt improve performance. if better use 3 while loops, please let me know, tried below.

//i run 3 queries , store them in given variables $result_mens_senior = mysqli_query($link,$get_mens_senior_query); $result_mens_inter = mysqli_query($link,$get_mens_inter_query); $result_ladies= mysqli_query($link, $get_ladies_query);  //i want create 1 while loop based of results returned 3  //queries based on results returned 3 queries,  //i max number of times want query run $ladies_boundary = mysqli_num_rows($result_ladies); $mens_senior_boundary = mysqli_num_rows($result_mens_senior); $mens_inter_boundary = mysqli_num_rows($result_mens_inter); $max_size = max(array($ladies_boundary,$mens_senior_boundary,$mens_inter_boundary));  //set index start 0 $index = 0;  //i show example 1 of arrays idea issue occur while ($index < $max_size) { //first, everytime loop entered, need next row fetched      $standings_men_inter_table = mysqli_fetch_assoc($result_mens_inter);     $standings_ladies_table = mysqli_fetch_assoc($result_ladies);  //there high chance other 2 tables return different row size //so best check not go beyond if($index < $mens_senior_boundary)     {             //we fetch rows every time enter block             $standings_men_senior_table = mysqli_fetch_assoc($result_mens_senior);              //then, how attempt @ creating 2 dimensional array         array_push($standings_men_senior, array(         $standings_men_senior_table['team_name'],         $standings_men_senior_table['played'],         $standings_men_senior_table['won'],         $standings_men_senior_table['drawn'],         $standings_men_senior_table['lost'],         $standings_men_senior_table['for'],         $standings_men_senior_table['against'],         $standings_men_senior_table['points']));     }  //incrementing index each time loop runs $index++;  } 

then finally, want print think array this, attached image, hope can see enter image description here

just investigate further, every time, 'if' block entered, commented out , put see being returned

if($index < $mens_senior_boundary) {     print_r(mysqli_fetch_assoc($result_mens_senior)); } 

the output got 90% need

array  ([team_name] => morley gaels [played] => 8 [won] => 6 [drawn] => 2  [lost] => 0 [for] => 110 [against] => 83 [points] => 14 )  array ( [team_name] => southern districts [played] => 8 [won] => 3 [drawn] => 2  [lost] => 3 [for] => 104 [against] => 98 [points] => 8 )  array ( [team_name] => st finbarrs [played] => 8 [won] => 3 [drawn] => 2  [lost] => 3 [for] => 107 [against] => 99 [points] => 8 )  array ( [team_name] => western shamrocks [played] => 8 [won] => 3 [drawn] => 0  [lost] => 5 [for] => 96 [against] => 88 [points] => 6 )  array ( [team_name] => greenwood [played] => 8 [won] => 1 [drawn] => 1  [lost] => 9 [for] => 82 [against] => 109 [points] => 3 ) 

what need example:

array(0=>array  ([team_name] => morley gaels [played] => 8 [won] => 6 [drawn] => 2  [lost] => 0 [for] => 110 [against] => 83 [points] => 14 ) 1=>array ([team_name] => southern districts [played] => 8 [won] => 3 [drawn] => 2  [lost] => 3 [for] => 104 [against] => 98 [points] => 8 )..... on); 

my questions are

  • what wrong code , correct way dynamically create multidimensional arrays in php ?
    • is there have not understood how mysql_fetch_assoc works , how returns ?
    • anything improve, doing wrong ?

i appreciate time, reading it, tried detailed can have tried.

thank you.

try this

after this:

$result_mens_senior = mysqli_query($link,$get_mens_senior_query); $result_mens_inter = mysqli_query($link,$get_mens_inter_query); $result_ladies= mysqli_query($link, $get_ladies_query); 

just this

while ($standings_men_senior[] = mysqli_fetch_assoc($result_mens_senior)){} while ($standings_men_inter[] = mysqli_fetch_assoc($result_mens_inter)){} while ($standings_ladies[] = mysqli_fetch_assoc($result_ladies)){} 

basically of posted code should able replaced with:

<?php $ladies_query = "select `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points`  `standings_ladies` order pos";  $inter_query = "select `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points`  `standings_men_inter` order pos";  $senior_query = "select `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points`  `standings_men_senior` order pos";  $ladies_stmt = mysqli_query($link, $ladies_query) || die ("couldn't ladies"); // reminds me of high school $inter_stmt  = mysqli_query($link, $inter_query)  || die ("couldn't inter"); $senior_stmt = mysqli_query($link, $serior_query) || die ("couldn't seniors");  $standings_men_senior = array(); $standings_men_inter = array(); $standings_ladies = array();  while ($row = mysqli_fetch_assoc($senior_stmt)){     $standings_men_senior[] = $row; } while ($row = mysqli_fetch_assoc($inter_stmt)){     $standings_men_inter[] = $row; } while ($row = mysqli_fetch_assoc($ladies_stmt)){     $standings_ladies[] = $row; } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -