Loop through database fields PHP MySQL -


i working on election system , having difficulty solving problem. following how application looks like. enter image description here

at moment these results adding query multiple times on php file. (for example run raceid = 9, raceid = 10 .........) sure there should way of reading raceid array or other way , results way. since these join tables looking way of retrieving racename (presidential names, justice supreme court ... etc) , mainracename(titles red, blue, gray) well. hope making sense far... following code

<!-- main election ticket  mainid loop should control --> <div class="panel panel-red margin-bottom-40"> <div class="panel-heading">     <h2 class="panel-title"> <strong>republican national</strong>  </h2> </div>  <!-- end sub election ticket    raceid loop should control section--> <h3 style="background-color:#f5f5f5; margin:30px; padding:5px; font-weight:bold ">presidential race  </h3>   <!-- results section election results --> <table class="table table-hover">     <thead>     <tr>         <th></th>         <th>candidate</th>         <th>votes</th>         <th>%</th>     </tr>     <!-- area gets sum of votes specific raceid -->     <?php     $result = mysql_query('select sum(candidatevotes) value_sum candidates 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                                         candidates                                         join race on race.raceid = candidates.raceid                                         join mainrace on mainrace.mainid = candidates.mainid                                         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 win field selected 1 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> 

i appreciate if can provide samples (loops) how can resolve problem. appreciated. sincerely,

you should able pull down need in 1 query.

in order sum votes candidate, need make sure group by candidate name.

try this:

select   sum(candidates.candidatevotes) value_sum,   candidates.candidatename,    candidates.party,   mainrace.mainracename,   race.racename,   candidates.win candidates join race    on race.raceid = candidates.raceid join mainrace   on mainrace.mainid = candidates.mainid candidates.raceid = :raceid group candidates.candidatename 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -