Easiest way to alternate row colors in PHP/HTML?

前端 未结 19 1844
無奈伤痛
無奈伤痛 2020-12-01 03:41

Here\'s a PHP example of mine. Can anyone find a shorter/easier way to do this?


    
相关标签:
19条回答
  • 2020-12-01 04:29

    If you'd like to have less in-line PHP, a great way of doing it is via JavaScript.

    Using jQuery, it's simply:

    <script type="text/javascript">
    $('div:odd').css('background-color', 'red');
    </script>
    
    0 讨论(0)
  • 2020-12-01 04:29

    If you want to do it on the display end and are comfortable with or otherwise already using javascript, libraries like jQuery will often have :odd and :even selectors, which you can then hook up to adding specific style properties or hooking into CSS more generally by adding classes.

    0 讨论(0)
  • 2020-12-01 04:33

    Spot on Vilx but, always go minimal for speed (page weight)

    <tr class="'.(($c = !$c)?'odd':'even').'">
    
    0 讨论(0)
  • 2020-12-01 04:34

    A simple little function that works well for me.

     <?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="<?php echo $tablerows->rowclass("oddrow","evenrow"); ?>">
        <td>some data</td>
        <td>some more data</td>
      </tr>
      <?php } //end foreach?>
    </table> 
    
    0 讨论(0)
  • 2020-12-01 04:36

    Maybe a function with a static variable?

    <?php
    
    function alternate_row_color($css_class) {
        static $show = true;
    
        $show = !$show;
    
        if ($show) {
            return $css_class;
        } else {
            return NULL;
        }
    }
    
    ?>
    

    Then to use it (using your example):

    <?php foreach($posts as $post) { ?>
        <div class="<?=alternate_row_color('odd')?>">
            <?=$post?>
        </div>
    <?php } ?>
    
    0 讨论(0)
  • 2020-12-01 04:37

    You can abuse the $GLOBAL scope to store the current selected class state, see below table_row_toggle() function. Yes, I know its dirty to abuse the $GLOBAL scope, but hey, we're here to fix problems ain't we? :)

    Calling the table row toggle function in HTML:

    <tr <? table_row_toggle(); ?>>
    

    The function in 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 . '"';
    }
    
    0 讨论(0)
提交回复
热议问题