0

我正在尝试从下表中输出数据,如下所示:

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

标题(1 级等)必须保留在那里,并且目前是硬编码的。我知道有多少不同的级别,所以就是这样。我的问题是,我如何检测它是哪个级别并输出它的计数。无论是否有计数,我都需要那里的所有级别。如果数据库中没有计数数字,则计数将读取 0。

我现在这样做的方式是案例。我尝试使用if(),但失败了。使用案例的问题是,如果没有特定标题类型的计数,如果不会输出标题名称,那么我可以将计数打印为 0

你能帮我吗?

编辑 我不需要使用switch. 其他任何事情都会做。

我的代码

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>';
        }
    }
}

数据

"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"
4

1 回答 1

1

试试这种方法。我们没有尝试打印结果,而是将所有计数初始化为 0 并将它们存储在输出数组中。对于输出,我们循环遍历打印数据的数组。这使您甚至可以打印 0 值,因为它们不会因处理 DB 数据而改变

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>';}
    }
}
于 2013-09-20T15:31:51.613 回答