Amar Blouch February 2016

new badge on category list php

i am having category sidebar below code

Working Code:

//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

this working fine but what i want, i want to add a new badge image next to the category name. i tried a lot but failed to do, what i think is the most relevant code.

Please check this code:

//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'        

Answers


isnisn February 2016

If i understand you correctly you want to show "new badge" on those topics that doesnt have any messages in them. if so you got $q_count Just replace the while{} inside with an if{}else{}like so:

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

The problem is that you are doing another query that is returning empty array, and therefore you are getting badges on topics that has got messages. So either remove the while{} and replace with the if-else or you can just do a check like: if(empty($new)) { $newbadge1 = "<img src='images/mynew.gif'>"; } else { $newbadge1 = ''; }

Post Status

Asked in February 2016
Viewed 2,715 times
Voted 4
Answered 1 times

Search




Leave an answer