2015-07-03 48 views
0

我有两个表格 第一个包含成员级别和相关课程的列表(例如:course1分配给level1 ,course2分配给level1,course3分配给level2) 第二个包含所有课程的列表。我只想列出与当前用户级别相关的课程。 所以我写了:Wordpress - Mysql:需要从表格中提取数值从第二个表格的字段“course_id”中过滤

全球

$wpcwdb, $wpdb; 
    if (!current_user_can('administrator')) { 
     function get_user_roles_by_user_id($user_id) { 
     $user = get_userdata($user_id); 
     return empty($user) ? array() : $user->roles; 
     } 
     $user_id = get_current_user_id(); 
     $user_ad_roles= get_user_roles_by_user_id($user_id); 
     $user_ad_roles= implode("','",$user_ad_roles); 
     echo $user_ad_roles; 

// and this show the role (level) for the current user 
// then interrogate the table 1 

$SQL = " 
       SELECT course_id 
       FROM $wpcwdb->map_member_levels 
       WHERE member_level_id = '$user_ad_roles' "; 
       $corsi_del_livello = $wpdb->get_results($SQL); 

//now I need to filter the second table on the results of this first query 

$SQL = " 
       SELECT * 
       FROM $wpcwdb->courses 
       WHERE course_id IN(".implode(',',$corsi_del_livello).") 
       ORDER BY $orderBy $ordering"; 

     $courses = $wpdb->get_results($SQL); 
    } 

但它不工作。

在Wordpress中出现以下错误"Catchable fatal error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\profad.it\wp-content\plugins\wp-courseware\pages\page_course_das‌​hboard.inc.php on line 79"。第79行在第二个查询中是“WHERE”子句。

有人可以帮助我吗?

+0

你有一个错误日志或可以更详细地描述当前的行为? –

+0

在wordpress中,我收到以下错误:“可捕获的致命错误:class stdClass的对象无法转换为C:\ xampp \ htdocs \ profad.it \ wp-content \ plugins \ wp-courseware \ pages \ page_course_dashboard.inc中的字符串第79行“.php” 第79行在第二个查询中是“WHERE”子句。 –

回答

0

解决! 我做了两个表之间的加入,这就是代码:

global $wpcwdb, $wpdb; 
    if (!current_user_can('administrator')) { 
     function get_user_roles_by_user_id($user_id) { 
     $user = get_userdata($user_id); 
     return empty($user) ? array() : $user->roles; 
     } 
     $user_id = get_current_user_id(); 
     $user_ad_roles= get_user_roles_by_user_id($user_id); 
     $user_ad_roles= implode("','",$user_ad_roles); 
     echo $user_ad_roles; 
    $livelli= $wpcwdb->map_member_levels; 
     $corsi= $wpcwdb->courses; 
     $SQL = " 
       SELECT $livelli.course_id, $corsi.* 
       FROM $corsi INNER JOIN $livelli ON $corsi.course_id = $livelli.course_id 
       WHERE $livelli.member_level_id = '$user_ad_roles' "; 

     $courses = $wpdb->get_results($SQL); 
    } 
相关问题