I am trying to count the number of images submitted to photo competitions. I am using an array (called $frequencies) to store my values generated from my query. I can create a table with both the competition name and count of entries, but cannot see how I can include the id associated with each of the competitions. In short, I'd like to have the id, competition name, and count - I can get all three of these but not so that the id can be used in a link to direct users to the specific competition).
I get this (everything works as it should, but no id):
...but want this (I cannot think how to get id into the table):
See my query and array below.
<?php
$con = mysqli_connect( DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE );
if ( mysqli_connect_errno() ) {
echo "Failed to connect to MySQL: " . mysqli_connect_error( $con );
}
$frequencies = array();
$cnt_result = "SELECT entered_images.id AS compID,
competitions.name AS compName,
COUNT(entered_images.id) AS Freq
FROM entered_images
INNER JOIN competitions
ON competitions.id = entered_images.id
GROUP BY competitions.name";
$cnt_Recordset1 = mysqli_query( $con, $cnt_result )or die( mysqli_error( $con ) );
$row_Recordset1 = mysqli_num_rows( $cnt_Recordset1 );
if ( $cnt_Recordset1 === false ) {
trigger_error( mysqli_error( $GLOBALS[ 'con' ] ) );
}
while ( $row = mysqli_fetch_row( $cnt_Recordset1 ) ) {
$frequencies[$row[1]] = $row[2];
}
?>
This works as required. Here is how I get the values into my table:
<table width="95%" border="1" align="center">
<tbody>
<tr>
<td align="center" bgcolor="#F8F2C3"><strong>Competition Name</strong></td>
<td align="center" bgcolor="#F8F2C3"><strong>Count</strong></td>
</tr>
<?php foreach ($frequencies as $name => $count) {?>
<tr>
<td><?php echo $name?></td>
<td align="center"><?php echo $count; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>