V
V
Valeri Lavrov2015-09-26 00:53:48
PHP
Valeri Lavrov, 2015-09-26 00:53:48

How to make alternating colors of rows in a table when calling data from a db?

Using PHP, I retrieve data from the database like this:

<?PHP
  
  $db->Query("SELECT * FROM db_spisok WHERE user_id = '$usid' ORDER BY id DESC LIMIT 20");  
  if($db->NumRows() > 0){ 
  		while($user = $db->FetchArray()){
    if ($user["start"] == 1) { 
    $yn= '<font color="#1b7b12"><b>Да</b></font>'; 
    } else { 
    $yn= '<font color="#ba0000"><b>Нет</b></font>'; 
    }
    $date = date('d.m.Y', $add["date"]);
    ?>
    <tr class="htt">
    		<td align="center"><?=$user["start"] ?> </td>
      <td align="center"><?=$date; ?></td>
      <td align="center"><?=$yn; ?></td>    		
  		</tr>
    <?PHP		
    }  
  }else echo '<tr><td align="center">Нет записей</td></tr>'
  
  ?>

It should be something like this:
1st line of black color
2nd line of white color
3rd line of black color
4th line of white color
Etc.
Because I need to display only 20 lines, I came up with the idea to assign my own color to even ones, to odd ones, but maybe there is a simpler way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2015-09-26
@Ablovka

htmlbook.ru/css/nth-child

P
Phizio, 2015-09-26
@Phyzio

I use the Datatable plugin in the output interface, very convenient tables are obtained at the output. And the "chess" that you want will be there. In addition to pagination, search, sorting - even export to XLS / CSV can be done on the client side (via a SWF gadget). Definitely handy, I recommend.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question