Php 从带有计数的表中输出数据

Php 从带有计数的表中输出数据,php,Php,我试图从下表中输出数据,如下所示: Level 1 - [Count Figure] Level 2 - [Count Figure] Level 3 - [Count Figure] Level 4 - [Count Figure] Level 10 - [Count Figure] 标题(1级等)必须保留在那里,并且目前是硬编码的。我知道有多少不同的等级,所以这就是为什么。 我的问题是,如何检测它是哪个级别并输出它的计数。我需要那里的所有级别,无论是否有计数。 如果数据库中没有计数

我试图从下表中输出数据,如下所示:

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

我现在的做法是用case。我尝试使用
if()
执行此操作,但失败了。 用例的问题是,如果某个特定的标题类型没有计数,if不会输出标题名称,所以我可以将计数打印为0

你能帮忙吗

编辑 我不需要使用
开关
。别的什么都行

我的代码

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

试试这种方法。我们将所有计数初始化为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>';}
    }
}
function staticCountArticles($country){
全球$conn;
$countArticles=$conn->prepare('从artc_status=2和artc_country=:按artc_类型划分的国家组中选择count(id)作为artcCount,artc_类型');
$countArticles->bindParam(':country',$country);
$countArticles->execute();
$countArticles->bindColumn($artcCount',$artcCount);
$countArticles->bindColumn($artc_type',$artcType);
$hasArticles=$countArticles->rowCount();
//初始化我们的输出数组
$output=Array(1=>0,2=>0,3=>0,4=>0,10=>0);
而($artcData=$countArticles->fetch()){
如果($ARTCOUNT>0)$output[$artcType]=$ARTCOUNT;
}
//打印结果
foreach($level=>$item的输出)
{
回显“级别”。$级别。“
”; 如果($item>0){echo$item.
';}其他{echo'这里还没有任何内容
';} } }
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>';}
    }
}