php 向偶数和奇数 div 添加不同的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8540141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add different class to even and odd divs
提问by aborted
I have a block of PHP code that looks like this:
我有一段 PHP 代码,如下所示:
$flag = false;
if (empty($links))
{
echo '<h1>You have no uploaded images</h1><br />';
}
foreach ($links as $link)
{
$extension = substr($link, -3);
$image_name = ($extension == 'peg') ? substr($link, -15) : substr($link, -14);
($delete_submit) ? deleteImage('.' . $image_name, $link) : '';
echo '<div>';
echo '<table>';
echo '<tr><td class="fullwidth"><a class="preview_img" href="' . $link . '"><img src="' . $link . '" title="Click to enlarge" width="300" class="thumb" /></a></td></tr>';
echo '<tr><td><span class="default">Direct:</span> ';
echo '<input type="text" readonly="readonly" class="link-area" onmouseover="this.select();" value="' . $link . '" />';
echo '</td></tr>';
echo ($flag) ? '<hr /><br>' : '';
echo '</table>';
echo '<br>';
echo '</div>';
$flag = true;
}
I want the <div>
to include a different class based on if it is even or odd. If it is even, it gets X class, if it's odd it gets Y class.
我希望<div>
根据它是偶数还是奇数包含一个不同的类。如果是偶数,它会得到 X 类,如果它是奇数,它会得到 Y 类。
How do I do this in my case? I'm totally clueless and I don't know how to start!
在我的情况下,我该怎么做?我完全无能为力,我不知道如何开始!
回答by Rob W
Initialise a variable $count=0;
before the loop. Then place the following in the loop: ++$count%2?"odd":"even"
.
$count=0;
在循环之前初始化一个变量。然后将其放置在循环如下:++$count%2?"odd":"even"
。
$count = 0;
foreach ($links as $link)
{
$extension = substr($link, -3);
$image_name = ($extension == 'peg') ? substr($link, -15) : substr($link, -14);
($delete_submit) ? deleteImage('.' . $image_name, $link) : '';
echo '<div class="' . (++$count%2 ? "odd" : "even") . '">';
回答by konsolenfreddy
[...]
$i++;
echo '<div class="'.($i%2 ? 'odd':'even').'>';
[...]
回答by Andrew Hymanman
before the foreach, declare a boolean:
在 foreach 之前,声明一个布尔值:
$div_flag = false;
in the foreach, where you echo the div add this:
在 foreach 中,您在回显 div 的地方添加以下内容:
"class=".( $div_flag ? "'odd'" : "'even'" )
at the end of the foreach (or anywhere in it, really) add this:
在 foreach 的末尾(或其中的任何地方,真的)添加以下内容:
$div_flag = !$div_flag;
回答by Wex
If you're working on a prototype site, you could always implement a pure CSS solution:
如果你在原型网站上工作,你总是可以实现一个纯 CSS 解决方案:
div:nth-child(even) { /* Apply even class rules here */ }
div:nth-child(odd) { /* Apply odd class rules here */ }
The reason I recommend that you only use this for prototype sites is because the compatibility for :nth-child
does not support IE8 or below.
我建议你只将它用于原型站点的原因是因为它的兼容性:nth-child
不支持 IE8 或以下。
回答by Ovidiu
Add a variable for your loop.
为您的循环添加一个变量。
$c = true;
foreach ($links as $link)
{
$extension = substr($link, -3);
$image_name = ($extension == 'peg') ? substr($link, -15) : substr($link, -14);
($delete_submit) ? deleteImage('.' . $image_name, $link) : '';
echo '<div'.(($c = !$c)?' class="odd"':'').">
//echo '<div>';
echo '<table>';
echo '<tr><td class="fullwidth"><a class="preview_img" href="' . $link . '"><img src="' . $link . '" title="Click to enlarge" width="300" class="thumb" /></a></td></tr>';
echo '<tr><td><span class="default">Direct:</span> ';
echo '<input type="text" readonly="readonly" class="link-area" onmouseover="this.select();" value="' . $link . '" />';
echo '</td></tr>';
echo ($flag) ? '<hr /><br>' : '';
echo '</table>';
echo '<br>';
echo '</div>';
$flag = true;
}
回答by Brian Hoover
If you are using a modern browser, you can use CSS with something like this in your style sheet.
如果您使用的是现代浏览器,则可以在样式表中使用带有类似内容的 CSS。
div:nth-child(odd)
{
background:#ff0000;
}
div:nth-child(even)
{
background:#0000ff;
}
回答by useful
Try something like this
尝试这样的事情
$count = 0;
foreach($links as link) {
$count++;
print ($count % 2 == 1) ? "odd" : "even";
}
回答by fat_mike
Without counters:
没有计数器:
<?php $toggle_class = 'even';?>
<?php foreach ($links as $link):?>
<?php $toggle_class = ($toggle_class == 'odd' ? 'even' : 'odd');?>
<div class="<?php echo $toggle_class;?>">
Your div content...
</div>
<?php endforeach;?>