简体   繁体   English

如何在php foreach循环中替换div元素

[英]How can I alternate div elements in php foreach loop

I have a set of arrays I'm getting back from the database, I'm using a php foreach loop to go through them. 我有一组要从数据库中获取的数组,我正在使用php foreach循环来遍历它们。 If I'm returning 5 arrays [1, 2, 3, 4, 5] , each one containing a (time, status, location) I want to style all even arrays with a certain div and each odd ones with a different div. 如果我要返回5个数组[1, 2, 3, 4, 5] ,则每个数组都包含一个(时间,状态,位置),我想为所有偶数数组设置特定的div样式,为每个奇数数组设置不同的div样式。 How would I do this? 我该怎么做?

I'm assuming I would use the loop with modulo operator. 我假设我将使用带模运算符的循环。

for ($i = 1; $i <= $count; $i++)
    if ($i % 2 == 0) {

    } else {

    }
}

<?php foreach ($reservation as $seat) : ?>

If it is array 1, 3, 5. 如果是数组1、3、5

 <div style="font-size:20px; background-color: #red; float:left;">
 <?php echo $seat['location']; ?>
 <?php echo $seat['time']; ?> 
 <?php echo $seat['status']; ?> </div>

If it is array 2, 4 如果是数组2、4

<div style="font-size:20px; background-color: #blue; float:right;">
 <?php echo $seat['location']; ?>
 <?php echo $seat['time']; ?> 
 <?php echo $seat['status']; ?> </div>


<?php endforeach ?>

Looks reasonable, but: 看起来很合理,但是:

background-color: #blue;

doesn't make sense. 没有道理。 The '#' is used as a prefix for hexadecimal color values, whilst you have a color name, so you'd want: “#”用作十六进制颜色值的前缀,而您具有颜色名称,因此需要:

background-color: blue;

or 要么

background-color: #0000ff;

Also, are you aware that, in modern browsers (ie IE9+ ), alternately styled elements can be achieved solely via CSS3, eg 另外,您知道吗,在现代浏览器(即IE9 + )中,仅通过CSS3即可实现替代样式的元素,例如

div:nth-child(odd) { background-color: blue; }

Check 校验

You do not have to use a foreach loop to enumerate all elements of an array: 您不必使用foreach循环来枚举数组的所有元素:

for ($i=1; $i <= $count; $i++) {
    $seat = $reservation[$i-1];
    if ($i % 2 == 0) {
        // Do your 2 and 4
    } else {
        // Do your 1, 3 and 5
    }
}

I'd recommend setting CSS-classes "odd" and "even" to the divs and then style them in a separate file. 我建议将CSS类“ odd”和“ even”设置为div,然后在单独的文件中设置样式。 If you want, you can also use CSS3 pseudo classes :nth-child(odd) and :nth-child(even) . 如果需要,还可以使用CSS3伪类:nth-child(odd):nth-child(even)

As long as you keep a counter, your modulo test can even be done in-line, within the foreach loop: 只要您保留一个计数器,您的模数测试甚至可以在foreach循环中在线完成:

$ cat divloop.php 
<?php

$a=array( "one", "two", "three", "four", "five" );

$fmt="<div class='reservation %s'>%s</div>\n";

$count=0;

foreach ($a as $item) {
  printf($fmt, ++$count % 2 == 0 ? "even" : "odd", $item);
}

And the results: 结果:

$ php divloop.php 
<div class='reservation odd'>one</div>
<div class='reservation even'>two</div>
<div class='reservation odd'>three</div>
<div class='reservation even'>four</div>
<div class='reservation odd'>five</div>
$ 

At this point it's safer to use explicit classes rather than rely on browser functionality that may not be available across the board. 此时,使用显式类而不是依赖可能无法全面使用的浏览器功能会更安全。 At least everyone seems to understand basic CSS. 至少每个人似乎都了解基本的CSS。 :) :)

.reservation {
  font-size: 20px;
  float: left;
}

.even {
  background-color: #blue;
}

.odd {
  background-color: #red;
}

what if you put the counter in the foreach. 如果您将计数器放在foreach中该怎么办。

$count = 0;
<?php foreach ($reservation as $seat) : ?>
if ($count % 2 == 0){
  //even div 
}else {
  // odd div
}
$count++;

<?php endforeach ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM