从wp_users表中检索数据而不显示空值

时间:2018-10-02 05:00:24

标签: wordpress

我在多次尝试后问了这个问题,并在此论坛中讨论了类似的问题。我想从我的Wordpress网站的wp_users表中显示两个值(user_portal,user_url)。我为此写了一个函数。但是问题在于空值也会显示出来,这是不必要的。 我的代码如下:

function get_housenames() {
 global $wpdb;
    $houses = $wpdb->get_results( 'SELECT user_portal, user_url FROM 
wp_users WHERE user_url IS NOT NULL' );
    foreach ($houses as $house) {
        echo "<div class='user-box' style='background-color: #8697a8; color: 
#000; font-size: 24px; padding-left: 10px; padding-top: 4px; border-radius: 
8;'>";
        echo $house->user_portal .' <br>';
        echo "<a href class='url-box'style='color: #fff; font-size: 17px; 
margin-left: 12px; text-align: center;'>";
            echo $house->user_url;
        echo "</a>".' <br>';
        echo "</div>" .' <br>';
        }           
}
 add_shortcode('houses', 'get_housenames');
    ?>

此外,我想在显示的网址旁边的圆括号内显示相同user_url的用户数,例如“ www.nenow.in(12)”。

由于我是编程新手,所以希望得到一个很好且易于理解的答案。

1 个答案:

答案 0 :(得分:0)

您必须INNER JOIN到同一表,然后使用GROUP BY将结果分组。除了NULL外,我宁愿检查空字符串

a = 'sam'
print("hello",a)

回答问题

如果要忽略计数器-删除从INNER JOIN到ON的所有内容。我无法理解为什么要添加DISTINCT的情况。但是我想您想使用GROUP_CONCAT

hello sam

因此,您将获得一个对象数组。遍历其中看起来会让人觉得

global $wpdb;
$houses = $wpdb->get_results('
    SELECT t1.user_portal as user_portal, t1.user_url as user_url, t2.countUsers as countUsers
    FROM wp_users AS t1
    INNER JOIN (
        SELECT user_url, count(user_url) as countUsers
        FROM wp_users
        GROUP BY user_url
        ) AS t2
    ON t1.user_url = t2.user_url
    WHERE t1.user_url IS NOT NULL AND t1.user_url != ""
    ');
foreach ($houses as $house) {
    echo "<div class='user-box' style='background-color: #8697a8; color: #000; font-size: 24px; padding-left: 10px; padding-top: 4px; border-radius: 8;'>";
    echo $house->user_portal.' <br>';
    echo $house->user_url.'('.$house->countUsers.') <br>';
    echo "<a href class='url-box'style='color: #fff; font-size: 17px; margin-left: 12px; text-align: center;'>";
    echo $house->user_url;
    echo "</a>".' <br>';
    echo "</div>" .' <br>';
}