本文介绍了PHP的while循环,如果要为第一行做,否则为第二行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自mysql的while循环,我尝试为第一个结果做一些事情,如果其他情况为第二个结果做其他事情…….

I have while loop from mysql , i try to do some thing for first result under if else do other thing for second result etc....

我的代码是

$i = 1;
while($row = mysql_fetch_array($get_row)) {
$rowid = $row['rowid'];
$number = $row['number'];
$result = $number - 1;
if ($result != '0') {
// for only first result != 0 do this
require('result.php');
$i++;
// for all other results != 0 too do this
$edit_row = "UPDATE rows SET status = 'Ok' WHERE rowid = '$rowid'";
mysql_query($edit_row);
} elseif ($result == '0') {
$edit_row = "UPDATE rows SET status = 'Not Ok' WHERE rowid = '$rowid'";
mysql_query($edit_row);
} 
}

推荐答案

只需增加$i就可以使用它.当$i == 1时对第一行执行,否则执行其他操作.示例:

Simply can use $i by increasing it. Do for first row when $i == 1 else do other. Example:

$i = 1;
while($row = mysql_fetch_array($get_row)) {
    $rowid = $row['rowid'];
    if ($i == 1) { //Do for first row when $i == 0
        $i++; //Increase the value of $i
        require('result.php');
        $edit_row = "UPDATE rows SET status = 'Ok' WHERE rowid = '$rowid'";
        mysql_query($edit_row);
    } else { //Do other when $i > 1
        $edit_row = "UPDATE rows SET status = 'Not Ok' WHERE rowid = '$rowid'";
        mysql_query($edit_row);
    }
}

还建议您使用 mysqli PDO 代替mysql.

这篇关于PHP的while循环,如果要为第一行做,否则为第二行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:23