奇数和偶数行的表 [英] Odd and Even Rows for a table

查看:133
本文介绍了奇数和偶数行的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从MYSQL数据库获取行的表

 < table id =table1> 
<?php
//连接数据库服务器
mysql_connect(localhost,root,asnaeb)或die(mysql_error());
//选择数据库
mysql_query(SET NAMES`utf8`); // UTF 8支持!
mysql_select_db(scores)或die(mysql_error());
// SQL查询
$ strSQL =SELECT * FROM latest;
//执行查询(记录集$ rs包含结果)
$ rs = mysql_query($ strSQL);
//循环记录集$ rs
//使用mysql_fetch_array
将每行记录到一个数组($ row)中($ row = mysql_fetch_array($ rs)){
//写入列FirstName的值(它现在在数组$ row中)
?>

<?php echo $ row ['Header']。; ?>
< tr>
< td id ='date'><?php echo $ row ['Date']。; ?>< / TD>
< td id ='time'><?php echo $ row ['Time']。; ?>< / TD>
< td id ='hometeam'><?php echo $ row ['HomeTeam']。; ?>< / TD>
< td id ='score'><?php echo $ row ['Score']。; ?>< / TD>
< td id ='awayteam'><?php echo $ row ['AwayTeam']。; ?>< / TD>
< td id ='other'><?php echo $ row ['Other']。; ?>< / TD>
< / tr>

<?php} mysql_close(); ?>
< / table>

我有2个css类,称为奇数行和偶数行的A和B p>

我目前通过用< tr class ='替换< tr> <?php echo $ row ['Row']。; ?>'> ,我在我的数据库表中有一列行,我在A或B中添加偶数行或奇数行...问题是如果我想删除或添加新行之间我将不得不改变所有的A和B在其他。



我在另一个问题中看到很多方式来做到这一点在JavaScript或jQuery但对于一个正常的TR表,这不是我的情况...(尝试了一些这些脚本,但无法修复)

所以我想要更容易方法来做这偶和奇数行,谢谢!

解决方案

您可以轻松地使用jQuery添加这些类


$ b $

  $(function(){
$('#table1 tr:odd')。addClass('A');

//甚至是

$('#table1 tr:even')。addClass('B');

});


I have a table that get its rows from a MYSQL database

<table id="table1">
<?php
// Connect to database server
    mysql_connect("localhost", "root", "asnaeb") or die (mysql_error ());
// Select database
        mysql_query("SET NAMES `utf8`"); // UTF 8 support!!
    mysql_select_db("scores") or die(mysql_error());
// SQL query
    $strSQL = "SELECT * FROM latest";
// Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) { 
// Write the value of the column FirstName (which is now in the array $row) 
?>

<?php echo $row['Header'].""; ?>
<tr>
<td id='date'><?php echo $row['Date'].""; ?></td>
<td id='time'><?php echo $row['Time'].""; ?></td>
<td id='hometeam'><?php echo $row['HomeTeam'].""; ?></td>
<td id='score'><?php echo $row['Score'].""; ?></td>
<td id='awayteam'><?php echo $row['AwayTeam'].""; ?></td>
<td id='other'><?php echo $row['Other'].""; ?></td>
</tr>

<?php } mysql_close(); ?>
</table>

i have 2 css class called "A" and "B" for Odd Rows and Even Rows

i currently getting this done by replacing <tr> with <tr class='<?php echo $row['Row'].""; ?>'> and i have in my Database table a column "Row" which i add in A or B for even or odd row... the problem is if i wanna delete or add a new row between one of these i will have to change all the A and B in the other.

I have seen in another questions many way to do that in javascript or jquery but for a normal table with TR's which is not my case...(tried some of these scripts but couldn't get it fixed)

So what i want an easier way to do that Even and Odd rows, Thanks!

解决方案

You can add those classes using jQuery easily like this

$(function(){
  $('#table1 tr:odd').addClass('A');

 // for even

 $('#table1 tr:even').addClass('B');

});

这篇关于奇数和偶数行的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆