Solarian_TK Posted July 26, 2010 Share Posted July 26, 2010 Hey Folks! So, my brain is hurting now... I am making a little process to go through a table and if a row as one set of data, and the next row as the same data based on 3 fields then update a field in the current row. Here is a snippet of what I have been working on: <?php date_default_timezone_set('AUSTRALIA/BRISBANE'); $lasthour = (time() - 3600); $lastattack45 = (time() - 2820); $lasthalfhour = (time() - 1800); $lastfifteen = (time() - 900); $lastfhourhalf = (time() - 5400); $next15 = (time() + 900); $now = time(); mysql_connect($mysql_host, $mysql_user, $mysql_password) or die('Error connecting to mysql'); mysql_select_db($mysql_database); $result = mysql_query("SELECT feeder, attacker, defender, attacklandunix, smurfid, smurf, unixtime, record, leader FROM nation_incoming WHERE attacklandunix > $now && smurf <> 'smurfed' ") or die("SELECT Error: ".mysql_error()); while($row = mysql_fetch_array($result)){ $a[][feeder] = $row['feeder']; $a[][defender]= $row['defender']; $a[][landing]= $row['attacklandunix']; } foreach($a as $b){ //in here it is to compare the 'feeder' && 'defender' && 'attacklandunix' to the next row. IF the same combo exists, then update field 'leader' to "0". else update 'leader' to "1" } ?> How can/should I pull up the next row? Quote Link to comment https://forums.phpfreaks.com/topic/208884-help-with-getting-next-row-data-to-check-for-similar-data/ Share on other sites More sharing options...
vichu.1985 Posted July 28, 2010 Share Posted July 28, 2010 You should have selected your table's primary key field with $a[]['key'] = "primary key value" to update the leader field. Try with this $index = 1; foreach($a as $value){ if($a[$index]['feeder'] == $value['feeder']){ //update the "leader" field to 1 where key_field = "$a[]['key']" } else{ //update the "leader" field to 0 } $index++; } Quote Link to comment https://forums.phpfreaks.com/topic/208884-help-with-getting-next-row-data-to-check-for-similar-data/#findComment-1091960 Share on other sites More sharing options...
katierosy Posted August 2, 2010 Share Posted August 2, 2010 If you will use for loop instead of foreach it will make your tas easy. This should make it clear. <?php for($i=0;$i<count($result);$i++){ if($result[$i-1]['fieldname']=='value' && $result[$i]['fieldname']=='value'){ // update with the $i-1 index or $i index } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208884-help-with-getting-next-row-data-to-check-for-similar-data/#findComment-1094137 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.