I’m trying to create shortcodes for WordPress. I would like to display by DISTINCT
from ‘answer’ but COUNT
the total from ‘answer’.
If you find on foreach
in my code, I have no idea how to echo the count as I’m using function to return the value.
Currently I’m using count($data->answer);
.
TABLE : wp_events_answer
ââââââ¦âââââââââââââââ¦âââââââââââââ
â id â answer âquestion_id â
â âââââ¬âââââââââââââââ¬âââââââââââââ£
â 1 â Maybank â 12 â
â 2 â Maybank â 12 â
â 3 â Maybank â 12 â
â 4 â CIMB â 12 â
ââââââ©âââââââââââââââ©âââââââââââââ
My desired outcome is
ââââââ¦âââââââââââââââ¦âââââââââââââ
â No â Bank â Total â
â âââââ¬âââââââââââââââ¬âââââââââââââ£
â 1 â Maybank â 2 â
â 2 â CIMB â 1 â
ââââââ©âââââââââââââââ©âââââââââââââ
My current outcome
ââââââ¦âââââââââââââââ¦âââââââââââââ
â No â Bank â Total â
â âââââ¬âââââââââââââââ¬âââââââââââââ£
â 1 â Maybank â 1 â
â 2 â Maybank â 1 â
â 3 â Maybank â 1 â
â 4 â CIMB â 1 â
ââââââ©âââââââââââââââ©âââââââââââââ
Here is my code:
$sql= "
SELECT *, count(*)
FROM wp_events_answer
INNER JOIN wp_events_attendee ON wp_events_attendee.registration_id= wp_events_answer.registration_id
WHERE question_id=12 AND event_id='$eventid' GROUP by answer
";
$datas= $wpdb->get_results($sql);
$num_rows = $wpdb->num_rows;
if ($num_rows > 0)
{
$result .= '<table id="attendeeList">
<thead>
<tr>
<th width="10%" scope="col">No.</th>
<th width="70%" scope="col">Group Name</th>
<th width="20%" scope="col">Total</th>
</tr></thead>';
$number = 1;
foreach ($datas as $data) {
$result .= '<tbody>';
$result .= '<tr>';
$result .= '<td>';
$result .= $number++;
$result .= '</td>';
$result .= '<td>';
$result .= $data->answer;
$result .= '</td>';
$result .= '<td>';
$result .= count($data->answer); //i have no idea how to print total this
$result .= '</td>';
$result .= '</tr>';
}
$result .= '</table>';
return $result;
}
else
{ return 'There is no group'; } }
SQL:
PHP: