WordPress PHP quiz – code to recall answer recalls all past submissions as well

Sorry if the answer to this question is blindingly obvious, I’ve only just started learning Php so I’m not 100% sure on what I’m doing.

I’ve got a quiz that users fill out on wordpress using the plugin FSQM Pro and when they submit it they are redirected to a page that will display some text along with their answer to a question in the quiz. To recall the answer they submit, I have used this code:

Read More
<?php global $wpdb, $ipt_fsqm_info;
$form_id = 9;
$data_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$ipt_fsqm_info['data_table']} WHERE form_id = %d", $form_id ) );
foreach ( $data_ids as $data_id ) {
$data = new IPT_FSQM_Form_Elements_Data( $data_id );
echo wpautop( $data->data->pinfo[13]['value'] );
}
?>

However when I execute it, it returns all the values that the user has ever entered for that question with the most recent value at the bottom.

How do I get it to output only the most recent value?

Related posts

Leave a Reply

1 comment

  1. Well, you can modify your MySQL query, by using ORDER BY and LIMIT clauses, like this:

    $data_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$ipt_fsqm_info['data_table']} WHERE form_id = %d ORDER BY id DESC LIMIT 0,1", $form_id ) );