Wordpress php quiz - code returns values of quiz regardless of user. How do i make it only returns current users values?

StackOverflow https://stackoverflow.com/questions/23659102

  •  22-07-2023
  •  | 
  •  

Domanda

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:

<?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 ORDER BY id DESC LIMIT 0,1", $form_id ) );
    foreach ( $data_ids as $data_id ) {
        $data = new IPT_FSQM_Form_Elements_Data( $data_id );
        echo wpautop( $data->data->pinfo[14]['value'] );
    }
?>

However when I execute it, it returns the most recent value regardless of user. How would I get it to only return the most recent value of the current user?

----------Update----------

After a lot of tinkering it is now working. The correct code is as follows

<?php 
    global $wpdb, $ipt_fsqm_info, $user_ID;
    get_currentuserinfo();
    $form_id = 9;
    $data_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id 
        FROM {$ipt_fsqm_info['data_table']}   
        WHERE form_id = %d   
        AND user_id = %d   
        ORDER BY id DESC LIMIT 0,1", $form_id, $user_ID ) );

    foreach ( $data_ids as $data_id ) {
    $data = new IPT_FSQM_Form_Elements_Data( $data_id );
    echo wpautop( $data->data->pinfo[14]['value'] );
}
?>

Massive thank you's to everyone that gave their time to helping me with this.

È stato utile?

Soluzione

Because of the way your table is setup you can have it read only the quiz that is associated with the user by adding AND user_id = %d", $form_id, $user_id ) ); You just need to make sure that $user_id is set to the id of the user currently logged in which will probably be in a session (or however it knows what user to select).

For example:

$userid = $_SESSION['userid'];

The new code

<?php 
    global $wpdb, $ipt_fsqm_info;

    $userid = $_SESSION['userid']; //Change

    $form_id = 9;
    $data_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$ipt_fsqm_info['data_table']} WHERE form_id = %d AND user_id = %d ORDER BY id DESC LIMIT 0,1", $form_id, $user_id ) );
    foreach ( $data_ids as $data_id ) {
        $data = new IPT_FSQM_Form_Elements_Data( $data_id );
        echo wpautop( $data->data->pinfo[14]['value'] );
    }
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top