开发者

make the output to odd and even?

开发者 https://www.devze.com 2023-03-13 07:11 出处:网络
the original code is as the following: <?php foreach($values as $value){ $downlink=node_load($value);

the original code is as the following:

  <?php

    foreach($values as $value){

        $downlink=node_load($value);

    echo '<li><input type="checkbox" checked="checked"/>.$downlink->title.'</li>';
    }

?>

i want to make the output to

 <li class="odd">...</li>
<li class="even">...</li>
.....

this is my ways. but it is not work.

$i=0;
foreach($values as $value){

            $downlink=node_load($value);
          if($i%2==0){
echo '<li class="even"开发者_开发百科><input type="checkbox" checked="checked"/>.$downlink->title.'</li>';}
else{
        echo '<li class="odd"><input type="checkbox" checked="checked"/>.$downlink->title.'</li>';
}
$i++;
        }


My own personal method for doing this server side is:

foreach ($foo as $bar) { 

  $class = ($class == "even") ? "odd" : "even";

  echo "<li class='".$class."'>blah blah</li>";

}


You can achieve odd/even coloring with CSS alone:

li:nth-child(even) {background: #CCC}
li:nth-child(odd) {background: #FFF}

Browser support for nth-child selectors may vary though.

See http://www.w3.org/Style/Examples/007/evenodd


another note :

start from $i=1;

and its start from odd


Your way of doing is perfect but it has a small glitch that might be causing it not to work as intended. It seems you are missing a ' (single-quote) after the starting <li> tag after "/>" in both your statements.

This is how it should look (notice the bold single-quote):

echo '<li class="even"><input type="checkbox" checked="checked"/>' . $downlink->title . '</li>'

Another thing though not related - it is not required to put an ending / (forward-slash) at the end of the starting <li> tag because it has it own ending tag </li>


Modify like below:

<?php
    $i=0;
    foreach($values as $value){
    $downlink=node_load($value); ?>
    <li class="<?php echo ($i%2==0) ? 'even' :'odd'" ?>>
    <input type="checkbox" checked="checked"><?php echo $downlink->title;?></li>
    <?php 
          $i++;
    }//end of foreach

Note: why u put checked=checked for all checkbox?? your logic may b wrong here


I think you are missing a quote:

$i=0;
foreach($values as $value){
        $downlink=node_load($value);
         if($i%2==0){
             echo '<li class="even"><input type="checkbox" checked="checked"/>'.$downlink->title.'</li>';}
else{
        echo '<li class="odd"><input type="checkbox" checked="checked"/>.$downlink->title.'</li>';
}
$i++;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消