2013-07-17 172 views
1

我写一个PHP脚本从MySQL数据库中提取一些数据,然后我想利用这些数据,并将其存储到一个二维数组。通常它的思考,如果我用这样的PHP动态创建多维数组

$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); 

代码的输出将是:

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

这是我多么希望我的输出是

所以这是我做的我的脚本

因此,基本上在我的数据库中,我有一个女子联赛积分榜,列是

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

所有连接已采取的成功护理,下面是我的查询

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

的重要一点不是之前我给下一个代码是,有2个其他排名表,men_senior和men_intermediate具有相同的结构,但显然只是数据的变化,下面是两个查询只是柜面

$get_mens_inter_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_inter` order by pos"; 

    $get_mens_senior_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_senior` order by pos"; 

现在我创建3个数组,我想seperately保持积分榜上的女士,男士高级,中级男子

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

我希望显示的阵列中的数据,像这样

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

现在,因为我想创造榜上所有3个类别的多维数组,我可以在3个独立运行的查询虽然循环虽然我认为,我可以在1完成相同的结果,我觉得它会帮助提高性能。如果最好使用3个while循环,请告诉我,我试过的是如下。

//I run the 3 queries and store them in the 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 to create 1 while loop so based of the results returned from the 3 
//queries so based on the results returned from the 3 queries, 
//I get the max number of times I want the query to 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 an index to start from 0 
$index = 0; 

//I will only show the example for 1 of the arrays but you get an idea 
that this issue will occur for all 
while ($index < $max_size) 
{ 
//first, everytime the loop is entered, we need the next row to be fetched 

    $standings_men_inter_table = mysqli_fetch_assoc($result_mens_inter); 
    $standings_ladies_table = mysqli_fetch_assoc($result_ladies); 

//there is a high chance that the other two tables return a different row size 
//so its best to check that we do not go beyond 
if($index < $mens_senior_boundary) 
    { 
      //we fetch the rows every time we enter the block 
      $standings_men_senior_table = mysqli_fetch_assoc($result_mens_senior); 

      //then, this is how I attempt at creating the 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 the loop runs 
$index++; 

} 

于是最后,我只是想打印什么,我认为是数组,但得到这个,贴图片,希望你能清楚地看到它 enter image description here

只是为了更进一步,每一次调查中, “如果”输入框,我只是评论说所有的东西,只是把该看什么正在返回

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

我得到的输出几乎90%是我需要

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) 

我需要的是例如:

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)..... so on); 

我的问题是

  • 什么是错我的代码,什么是动态创建的PHP 多维数组的正确方法?
    • 有什么我不明白如何mysql_fetch_assoc工作以及如何返回?
    • 任何事情要改善,我做错了什么?

我很欣赏你的时间,比你读它,我试图尽可能详细,我可以什么我都试过了。

谢谢。

+1

如果表格的字段相似 - 最好有一个表与这个领域,并添加像'游戏(团队)_type'字段值'['男','女'...]' –

+0

您好u_mulder,这是一个很好的建议,但主要原因拥有单独的表格是因为通过这种方式获得排名更容易。稍后可能会有更多的球队,所以更容易拥有3个独立的桌位,更容易排名,这只是我的看法,但当然,我正在研究如何以这种方式做到这一点。 –

回答

1

试试这个

后你这样做:

$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); 

只是这样做

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)){} 

基本上所有的公布代码应该可以被替换为:

<?php 
$ladies_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_ladies` order by pos"; 

$inter_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_inter` order by pos"; 

$senior_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_senior` order by pos"; 

$ladies_stmt = mysqli_query($link, $ladies_query) || die ("Couldn't get Ladies"); // reminds me of high school 
$inter_stmt = mysqli_query($link, $inter_query) || die ("Couldn't get Inter"); 
$senior_stmt = mysqli_query($link, $serior_query) || die ("Couldn't get 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; 
} 
+0

实际上,我发现问题与我的代码,它实际上工作,这是我的错误。我将print_r函数保留在初始值的内部,同时look($ index

+0

而($ standings_ladies [] = mysqli_fetch_assoc($ result_ladies)) { \t \t $ standings_ladies [] [ 'teamname' ] => $ standings_ladies_table [ 'TEAM_NAME'], \t \t $ standings_ladies [] [ '播放'] => $ standings_ladies_table [ '播放'], \t \t $ standings_ladies [] [ '赢得'] => $ standings_ladies_table [ 'won'], \t \t $ standings_ladies [] ['drawn'] => $ standings_ladies_table ['drawn'], \t \t $ standings_ladies [] ['lo ST '] => $ standings_ladies_table [' 丢失 '], \t \t $ standings_ladies [] [' 为 '] => $ standings_ladies_table [' 为 '], \t \t $ standings_ladies [] [' 对抗'] => $ standings_ladies_table [ '反对'], \t \t $ standings_ladies [] [ '点'] => $ standings_ladies_table [ '点'] }上述 –

+0

给出了这样的错误致命错误:无法使用[]在 读书我也尝试过array_push($ standings_ladies_inter [],array(...));它输出结果,但是在每次push时array_push()[function.array-push]:第一个参数应该是一个数组 总的来说,我做了 while($ standings_men_inter_t能够= mysqli_fetch_assoc($ result_mens_inter)) {array_push($ standings_men_inter,array(..));} 这是最好的。 谢谢你的时间。 –

1

不要试图把一切都放在一个循环,它显著降低了代码的清晰度,并给你一点在性能上没有收获,有这个一个术语,它被称为微优化,你永远不应该除非它真的有必要(大型网站的瓶颈)。

现在,我建议你是翻拍你的循环,并使其尽可能明确的数据是如何操作的。

你的情况最重要的事情是调试,print_r你的数组的内容在进程的每一步检查它包含什么,从数据库到代码结束,你会发现问题出在哪里。

+1

我只是仔细看了一下我的代码,我没有做任何错误,但把print_r语句放在while($ index

+0

请看看Orangepill的代码也对于有类似问题的其他人来说,它的结构很好,你如何去做。 –