2012-10-10 129 views
0

可能重复:
CSS3 selector last element that is not of class Xfor循环,最后一个值

我有这样的循环:

for($t=0; $t<$numRowszzx; $t++) 
{ 
    if(!isset($_GET["prodcat"])) 
    { 
     header("Location: products.php?prodcat=".$rowzx[0]["category_id"]); 
    } 
    $categoryName = $rowzx[$t]["category_name"]; 

    if($rowzx[$t]["category_id"]==$_GET["prodcat"]) 
    { 
     ?> 
     <li style="height:30px; line-height:30px; color:#2a598a; font-weight:bold; font-size:14px;"><img src="<?=$site_url?>/images/parrows.png" width="13" height="7" /><span style="padding-left:12px;"><a href="<?=$site_url?>/products.php?prodcat=<?=$rowzx[$t]["category_id"];?>" style=" color:#f15a22; font-weight:bold; font-size:14px;"><?=$categoryName?></a></span></li> 
     <?php 
    } else { 
     ?> 
     <li style="<?php if($i > $numRowszzx-1){ ?>margin-bottom:500px;<?php } ?> height:30px;line-height:30px; color:#2a598a; font-weight:bold; font-size:14px;"><img src="<?=$site_url?>/images/parrows.png" width="13" height="7" /><span style="padding-left:12px;"><a href="<?=$site_url?>/products.php?prodcat=<?=$rowzx[$t]["category_id"];?>" style=" color:#2a598a; font-weight:bold; font-size:14px;"><?=$categoryName?></a></span></li> 
     <?php 
    } 
} 

现在我需要的是让最后 ... 在这段代码中,我需要做的,如果是最后一个,给它margin-bottom:500px;

else { 
    ?> 
    <li style="<?php if($i > $numRowszzx-1){ ?>margin-bottom:500px;<?php } ?> height:30px;line-height:30px; color:#2a598a; font-weight:bold; font-size:14px;"><img src="<?=$site_url?>/images/parrows.png" width="13" height="7" /><span style="padding-left:12px;"><a href="<?=$site_url?>/products.php?prodcat=<?=$rowzx[$t]["category_id"];?>" style=" color:#2a598a; font-weight:bold; font-size:14px;"><?=$categoryName?></a></span></li> 
    <?php 
} 

任何帮助吗?

+2

内联样式是不好的做法,您可以使用CSS选择器将样式应用到最后一个样式。 – Gordon

回答

0

如果你想要最后的li与margin-bottom:500px。使用CSS,不要使用内联样式。

您可以通过将:last-child添加到您的节点来轻松进行设计。

li:last-child { 
    margin-bottom:500px; 
} 
1
if($t+1 == $numRowszzx) 
{ 
//this is the last iteration 
} 
1

您可以添加一个if语句与否则你条件$ T ==($ numRowszzx-1)

喜欢的东西:

else 
{ 
    if($t == ($numRowszzx-1)) // So we reached the upper bound of your for loop 
    { 
     // Your <li> here 
    } 
} 

你可能改写正如吉米提到的那样,它也是一种陈述。

+1

如果这是'else'语句中的唯一操作,那么可以将其重写为'elseif'。 (但是我会在CSS中解决像这样的问题,就像Gordon评论过的那样)。 –