for循环,最后一个值

时间:2012-10-10 12:43:20

标签: php loops html-lists

  

可能重复:
  CSS3 selector last element that is not of class X

我有这个循环:

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

现在我需要的是获得最后的 li ... 在此代码中,如果 li 是最后一个,我需要这样做,以便给它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
}

请帮忙吗?

3 个答案:

答案 0 :(得分:1)

if($t+1 == $numRowszzx)
{
//this is the last iteration
}

答案 1 :(得分:1)

您可以在 else

中添加条件$ t ==($ numRowszzx-1)的 if语句

类似的东西:

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

如Jimmy所述,您可以将其重写为else if语句。

答案 2 :(得分:0)

如果你想要margin-bottom:500px的最后一个李。使用css,不要使用内联样式。

您可以通过向节点添加:last-child来轻松设置样式。

li:last-child {
    margin-bottom:500px;
}