-1

在下面的代码中...标签是“$mname1”,现在只取三个值 APRIL、MAY、JUNE。

if ( $profile_stats['month'] < $old_month && $profile_stats['year'] == $old_year
                    || $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y') ) {
                    ?>

<?php
                    }
                    else {

                        if ( $profile_stats['month'] == 1      ) {
                        $mname1 = 'January';
                        }
                        elseif ( $profile_stats['month'] == 2 ) {
                        $mname1 = 'Feburary';
                        }
                        elseif ( $profile_stats['month'] == 3 ) {
                        $mname1 = 'March';
                        }
                        elseif ( $profile_stats['month'] == 4 ) {
                        $mname1 = 'April';
                        }
                        elseif ( $profile_stats['month'] == 5 ) {
                        $mname1 = 'May';
                        }
                        elseif ( $profile_stats['month'] == 6 ) {
                        $mname1 = 'June';
                        }
                        elseif ( $profile_stats['month'] == 7 ) {
                        $mname1 = 'July';
                        }
                        elseif ( $profile_stats['month'] == 8 ) {
                        $mname1 = 'August';
                        }
                        elseif ( $profile_stats['month'] == 9 ) {
                        $mname1 = 'September';
                        }
                        elseif ( $profile_stats['month'] == 10 ) {
                        $mname1 = 'October';
                        }
                        elseif ( $profile_stats['month'] == 11 ) {
                        $mname1 = 'November';
                        }
                        elseif ( $profile_stats['month'] == 12 ) {
                        $mname1 = 'December';
                        }

                        if ( $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y') ) {
                        $mname1 = "<i>Current Month</i> - $mname1";
                        }

                        if ( $profile_stats['month'] == 04 && $profile_stats['year'] == 2012 ) {
                        $newstats_alert = "<p><b style='color: red;'><i>April 2012 stats are only partial due to implementation of this feature mid month.</i></b></p>";    

                        }
                        else {
                        $newstats_alert = NULL;
                        }

                    ?>

这里,

  date("m") shows value 10 10 10
  date("Y") shows 12 12 12
  $profile_stats['month'] shows 05 04 03
  $profile_stats['year'] shows 2012 2012 2012
  $old_month shows 10 10 10
  $old_year shows 2011 2011 2011

我正在使用所有这些打印一张表格。下面是代码:

<tr>
        <td colspan='4' style='border: 2px solid black;'><div align="center"><b><u><?=$mname1?> <?=$profile_stats['year']?></u></b><?=$newstats_alert?></div></td>
 </tr>
 <tr>
<td>Profile Views</td>
<td>Course Views</td>
<td>General Enquiries</td>
<td>Course Enquiries</td>
 </tr>
 <tr>
<td><?=($profile_stats['profile_views'] + 0)?></td>
<td><?=($course_views[$profile_stats['year']."-".$profile_stats['month']] + 0)?></td>
<td><?=($profile_stats['general_enquiries'] + 0)?></td>
<td><?=($course_enquiries[$profile_stats['year']."-".$profile_stats['month']] + 0)?></td>
</tr>


<tr>
<td colspan='2'>Total Views: <?=($profile_stats['profile_views'] + $course_views[$profile_stats['year']."-".$profile_stats['month']] + 0)?></td>
<td colspan='2'>Total Enquiries: <?=($profile_stats['general_enquiries'] + $course_enquiries[$profile_stats['year']."-".$profile_stats['month']] + 0)?></td>
 </tr>

这段代码只为 4 月、5 月和 6 月生成表格……我想问你,有什么方法可以编辑这段代码以将表格打印到date("m")-1一个月,即到 9 月。

请帮我

4

1 回答 1

2

首先,您可以通过执行以下操作摆脱所有 if-elseif:

$months = array("January","February", "March",... );
$mname1 = $months[$profile_stats['month']-1];

其次,关于您的问题 - 现在很容易$months在 for 循环中进行检查 - 直到“上个月”:

for($i=0; $i<$profile_stats['month']-1; $i++){
   echo $mname1[$i];
}
于 2012-10-18T20:16:27.923 回答