2016-04-21 53 views
0

我正在进行选举系统工作,并且在解决问题时遇到困难。以下是我的应用程序的样子。 enter image description here通过数据库字段循环PHP MySQL

目前我通过在我的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> 

我真的很感激,如果你能提供一些样品(环路)我怎么能解决这个问题的代码。非常感谢您的帮助。此致

+0

[小博](http://bobby-tables.com/)说:[你的脚本是在对SQL注入攻击的风险。(HTTP://计算器.COM /问题/ 60174 /何灿I-防止-SQL注入式的PHP)。即使[转义字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –

+1

请[停止使用'mysql_ *'函数](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [这些扩展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[编写]​​(http://en.wikipedia.org/ wiki/Prepared_statement)语句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+0

感谢您的建议,我会将我的代码更改为PDO。 – CyberFla

回答

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 
+0

谢谢大卫。我会尝试这个查询并查看结果。这说得通。 – CyberFla