Jump to content

Help with getting next row data to check for similar data


Solarian_TK

Recommended Posts

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?

 

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++;

}

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

 

}

 

      }   

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.