嵌套循环PHP MySQL结果

时间:2016-05-18 20:39:37

标签: php mysql

我从查询中得到结果,但我似乎没有得到嵌套循环的工作。这是可能有助于解释我想要做什么的图像。 enter image description here

基本上这就是我想要实现的目标。

enter image description here

以下是我遇到困难的地方

<?php
$query = "SELECT CandidateName, CandidateVotes, Party, MainRaceName, RaceName, win
FROM candidates
WHERE RaceID = $RaceID";

  $result = mysql_query($query) or die(mysql_error());
  for($i=0; $row = mysql_fetch_array($result); $i++){
     ?>
        <!-- FOR LOOP 1 SHOW MainRaceName  -->
      {
        <h1><?php echo $row['MainRaceName']; ?></h1>


                    <!-- FOR LOOP 2 SHOW RaceName  -->
              {
              <h1><?php echo $row['RaceName']; ?></h1>


                    {

                     <h1><?php echo $row['CandidateName']; ?></h1>
                     <h1><?php echo $row['CandidateVote']; ?></h1>
                     .
                     .

                    }

               }
     }      



  <?php
  }
  ?>

您可能有一个简单的解决方案,我感谢您的时间和帮助。 我正在努力将代码更改为PDO,我知道SQL注入相关的风险。

1 个答案:

答案 0 :(得分:1)

如果您只查询RaceName,那么应该这样做。

<?php
$query = "SELECT CandidateName, CandidateVotes, Party, MainRaceName, RaceName, win
FROM candidates
WHERE RaceID = $RaceID";

$result = mysql_query($query) or die(mysql_error());
for ($i = 0; $row = mysql_fetch_array($result), $i++) {
    // Print out main race name and race name on first iteration only
    if ($i === 0) {
?>
        <h1><?php echo $row['MainRaceName'] ?></h1>
        <h1><?php echo $row['RaceName'] ?></h1>
<?php
    }
?>
    <p><?php echo $row['CandidateName'] ?></p>
    <p><?php echo $row['CandidateVotes'] ?></p>
<?php
}
?> 

如果您正在查询MainRaceName,并且您需要在主赛事下打印所有比赛,我会将比赛名称与候选人一起分组,并获得针对特定比赛名称的总候选人投票。

<?php
$query = "SELECT CandidateName, CandidateVotes, Party, MainRaceName, RaceName, win
FROM candidates
WHERE RaceID = $RaceID";

$result = mysql_query($query) or die(mysql_error());

$grouped_result = array();
$total_votes = 0;
$main_race = '';

for ($i = 0; $row = mysql_fetch_array($result), $i++) {
    // If it is first iteration, set the current race name to race name in row
    // and save main race name
    if ($i === 0) {
        $main_race = $row['MainRaceName'];
        $cur_race = $row['RaceName'];
    }
    // If current race name is different than race name in row we reset
    // total count of votes, since we need to have sum of votes grouped by race name
    if ($cur_race != $row['RaceName']) {
        $total_votes = 0;
    }    
    // Populate grouped array
    $grouped_result[$row['RaceName']]['candidates'][] = array(
        'name'  => $row['CandidateName'],
        'votes' => $row['CandidateVote']
    );
    $grouped_result[$row['RaceName']]['total_votes'] = $total_votes + $row['CandidateVotes'];;
}

从你的照片中$grouped_result会给你一个像这样的嵌套数组。

array(
   'President' => array(
       'candidates' => array(
           0 => array(
               'name'  => 'Mr. X',
               'votes' => 429
           ),
           1 => array(
               'name'  => 'Ms. Y',
               'votes' => 43
           ),
                  .
                  .
                  .
       ),
       'total_votes' => 5247
   ),
   'US House of ...' => array(
       'candidates' => array(
           0 => array(
               'name'  => 'whatever_his_name_is',
               'votes' => 3693 
           )        
        )
       'total_votes' => 3693
   )
)

现在你可以打印出html。

<h1><?php echo $main_race ?></h1>
<?php 
    // loop through all races
    foreach ($grouped_result as $racename => $data): 
?>
        <h1><?php echo $racename ?></h1>
    <?php 
        // loop through all candidates in this race
        foreach ($data['candidates'] as $candidate): 
    ?>
            <p><?php echo $candidate['name'] ?></p>
            <p><?php echo $candidate['votes'] ?></p>
            <!-- Calculate vote percentages -->
            <p><?php echo ($candidate['votes'] / $data['total_votes']) * 100 ?> %</p>
    <?php 
        endforeach; 
    ?>
        <h1>Total votes: <?php echo $data['total_votes'] ?></h1>
<?php
    endforeach;
?>

现在你可以设置所有这些,放入表格,无论......