首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用if/else语句的wpdb foreach循环

问使用if/else语句的wpdb foreach循环
EN

Stack Overflow用户
提问于 2016-10-14 00:24:53
回答 2查看 830关注 0票数 0

我知道这已经被问了几百次了,但我跳过了一步,如果有人能帮我找出问题所在(或者帮我写得更好),我会很感激的。

我在用wordpress/woocommerce。当用户购买产品时,我将以下内容插入到表中: user_id、email、product_id、时间戳。

在每个产品页面上,我都试图编写一些代码,在表中循环以检查用户是否购买了相应的视频;如果是,它将显示视频,如果没有,它将显示一些文本。

我的逻辑有点错误,或者我做得太多了,但我正试图遵循例句在法典中的做法。现在,在下面的代码中,它将显示该页面的视频(因为我购买了它),但是如果没有,它将不会显示其他文本;我将有6-9个视频,所以如果我能够使这段代码更高效,我很想学习如何。谢谢。这是我的代码:

代码语言:javascript
复制
global $wpdb;
$user_id = get_current_user_id();

$results = $wpdb->get_results("SELECT * FROM awdwp_webinar_orders WHERE user_id=" . $user_id . "");

if ( $results ) {
    foreach( $results as $result ) {
        $loop_uid = $result->user_id;
        $loop_pid = $result->product_id;?>

        if ( is_product(991) && $loop_uid == $user_id && $loop_pid == 991 ) {
            echo '<h2>' . get_the_title($id) . '<h2>';
            $the_video = get_field('video_file'); 
            //display video code
        } //end if
    } //end foreach 
} else {
    echo "please purchase";
}
EN

回答 2

Stack Overflow用户

发布于 2016-10-17 21:36:26

就这样解决了:

代码语言:javascript
复制
global $product;
global $wpdb;
$id = $product->id;
$user_id = get_current_user_id();

$results = $wpdb->get_results("SELECT * FROM awdwp_webinar_orders WHERE product_id=" . $id . "");

if ( $results ) {
    foreach( $results as $result ) {
        $loop_uid = $result->user_id;
        $loop_pid = $result->product_id;

        if ( is_product(991) && $loop_uid == $user_id ) {
           //code
        } else {}
    }
}
票数 1
EN

Stack Overflow用户

发布于 2016-10-14 00:48:15

代码语言:javascript
复制
global $wpdb;
$user_id = get_current_user_id();

$results = $wpdb->get_results("SELECT * FROM awdwp_webinar_orders WHERE user_id=" . $user_id . "");

if ( $results ) {
    foreach( $results as $result ) {
        $loop_uid = $result->user_id;
        $loop_pid = $result->product_id;?>

        if ( is_product(991) && $loop_uid == $user_id && $loop_pid == 991 ) {
            echo '<h2>' . get_the_title($id) . '<h2>';
            $the_video = get_field('video_file'); 
            //display video code
        }else{
            echo "please purchase";
        }//end if
    } //end foreach 
} else {
    echo "please purchase";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40033089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档