2012-10-18 27 views
-1

在下面的代码中...标签是“$ mname1”,现在只有三个值,四月,五月,六月。Php if function error

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 

我打印使用所有this..below表是代码:四月,五月和六月

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

此代码生成表只...我想问你,有没有什么办法可以编辑这个代码来打印表格,直到date("m")-1月,即直到9月份。

请帮我

回答

2

首先,你可以摆脱所有做这个IF-ELSEIF:

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

第二,关于你的问题 - 现在可以很容易地走了过来$months在对-loop - 直到“上个月”:

for($i=0; $i<$profile_stats['month']-1; $i++){ 
    echo $mname1[$i]; 
} 
+0

感谢Buddy..a多一点帮助......你可以在第三个代码中看到block..I被打印使用表名<= $ profile_stats ['Y耳朵']?>我应该在那个地方用一张照片打印一月,二月,三月,四月......。 –

+0

请帮帮我。 –

+1

您可以迭代:'for($ months as $ month):'并重新打印您发布的HTML代码,只需确保用'$ month'替换'$ mname1' – alfasin