php while loop if to do for first row else to do for second row?

无人久伴 提交于 2019-12-12 03:34:11

问题


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....

my code this

$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);
} 
}

回答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);
    }
}

Also suggest you to use mysqli or PDO instead mysql.



来源:https://stackoverflow.com/questions/31623596/php-while-loop-if-to-do-for-first-row-else-to-do-for-second-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!