类别列表上的新徽章php

时间:2016-02-08 13:01:50

标签: php

我的代码侧栏位于代码

之下

工作代码:

//Showing categories function
function getCats(){
global $db;

$q_cats = "select * from cats ORDER BY cat_name ASC";
$run_cats = mysqli_query($db, $q_cats);
while($row_cat = mysqli_fetch_array($run_cats)){
    $cat_id = $row_cat['cat_id'];
    $cat_name = $row_cat['cat_name'];
        $q_count = "select * from messages where cat_id='$cat_id'";
        $run_count = mysqli_query($db, $q_count);
        $cat_sms_count = mysqli_num_rows($run_count);
            if($cat_sms_count < 1){
                $cat_sms_count = 0;
                }       
    echo "<a href='category.php?category=$cat_id' class='list-group-item'>$cat_name<span class='badge'>$cat_sms_count</span></a>";

    }//while row END here

        $q_tmsgs = "select * from messages";
        $run_tcount = mysqli_query($db, $q_tmsgs);
        $tsms_count = mysqli_num_rows($run_tcount);
        echo "<a href='allindb.php' class='list-group-item t-list'>Total Messages in DB<span class='badge'>$tsms_count</span></a>";

    }//Function getCats END here

这个工作正常,但我想要的,我想在类别名称旁边添加一个新的徽章图像。我尝试了很多,但未能做到,我认为是最相关的代码。

请检查此代码:

//Showing categories function
function getCats(){
global $db;

$q_cats = "select * from cats ORDER BY cat_name ASC";
$run_cats = mysqli_query($db, $q_cats);
while($row_cat = mysqli_fetch_array($run_cats)){
    $cat_id = $row_cat['cat_id'];
    $cat_name = $row_cat['cat_name'];
        $q_count = "select * from messages where cat_id='$cat_id'";
        $run_count = mysqli_query($db, $q_count);
        $cat_sms_count = mysqli_num_rows($run_count);
        //problem area----------------------------------
                    $q_new = "select * from messages where cat_id='$cat_id' ORDER BY date DESC LIMIT 20";
                    $run_new = mysqli_query($db, $q_new);
                    while($new = mysqli_num_rows($run_new)){
                        $newbadge = $new['cat_id'];
                        }
                        if($cat_id==$newbadge){
                            $newbadge1 = "<img src='images/mynew.gif'>";
                            }

        //--------------------------------------------              
            if($cat_sms_count < 1){
                $cat_sms_count = 0;
                }       
    echo "<a href='category.php?category=$cat_id' class='list-group-item'>$cat_name $newbadge1<span class='badge'>$cat_sms_count</span></a>";

    }//while row END here

        $q_tmsgs = "select * from messages";
        $run_tcount = mysqli_query($db, $q_tmsgs);
        $tsms_count = mysqli_num_rows($run_tcount);
        echo "<a href='allindb.php' class='list-group-item t-list'>Total Messages in DB<span class='badge'>$tsms_count</span></a>";

    }//Function getCats END here

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想在那些没有任何消息的主题上显示“新徽章”。如果是这样你得到$q_count 只需将while{}内部替换为if{}else{},如下所示:

if($q_count == 0) { $newbadge1 = "<img src='images/mynew.gif'>"; } else { $newbadge1 = ''; }

问题在于您正在执行另一个返回空数组的查询,因此您将获得有消息的主题的徽章。因此,要么删除while{}并使用if-else替换,要么只需执行以下检查:     if(empty($new)) { $newbadge1 = "<img src='images/mynew.gif'>"; } else { $newbadge1 = ''; }