-1
$jan5 = 0;
$feb5 = 0;
$mar5 = 0;
$apr5 = 0;
$may5 = 0;
$jun5 = 0;
$jul5 = 0;
$aug5 = 0;
$sep5 = 0;
$oct5 = 0;
$nov5 = 0;
$dec5 = 0;
$jan4 = 0;
$feb4 = 0;
$mar4 = 0;
$apr4 = 0;
$may4 = 0;
$jun4 = 0;
$jul4 = 0;
$aug4 = 0;
$sep4 = 0;
$oct4 = 0;
$nov4 = 0;
$dec4 = 0;
$jan3 = 0;
$feb3 = 0;
$mar3 = 0;
$apr3 = 0;
$may3 = 0;
$jun3 = 0;
$jul3 = 0;
$aug3 = 0;
$sep3 = 0;
$oct3 = 0;
$nov3 = 0;
$dec3 = 0;  

这是我尝试制作时最耗时的代码,例如我希望默认值$jan5等于零。有人可以教我如何缩短这个代码吗?非常感谢您的阅读

$date = 'smi_initialdate';
$level = 'smi_level';
$year = '2012';

$result1 = mysql_query("SELECT *, DATE_FORMAT( $date, '%m' ) As monthz FROM eis_mngt_sales_iti WHERE year($date) = '$year' ORDER BY $date");
while($row=mysql_fetch_array($result1))
{
$count++;


    if($row['smi_level']=='5star')
    {
        if($row['monthz']==1)
        {
        $jan++;
        }elseif($row['monthz']==2)
        {
        $feb++;
        }elseif($row['monthz']==3)
        {
        $mar++;
        }elseif($row['monthz']==4)
        {
        $apr++;
        }elseif($row['monthz']==5)
        {
        $may++;
        }elseif($row['monthz']==6)
        {
        $jun++;
        }elseif($row['monthz']==7)
        {
        $jul++;
        }elseif($row['monthz']==8)
        {
        $aug++;
        }elseif($row['monthz']==9)
        {
        $sep++;
        }elseif($row['monthz']==10)
        {
        $oct++;
        }elseif($row['monthz']==11)
        {
        $nov++;
        }elseif($row['monthz']==12)
        {
        $dec++;
        }
    }   
}

我决定这样 $jan5 = 0;$feb5 = 0;$mar5 = 0;$apr5 = 0;$may5 = 0;$jun5 = 0;$jul5 = 0;做,以便如果数据没有传递到这个条件if($row['smi_level']=='5star'),它们的返回值为零(0)。

这样我的输出就可以适应这段代码

[<?php  echo$jan5.",".$feb5.",".$mar5.",".$apr5.",".$may5.",".$jun5.",".$jul5.",".$aug5.",".$sep5.",".$oct5.",".$nov5.",".$dec5;
?]

如果不使用上面的代码,$jan5 = 0;$feb5 = 0;$mar5 = 0;$apr5 = 0;$may5 = 0;$jun5 = 0;$jul5 = 0;................当我回显或打印它时,我看起来像这样 1,2,5,11,12 但使用它(上面的第一个示例代码)输出将是 1,0,0, 0,0,0,0,1,2,5,11,12。

谢谢,对不起,如果这变得混乱,对不起我的语法

4

2 回答 2

1

这与您的问题略有不同,但建议的代码改进/修订:

    switch($row['monthz']){
      case 1:
        $jan++;
        break;

      case 2:
        $feb++;
        break;

      case 3:
        $mar++;
        break;

      case 4:
        $apr++;
        break;

      case 5:
        $may++;
        break;

      case 6:
[ ... ]

它看起来更干净,代码更少。PHP 手册了解更多详情。

于 2012-10-22T05:17:36.157 回答
0

这可能会奏效......

$month = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
$day = array(3,4,5);

foreach($day as $d)
{
    foreach($month as $m)
    {
        ${$m.$d} = 0;
    }
}

但我建议您使用Array

于 2012-10-22T04:56:34.280 回答