最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - change css for every 4th row - Stack Overflow

programmeradmin3浏览0评论

I want to display every 4th row grabbed from the database with different CSS:

<div class="last"></div>

but the rest of them should be displayed normaly:

<div></div>

I am using simple query to grab data from database:

SELECT LEN(column_name) FROM table_name

I saw this type of thing but I don't remember how to do it properly. Any ideas?

I want to display every 4th row grabbed from the database with different CSS:

<div class="last"></div>

but the rest of them should be displayed normaly:

<div></div>

I am using simple query to grab data from database:

SELECT LEN(column_name) FROM table_name

I saw this type of thing but I don't remember how to do it properly. Any ideas?

Share Improve this question edited Jul 31, 2011 at 7:49 mu is too short 435k71 gold badges858 silver badges818 bronze badges asked Jul 31, 2011 at 7:33 cutecute 212 bronze badges 2
  • Please post the PHP code you're using. – Dogbert Commented Jul 31, 2011 at 7:38
  • Added CSS and Javascript tags as they (along with php) are the best options for dynamically applying classes to a table of data. – Moses Commented Jul 31, 2011 at 7:41
Add a comment  | 

8 Answers 8

Reset to default 6

using css like

tr:nth-child(4n) {  
   background-color:yellow;
} 
<div<?php echo $i % 4 == 0 ? ' class="last"' : '' ?>>...</div>

Use % to get division remainder, if it equals zero then it is exatly forth div and we need to print it with class, otherwise print nothing.

This is not the sort of thing you should do in SQL. Have a variable in PHP act as a counter starting from 0, and use the alternate class when the modulus of 4 of the counter is 3.

<?php 
    $query = "SELECT LEN(column_name) FROM table_name";
    $result = mysql_query($query);
    $i = 1;
    echo  "<table>";
    while ($row = mysql_fetch_assoc($result)) {
        echo  "<tr><td>";
        echo  "<p";
        if ($i % 4 == 0) print " class='stripe'";
       echo  ">" . $row['item'] . "</p></td></tr>";
        $i++;
     }
    echo  "</table>";
?>

the qustion is absurd I dnt ustand why you are trying to set your css style in the db.. its illogical to do so and you will unnecessarily increase the complexity of your query.

Anyways my suggestion would be to do the same in your web tier i.e once you retrieve the records from the db and rendering the page. just write sm logic that will add the css class..

Something like this:

<?php 
    $query = "SELECT LEN(column_name) FROM table_name";
    $result = mysql_query($query);
    $i = 1;
    print "<table>";
    while ($row = mysql_fetch_assoc($result)) {
        print "<tr><td>";
        print "<p";
        if ($i % 4 == 0) print " class='stripe'";
        print ">" . $row['item'] . "</p></td></tr>";
        $i++;
     }
     print "</table>";
?>

Don't do it with SQL queries, do it with PHP:

  $result = mysql_query("SELECT * FROM table_name");
  $i = 1;    
  while($row = mysql_fetch_array($result))
  {
     $class = ($i % 4 == 0) ? ' last' : '';        
     echo '<div class="'.$class.'"></div>';
     $i++;
  }

This is how Bootstrap does it

.table-striped tbody tr:nth-child(odd) {  
    background-color: rgba(0, 0, 0, 0.05);
} 

So you can create your own class and replace odd with 4n

.table-striped-4th tbody tr:nth-child(4n) {  
    background-color: rgba(0, 0, 0, 0.05);
} 

https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

发布评论

评论列表(0)

  1. 暂无评论