循环数据库字段PHP MySQL

时间:2016-04-21 19:39:16

标签: php mysql

我正在制定选举制度,我很难解决问题。以下是我的应用程序的样子。 enter image description here

目前,我通过在php文件中多次添加查询来获得这些结果。 (例如,在RaceID = 9,RaceID = 10 .........的情况下运行它) 我确信应该有一种方法可以将RaceID作为数组或其他方式读取并以此方式获得结果。由于这些是JOIN表,我也在寻找一种方法来检索RaceName(总统名称,司法最高法院......等)和MainRaceName(红色,蓝色,灰色标题)。我希望到目前为止我有所作为...... 以下是我的

代码
<!-- MAIN ELECTION TICKET  MainID loop should control this -->
<div class="panel panel-red margin-bottom-40">
<div class="panel-heading">
    <h2 class="panel-title"> <strong>REPUBLICAN NATIONAL</strong>  </h2>
</div>

<!-- End then SUB ELECTION TICKET    RaceID loop should control this section-->
<h3 style="background-color:#f5f5f5; margin:30px; padding:5px; font-weight:Bold ">Presidential Race  </h3>


<!-- Finally Results section ELECTION RESULTS -->
<table class="table table-hover">
    <thead>
    <tr>
        <th></th>
        <th>Candidate</th>
        <th>Votes</th>
        <th>%</th>
    </tr>
    <!-- This area gets the sum of the votes for a specific RaceID -->
    <?php
    $result = mysql_query('SELECT SUM(CandidateVotes) AS value_sum FROM candidates WHERE RaceID =9');
    $row = mysql_fetch_assoc($result);
    $sum = $row['value_sum'];
    ?>
    </thead>
    <tbody>
    <?php
    $query = "SELECT candidates.CandidateName, candidates.CandidateVotes, candidates.Party, mainrace.MainRaceName, race.RaceName, candidates.win
                                        FROM candidates
                                        JOIN race ON race.RaceID = candidates.RaceID
                                        JOIN mainrace ON mainrace.MainID = candidates.MainID
                                        WHERE candidates.RaceID = 9";

    $result = mysql_query($query) or die(mysql_error());
    for($i=0; $row = mysql_fetch_array($result); $i++){
        ?>

        <tr>
            <!--Show winner Icon if the win field is selected as 1 from database -->
            <td width="5px">
                <?php if ($row['win']==1)
                {

                    echo "<span class=\"glyphicon glyphicon-check\"></span>";
                }

                else
                {
                    echo "<span class=\"glyphicon glyphicon-unchecked\" style=\"color:#cccccc\"></span>";
                }
                ?>


            </td>

            <td><?php if ($row['win']==1){
                    echo "<strong>";
                    echo $row['CandidateName'];
                    echo "</strong>";
                }
                else {
                    echo $row['CandidateName'];
                }

                ?>
            </td>

            <td width="20%"><?php echo $row['CandidateVotes']; ?></td>
            <td width="30%"><?php
                if ($row['CandidateVotes']>0){
                    echo number_format((float)(($row['CandidateVotes']/$sum)*100), 2, '.', ''). ' %';
                }

                else{
                    echo "N/A";
                }
                ?>
            </td>

        </tr>
        <?php
    }
    ?>

    <tr>
        <td></td>
        <td><strong>Total Votes:</strong></td>
        <td><strong><?php echo $sum ?></strong></td>
        <td></td>
    </tr>

    </tbody>

</table>

我非常感谢您能提供一些示例(循环)如何解决此问题。非常感谢您的帮助。此致,

1 个答案:

答案 0 :(得分:1)

您应该能够在一个查询中提取所需的所有内容。

为了总结候选人的投票,您需要确保GROUP BY候选人姓名。

尝试这样的事情:

SELECT
  SUM(candidates.CandidateVotes) AS value_sum,
  candidates.CandidateName, 
  candidates.Party,
  mainrace.MainRaceName,
  race.RaceName,
  candidates.win
FROM candidates
JOIN race 
  ON race.RaceID = candidates.RaceID
JOIN mainrace
  ON mainrace.MainID = candidates.MainID
WHERE candidates.RaceID = :raceId
GROUP BY candidates.CandidateName