Norman
Norman

Reputation: 6365

Outputting data from a table with count

I'm trying to output data from the following table like this:

Level  1 - [Count Figure]
Level  2 - [Count Figure]
Level  3 - [Count Figure]
Level  4 - [Count Figure]
Level 10 - [Count Figure]

The Headings (Level 1 etc.) have to remain there, and are currently hard coded. I know how many different levels are there, so thats how. My problem is, how do I detect which level it is and output it's count. I need all the levels there regardless of if there is a count or not. If there no count figure from the database, then count'll read 0.

The way I'm doing it now is with case. I tried doing it with if(), but failed. Problem with using case is that, if there's no count for a particular heading type if wont output the heading name, so I can print count as 0

Can you help?

Edit I don't need to use switch. Anything else will do.

My code

function staticCountArticles($country){
global $conn;
    $countArticles = $conn->prepare('select count(id) as artcCount, artc_type from contributions where artc_status = 2 and artc_country = :country group by artc_type');
    $countArticles->bindParam(':country',$country);
    $countArticles->execute();
    $countArticles->bindColumn('artcCount',$artcCount);
    $countArticles->bindColumn('artc_type',$artcType);
    $hasArticles = $countArticles->rowCount();

    while( $artcData = $countArticles->fetch()) {
        switch($artcType){
            case 1:
            echo '<b>Level 1</b><br>';
            if($artcCount > 0){ echo $artcCount.'<br>';} else { echo 'Nothing here yet <br>';}
            break;

            case 2:
            echo '<b>Level 2</b><br>';
            if($artcCount > 0){ echo $artcCount.'<br>';} else { echo 'Nothing here yet <br>';}
            break;

            case 3:
            echo '<b>Level 3</b><br>';
            if($artcCount > 0){ echo $artcCount.'<br>';} else { echo 'Nothing here yet <br>';}
            break;

            case 4:
            echo '<b>Level 4</b><br>';
            if($artcCount > 0){ echo $artcCount.'<br>';} else { echo 'Nothing here yet <br>';}
            break;

            case 10:
            echo '<b>Level 10</b><br>';
            if($artcCount > 0){ echo $artcCount.'<br>';} else { echo 'Nothing here yet <br>';}
            break;

            default:
            echo 'Unidentified type encountered<br>';
        }
    }
}

Data

"id"    "artc_type" "artc_status"   "artc_user" "artc_country"
"1"     "1"         "2"             "1"         "US"
"2"     "1"         "2"             "1"         "US"
"3"     "1"         "2"             "1"         "US"
"4"     "2"         "2"             "2"         "US"
"5"     "2"         "2"             "2"         "US"
"6"     "2"         "2"             "2"         "US"
"7"     "4"         "2"             "2"         "US"
"8"     "4"         "2"             "3"         "US"
"9"     "4"         "2"             "3"         "US"
"10"    "10"        "2"             "3"         "US"
"11"    "10"        "2"             "4"         "US"
"12"    "10"        "2"             "4"         "US"
"13"    "10"        "2"             "4"         "US"
"14"    "10"        "2"             "4"         "US"

Upvotes: 0

Views: 31

Answers (1)

Chris Rasco
Chris Rasco

Reputation: 2731

Try this approach instead. Instead of trying to print the results as you go, we init all counts to 0 and store them in an output array. For output, we loop through the array printing the data. This allows you to print even the 0 values as they wouldn't have been changed by the processing of the DB data

function staticCountArticles($country){
    global $conn;
    $countArticles = $conn->prepare('select count(id) as artcCount, artc_type from contributions where artc_status = 2 and artc_country = :country group by artc_type');
    $countArticles->bindParam(':country',$country);
    $countArticles->execute();
    $countArticles->bindColumn('artcCount',$artcCount);
    $countArticles->bindColumn('artc_type',$artcType);
    $hasArticles = $countArticles->rowCount();

    // Init our output array
    $output = Array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 10 => 0);

    while( $artcData = $countArticles->fetch()) {
        if($artcCount > 0) $output[$artcType] = $artcCount;
    }

    // Print the results
    foreach($output as $level => $item)
    {
        echo '<b>Level '.$level.'</b><br>';
            if($item > 0){ echo $item.'<br>';} else { echo 'Nothing here yet <br>';}
    }
}

Upvotes: 1

Related Questions