dswwuo1223 2013-09-20 15:03
浏览 49
已采纳

从具有计数的表中输出数据

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"
  • 写回答

1条回答 默认 最新

  • douhan6738 2013-09-20 15:31
    关注

    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>';}
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么