Add different class to even and odd divs

2020-08-09 07:03发布

I have a block of PHP code that looks like this:

$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>&nbsp;';
        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.

How do I do this in my case? I'm totally clueless and I don't know how to start!

标签: php html css
8条回答
贼婆χ
2楼-- · 2020-08-09 07:10

Initialise a variable $count=0; before the loop. Then place the following in the loop: ++$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") . '">';
查看更多
Bombasti
3楼-- · 2020-08-09 07:12

before the foreach, declare a boolean:

$div_flag = false;

in the foreach, where you echo the div add this:

"class=".( $div_flag ? "'odd'" : "'even'" )

at the end of the foreach (or anywhere in it, really) add this:

$div_flag = !$div_flag;
查看更多
手持菜刀,她持情操
4楼-- · 2020-08-09 07:14

If you are using a modern browser, you can use CSS with something like this in your style sheet.

div:nth-child(odd)
{
  background:#ff0000;
}
div:nth-child(even)
{
  background:#0000ff;
}
查看更多
啃猪蹄的小仙女
5楼-- · 2020-08-09 07:17

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>&nbsp;';
    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;
}
查看更多
家丑人穷心不美
6楼-- · 2020-08-09 07:18

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;?>
查看更多
Fickle 薄情
7楼-- · 2020-08-09 07:21
[...]
$i++;
echo '<div class="'.($i%2 ? 'odd':'even').'>';
[...]
查看更多
登录 后发表回答