在下面的function column\\u default中,我有几个案例执行几乎相同的查询。
是否可以对所有3种情况仅使用1个查询?如果是,我如何在下面的示例中使用它?谢谢你帮助我。
function column_default($item, $column_name){
global $wpdb;
/*
if ( ! in_array( $column_name, [\'id\', \'user_id\', \'user_email\', \'answer\', \'answer_time\', \'ip_address\' ] ) )
return \'\';
$sql = "SELECT $column_name as result
FROM {$wpdb->prefix}rdp_participants_answers
WHERE user_id = %d ORDER BY id DESC LIMIT 1 ";
$query = $wpdb->get_row( $wpdb->prepare( $sql, $item[ \'ID\' ] ) );
if ( is_object( $query ) )
return $query->result;
*/
switch($column_name){
// case \'id\': return $item[\'ID\'];
case \'user_id\': return $item[\'ID\'];
case \'user_email\': $user_info = get_userdata($item[\'ID\']);
return $user_info->user_email;
case \'display_name\': $user_info = get_userdata($item[\'ID\']);
return $user_info->display_name;
case \'city\': return get_user_meta($item[\'ID\'], \'city\', true);
case \'age\': return get_user_meta($item[\'ID\'], \'age\', true);
case \'user_status\': $user_status = get_user_meta($item[\'ID\'], \'status\', true);
if($user_status == 1) {
return \'
<img src="\'.RDP_URL.\'images/icon_status_green.gif" />
<a href="\'.get_admin_url().\'admin.php?page=rdp_admin_participants&off=\'.$item[\'ID\'].\'&status=update"><img src="\'.RDP_URL.\'images/icon_status_red_light.gif" /><a/>
\'; }
elseif($user_status == 0) {
return \'<a href="\'.get_admin_url().\'admin.php?page=rdp_admin_participants&on=\'.$item[\'ID\'].\'&status=update"><img src="\'.RDP_URL.\'images/icon_status_green_light.gif" /><a/>
<img src="\'.RDP_URL.\'images/icon_status_red.gif" />\';
}
case \'answer\':
$arrAnswer = $wpdb->get_row( "SELECT answer FROM {$wpdb->prefix}rdp_participants_answers WHERE user_id = ".$item[\'ID\']." ORDER BY id DESC LIMIT 1 " );
if(count($arrAnswer) == null)
return 0;
else
return $arrAnswer->answer;
case \'answer_time\':
$arranswerTime = $wpdb->get_row( "SELECT answer_time FROM {$wpdb->prefix}rdp_participants_answers WHERE user_id = ".$item[\'ID\']." ORDER BY id DESC LIMIT 1 " );
if(count($arranswerTime) == null)
return 0;
else
return $arranswerTime->answer_time;
case \'ip_address\':
$arrIPAddress = $wpdb->get_row( "SELECT ip_address FROM {$wpdb->prefix}rdp_participants_answers WHERE user_id = ".$item[\'ID\']." ORDER BY id DESC LIMIT 1 " );
if(count($arrIPAddress) == null)
return 0;
else
return $arrIPAddress->ip_address;
case \'user_registered\': return $item[\'user_registered\'];
case \'actions\': return $item[$column_name];
default: return \'<a href="\'.get_admin_url().\'admin.php?page=rdp_admin_participants&edit_rdp=\'.$item[\'ID\'].\'" class="page-title-action">Edit</a><a href="\'.get_admin_url().\'admin.php?page=rdp_admin_participants&view_rdp=\'.$item[\'ID\'].\'" class="page-title-action">View</a>\';
}
}
$query = "SELECT * FROM wp_users AS usr
JOIN wp_usermeta AS meta ON meta.user_id = usr.ID
LEFT JOIN wp_rdp_participants_answers as ans ON ans.user_id = usr.ID
WHERE ans.answer IS NOT NULL
GROUP BY usr.ID, usr.user_email ORDER BY ans.id ASC";
$data = $wpdb->get_results($query,ARRAY_A);
$this->items = $data;