Categories
post

Getting Counts For Groupings From Mysql

My dad “beto” asked me about a topic he would like to use on his website.  I decided to share it publicly.

He has a simple select box that stores a “Yes/No/Maybe” option in a table.  He wants to include the results on a webpage.

I have to assume his Mysql table (let’s call it ReunionAttendance) has at least two columns (SelectBoxValue, GuestsAttending).  Adjust the field names for the actual table columns in your table.

There are many ways to do this.  I am going to do it this way for a demonstration:

<?php
/*  Steps needed
1) Get data from table
2) Put data into a variable
3) Display the table via HTML
*/

/* 1 */
$result = mysql_query(“SELECT SelectBoxValue, SUM(GuestsAttending) as Attending FROM ReunionAttendance GROUP BY SelectBoxValue”);

/* 2 */
while (list($comingValue, $comingCount) = mysql_fetch_row($result)) {
$list[$comingValue] = $comingCount;
}

/* 3 */
?>
<div id=”WhosComing”>
<table>
<thead>
<tr>
<th>Coming?</th>
<th>Total</th>
</tr>
</thead>
<tbody>

<?php
foreach ($list as $comingValue => $comingCount) {
?>
<tr>
<td><?php echo $comingValue?></td>
<td align=”right”><?php echo $comingCount?></td>
</tr>

<?php
}

?>

</tbody>
</table>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *