【问题标题】:Different css styling for last element of a php array Pt.2php数组Pt.2的最后一个元素的不同css样式
【发布时间】:2013-08-28 14:29:32
【问题描述】:

好的,这是我的新问题,以及我想要完成的任务的完整描述。我使用了您给我的建议并且效果很好,但是当我将 ORDER by information 更改为不同的字段和 if 语句时,它变得疯狂。

这就是我需要的,我在一个页面上有两列,当处理数组时,我需要它来显示从左到右从上到下填充每一列的结果。每一项之间都有一条分割线

<div class="centerBoxContentsFeatured centeredContent back  vLine " style="width:50%;">

每一项下都有一个

<div class="product-col" >.  

如果项目是第 3、5、7 等等......它将在左侧并且不需要“vLine” div,如果它是最后一个项目,则不需要“ product-col” div,因为最后 2 项不必有底部分隔符,但如果整个页面只有 2 项,底部分隔符可以保留。我做错了什么?

<?php
$count_temp  = count ($productid);
for ($i = 0; $i < $count_temp; ++$i) {
    $artist = get_the_title();
    if (($productartist[$i] == $artist ) && $categoryID[$i] ==1 ) {
    ?>
//Content//
<?php if (!($i === 0) && !($i % 2)  &&
         ($productid[$i] == 1) &&
         ( $i === (count ($productid) - 1))) {
             echo'<div class="product-col-none" >';
      }
      else  { echo '<div class="product-col" >';} 
?>
//Content//
<? if !( $i === (count ($productid) - 1))
   { 
        echo '<div class="centerBoxContentsFeatured centeredContent back  vLine " '.
             'style="width:50%;">';
   }
   else { 
        echo '<div class="centerBoxContentsFeatured centeredContent back " '.
             'style="width:50%;">';}
?>

【问题讨论】:

  • 请提供您对应的其他问题的链接。
  • 让我们对“Pt.1”说...网址就可以了
  • 好吧,如果我的回答对你的问题很有效......那么你可以接受这个答案

标签: php css arrays


【解决方案1】:

你应该在这里写下你的 css 文件代码,你的 html 嵌入的 php 代码让事情变得复杂和不清楚。

如果您只想为 odd 编号 (3,5,7..) 的项目设置不同的样式,而为 最后一个 项目设置不同的样式,那么我会推荐使用css pseudo classes

您可以使用 css 伪类 last-child 作为您的最后一项。

li:last-child
{
  // Your specific style for last item goes here
}

您还可以使用 css 伪类 nth-child(odd) 来交替奇数项目

li:nth-child(odd)
{
  // Your specific style for odd items goes here
}

我希望这就是您正在寻找的,当您可以在客户端完成设置时,为什么要给服务器端负载。

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 2016-02-09
    • 2018-03-15
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多