最简单的方式来替换行的颜色 PHP/HTML?

这是我的例子 PHP. 任何人都可以找到较短的/简单的方法要做吗?


 foreach/$posts as $post/{?
<div class="&lt;?=/$c++%2==1/?‘odd’:NULL?&gt;">
<?=$post?>
</div>
}?
<style>
.odd{background-color:red;}
</style>


其他语言的示例也很有意思。
已邀请:

快网

赞同来自:

原则上,没有。 它大致容易地赶紧。 你可以重写一点 shorter/cleaner,, 但这个想法将是一样的。 这就是我写它的方式:


[code]$c = true; // Let's not forget to initialize our variables, shall we?
foreach/$posts as $post/
echo '<div'. $c="!$c/?'" ':''="" ."="" class="odd">$post</div'.>

郭文康

赞同来自:

如果你想拥有更少的内置 PHP, 做到这一点的好方法 JavaScript.

使用 jQuery, 这很简单:


<script type="text/javascript">
$/'div:odd'/.css/'background-color', 'red'/;
</script>

帅驴

赞同来自:

使用 CSS3, 你可以做这样的事情:


div:nth-child/odd/
{
background-color: red
}


但如果您真的希望您的用户看到这种颜色,最好不要使用它。

八刀丁二

赞同来自:

聪明它是内置的:


{section name=rows loop=$data}
<tr class="{cycle values=" odd,even"}"="">
<td>{$data[rows]}</td>
</tr>
{/section}


也 Django:


{% for o in some_list %}
<tr class="{% cycle 'row1' 'row2' %}">
...
</tr>
{% endfor %}

君笑尘

赞同来自:

我总是称我的斑马线 "row0" 和 "row1"- 它略微简化代码。


php // you should always use the full opening tag for compatibility
$i = 0;
foreach /$rows as $row/ {
echo '<tr class="row' . /$i++ % 2/ . '"...';
}
?>

冰洋

赞同来自:

也许是一个静态变量的函数?


php

function alternate_row_color/$css_class/ {
static $show = true;

$show = !$show;

if /$show/ {
return $css_class;
} else {
return NULL;
}
}

?


然后用它 /使用你的例子/:


php foreach/$posts as $post/ { ?
<div class="&lt;?=alternate_row_color/'odd'/?&gt;">
<?=$post?>
</div>
php } ?

二哥

赞同来自:

[code]php $alt = true; foreach /$posts as $post/: $alt = !$alt; ?
<div<?php $alt="" '="" '';="" :="" ?="" class="odd" echo="">&gt;
<!-- Content -->
</div<?php>

诸葛浮云

赞同来自:

纯娱乐

假设您可以使用选择器 CSS3, 你可以做一些这样的事情


<div class="posts">
<? foreach/$posts as $post/{?>
<div>
<?=$post?>
</div>
<? }?>
</div>
<style>
div.posts div:odd{background-color:red;}
</style>


即使是支持 CSS2 和 mootools /图书馆 javascript/ 您可以通过此替换风格。 javascript


<script type="text/javascript">
// obviously this script line should go in a js file in a onload /or onDomReady/ function
$$/'div.posts div:odd'/.setStyle/'background-color','red'/;
</script>


如果你什么都没有 php a it, 您可以使用数组轻微简化代码。


 $isodd=array/'','odd'/;
$c=0;
foreach/$posts as $post/{?
<div class="&lt;?=$isodd[$c++%2]?&gt;">
<?=$post?>
</div>
}?

龙天

赞同来自:

您可以封装此逻辑如下:


php

class ListCycler {
private $cols, $offs, $len;

// expects two or more string parameters
public function __construct// {
$this-offs = -1;
$this->len = func_num_args//;
$this->cols = func_get_args//;

foreach/$this->cols as &$c/
$c = trim/strval/$c//;
}

// the object auto-increments every time it is read
public function __toString// {
$this->offs = /$this->offs+1/ % $this->len;
return $this->cols[ $this->offs ];
}
}
?>
<html>
<head>
<style>
ul#posts li.odd { background-color:red; }
ul#posts li.even { background-color:white; }
</style>
</head>
<body>
<div>
<h3>Posts:</h3>
<ul id="posts"><?php
$rc = new ListCycler/'odd','even'/;
foreach/$posts as $p/
echo "<li class='$rc'>$p";
?&gt;</ul>
</div>
</body>
</html>

裸奔

赞同来自:

function row_color/$cnt,$even,$odd/ { 
echo /$cnt%2/ ? "<tr bgcolor='\"$odd\"'>" : "<tr bgcolor='\"$even\"'>";
}


如何使用:


$cnt=0;
while /$row = mysql_fetch_array /$result// {
row_color/$cnt++,"e0e0e0","FFFFFF"/;
}


</tr></tr>

石油百科

赞同来自:

他太短了,但我可能会用清晰的名字把它裹着某种辅助函数。 因此,它变得更加明显,事情发生了更明显,并且您不必在您需要的所有模板中重复此逻辑。

龙天

赞同来自:

如果您想在展示方面进行,您可以方便地或其他您已经使用过 javascript, 图书馆喜欢 jQuery, 经常有选择者
http://docs.jquery.com/Selectors/odd

http://docs.jquery.com/Selectors/even
, 然后可以连接到添加某些
http://docs.jquery.com/CSS/css#namevalue
或连接K. CSS 更普遍,
http://docs.jquery.com/Attributes/addClass#class
.

龙天

赞同来自:

在一边 noe, 交替两个含义

a



b

, 在循环中做的好方法是:


x = a;
while / true / {
x = a + b - x;
}


您也可以在没有添加和减法的情况下执行此操作:


x = a ^ b ^ x;


在哪里

^

- 手术 XOR.

如果你只是想交替 0 和 1, 你能行的:


x = 0;
while / true / {
x = !x;
}


当然你可以使用

x

作为颜色的索引,风格的课程 CSS 等等。

三叔

赞同来自:

php /$i%2==1/ ? $bgc='#999999' : $bgc='#FFFFFF'; ?
'<div bgcolor=" bgcolor='.$bgc.'">';


</div>
<div class="answer_text">
您可以滥用该地区 $GLOBAL 要存储当前所选的类状态,请参阅下面的函数 table_row_toggle//. 是的,我知道它肮脏的虐待 $GLOBAL 行动领域,但嘿,我们在这里为了纠正问题,对吗? :/

调用表线函数 HTML:


<tr ;="" <?="" ?="" table_row_toggle="">&gt;


功能B. PHP:


/* function to toggle row colors in tables */
function table_row_toggle// {
/* check if $trclass is defined in caller */
if/array_key_exists/'trclass', $GLOBALS// {
$trclass = $GLOBALS['trclass'];
}

/* toggle between row1 and row2 */
if/!isset/$trclass/ || $trclass == 'row2'/ {
$trclass = 'row1';
} else {
$trclass = 'row2';
}

/* set $trclass in caller */
$GLOBALS['trclass'] = $trclass;

/* write the desired class to the caller */
echo ' class="' . $trclass . '"';
}


</tr></div>
<div class="answer_text">
发现 Vilx, 但总是为了速度而微钱 /页面重量/


<tr class="'.//$c = !$c/?'odd':'even'/.'">


</tr></div>
<div class="answer_text">
一个简单的小功能,适合我。


<?php 
class alternating_rows//
{
private $cycler = true;
//------------------------------------------------------------------------------
function rowclass/$row0,$row1/
{
$this->cycler = !$this-&gt;cycler;//toggle the cycler
$class=/$this-&gt;cycler/?$row0:$row1;
return $class;
}// end function rowclass
//------------------------------------------------------------------------------

}//end class alternating rows
?&gt;
<?php $tablerows= new alternating_rows//;?>
<table>
<tr>
<th scope="col">Heading 1</th>
<th scope="col">Heading 2</th>
</tr>
<?php foreach /$dataset as $row/{?>
<tr ;="" ?="" class="&lt;?php echo $tablerows-&gt;rowclass/" oddrow","evenrow"="">"&gt;
<td>some data</td>
<td>some more data</td>
</tr>
<?php } //end foreach?>
</table>


</div>
<div class="answer_text">
用这样的东西:


<?php
function cycle/&$arr/ {
$arr[] = array_shift/$arr/;
return end/$arr/;
}

$oddEven = array/'odd', 'even'/;
echo cycle/$oddEven/."\n";
echo cycle/$oddEven/."\n";
echo cycle/$oddEven/."\n";


</div>
<div class="answer_text">
在 PHP 我使用这个代码:


function alternate/$sEven = "even", $sOdd = "odd"/
{
static $iCount;
return /$iCount++ &amp; 1/ ? $sOdd :$sEven;
}

for/$i = 0; $i&lt; 5; $i++/
echo alternate//;


/*output:

even
odd
even
odd
even

*/


来源:
http://sklueh.de/2013/11/einfa ... -php/
/
</div>
</div>

卫东

赞同来自:

您可以滥用该地区 $GLOBAL 要存储当前所选的类状态,请参阅下面的函数 table_row_toggle//. 是的,我知道它肮脏的虐待 $GLOBAL 行动领域,但嘿,我们在这里为了纠正问题,对吗? :/

调用表线函数 HTML:


<tr ;="" <?="" ?="" table_row_toggle="">&gt;


功能B. PHP:


/* function to toggle row colors in tables */
function table_row_toggle// {
/* check if $trclass is defined in caller */
if/array_key_exists/'trclass', $GLOBALS// {
$trclass = $GLOBALS['trclass'];
}

/* toggle between row1 and row2 */
if/!isset/$trclass/ || $trclass == 'row2'/ {
$trclass = 'row1';
} else {
$trclass = 'row2';
}

/* set $trclass in caller */
$GLOBALS['trclass'] = $trclass;

/* write the desired class to the caller */
echo ' class="' . $trclass . '"';
}


</tr>

八刀丁二

赞同来自:

发现 Vilx, 但总是为了速度而微钱 /页面重量/


<tr class="'.//$c = !$c/?'odd':'even'/.'">


</tr>

詹大官人

赞同来自:

一个简单的小功能,适合我。


php 
class alternating_rows//
{
private $cycler = true;
//------------------------------------------------------------------------------
function rowclass/$row0,$row1/
{
$this-cycler = !$this->cycler;//toggle the cycler
$class=/$this->cycler/?$row0:$row1;
return $class;
}// end function rowclass
//------------------------------------------------------------------------------

}//end class alternating rows
?>
php $tablerows= new alternating_rows//;?
<table>
<tr>
<th scope="col">Heading 1</th>
<th scope="col">Heading 2</th>
</tr>
<?php foreach /$dataset as $row/{?>
<tr ;="" ?="" class="&lt;?php echo $tablerows-&gt;rowclass/" oddrow","evenrow"="">"&gt;
<td>some data</td>
<td>some more data</td>
</tr>
<?php } //end foreach?>
</table>

卫东

赞同来自:

用这样的东西:


php
function cycle/&$arr/ {
$arr[] = array_shift/$arr/;
return end/$arr/;
}

$oddEven = array/'odd', 'even'/;
echo cycle/$oddEven/."\n";
echo cycle/$oddEven/."\n";
echo cycle/$oddEven/."\n";


</div
<div class="answer_text">
在 PHP 我使用这个代码:


function alternate/$sEven = "even", $sOdd = "odd"/
{
static $iCount;
return /$iCount++ &amp; 1/ ? $sOdd :$sEven;
}

for/$i = 0; $i&lt; 5; $i++/
echo alternate//;


/*output:

even
odd
even
odd
even

*/


来源:
http://sklueh.de/2013/11/einfa ... -php/
/
</div>

董宝中

赞同来自:

在 PHP 我使用这个代码:


function alternate/$sEven = "even", $sOdd = "odd"/
{
static $iCount;
return /$iCount++ & 1/ ? $sOdd :$sEven;
}

for/$i = 0; $i< 5; $i++/
echo alternate//;


/*output:

even
odd
even
odd
even

*/


来源:
http://sklueh.de/2013/11/einfa ... -php/
/

要回复问题请先登录注册