duanpin5168 2014-05-14 15:33
浏览 20
已采纳

Wordpress php测验 - 无论用户如何,代码都会返回测验值。 我如何使它只返回当前用户的值?

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.

  • 写回答

1条回答 默认 最新

  • dth62818 2014-05-14 16:31
    关注

    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'] );
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路