DISTINCT FROM field but COUNT total

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.

Read More

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'; } } 

Related posts

Leave a Reply

1 comment

  1. SQL:

    SELECT answer, count(answer) as total
      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
    

    PHP:

    ....
    $result .= $data->answer; 
    $result .= '</td>'; 
    $result .= '<td>';
    $result .= $data->total;
    $result .= '</td>';