Php 更改while循环中最后两行的类
我有一个while循环,它显示MySQL查询的结果。我知道如何更改最后一行或奇数行的输出,但如何更改最后两个结果的输出Php 更改while循环中最后两行的类,php,mysql,while-loop,Php,Mysql,While Loop,我有一个while循环,它显示MySQL查询的结果。我知道如何更改最后一行或奇数行的输出,但如何更改最后两个结果的输出 例如,我有一个2x2矩阵中的结果列表,每个结果底部有边框:1px,但我想显示没有边框的底部两个?如果可以使用css3,这很简单(我将使用列表作为示例): 选择最后两个li 如果您想在php中执行此操作,可以计算结果的数量,在循环中添加计数器,并在最后两项中添加一个类。我喜欢CSS方式,但您需要知道要列出的项的总数。然后您可以使用一个简单的条件来检查它 $total_items
例如,我有一个2x2矩阵中的结果列表,每个结果底部有边框:1px,但我想显示没有边框的底部两个?如果可以使用css3,这很简单(我将使用列表作为示例): 选择最后两个
li
如果您想在php中执行此操作,可以计算结果的数量,在循环中添加计数器,并在最后两项中添加一个类。我喜欢CSS方式,但您需要知道要列出的项的总数。然后您可以使用一个简单的条件来检查它
$total_items = [count of items];
$cnt = 0;
while($fetch as $row) {
...
if(++$cnt > ($total_items - 2)) {
// list with no border
} else {
// list with border
}
}
是的,就这样做:
$result = //execute your query
$num_rows = mysql_num_rows($result;
$num_rows_different = 2;
$loop_counter = 0;
while ($loop_counter < $num_rows) {
if ($loop_counter < $num_rows - $num_rows_different) {
// with border
} else {
// no border
}
$loop_counter++;
}
$result=//执行查询
$num\u rows=mysql\u num\u rows($result;
$num_rows_different=2;
$loop_计数器=0;
while($loop\u counter<$num\u行){
if($loop\u counter<$num\u rows-$num\u rows\u different){
//有边界
}否则{
//无边界
}
$loop_计数器++;
}
我不会使用CSS3方法,因为它的支持很差…你可以使用CSS,也许还可以保存一个类。你应该能够用jQuery实现类似的解决方案,不是吗?这个选择器似乎可以工作。@Derelease我还没有试过,但我会用它来处理那些本机不支持它的浏览器。
$result = //execute your query
$num_rows = mysql_num_rows($result;
$num_rows_different = 2;
$loop_counter = 0;
while ($loop_counter < $num_rows) {
if ($loop_counter < $num_rows - $num_rows_different) {
// with border
} else {
// no border
}
$loop_counter++;
}