简体   繁体   English

通过值排序php mysql

[英]Sorting through values php mysql

I have a database with rows containing an ID and a value. 我有一个包含ID和值的行的数据库。

I randomly insert these values between the numbers 1 and 100 as I insert records, so an ID has a random value. 我在插入记录时将这些值随机插入数字1和100之间,因此ID具有随机值。 I use a while loop to go through the records and print them to screen. 我使用while循环浏览记录并将其打印到屏幕上。 However, the way I want it is if the current value of the row in the while loop is LESS than the next value, then break out of the loop. 但是,我想要的方式是,如果while循环中该行的当前值比下一个值少,那么请退出循环。 Basically what I am asking is how to refer to the next record of a database while in a while loop working with the current, simply to check a value in it. 基本上,我要问的是在使用while循环时,如何引用数据库的下一条记录,而仅仅是检查其中的值。

Try this: 尝试这个:

$previousRow = mysql_fetch_assoc($result);
$previousRow['ID'] = -1;

while($currentRow = mysql_fetch_assoc($result))
{
    if($currentRow['ID'] > $previousRow['ID'])
         break;

    echo $previousRow['Value'];
    $previousRow = $currentRow;
}
$lastRecord = Array();
foreach($records as $thisRecord){
    if($lastRecord['value'] < $thisRecord['value']){
        // do what you need to do here
    }
    $lastRecord = $thisRecord;
}

This way you always keep the previous value stored 这样,您将始终保留先前的值

Do it the other way round. 反之亦然。 Instead of breaking out based on the next element, just check on the next element based on the previous element (which you can simply store somewhere). 无需根据下一个元素进行分解,只需根据上一个元素(您可以将其存储在某个位置)检查下一个元素即可。 I guess that the loop actually loops through the elements and does something; 我猜想循环实际上会遍历所有元素并执行某些操作。 you can then just check the previous/current element first before doing anything and nobody will notice that you in fact loaded the next element. 那么你可以查看以前的/当前元素第一做任何事情之前,没有人会注意到你其实装的下一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM