The database stores data (unique identifier, value, version).

The table has the following form:

CREATE TABLE `data` ( `ident` varchar(32) NOT NULL, `value` varchar(255) NOT NULL, `version` int(10) unsigned NOT NULL, UNIQUE KEY `ident` (`ident`) ) ENGINE=MyISAM DEFAULT CHARSET=cp1251 

At the entrance of the script in GET comes a request in the following format:

 ident[0]=<IDENT_0>&value[0]=<VALUE_0>&version[0]=<VERSION_0>&.... &ident[N]=<IDENT_N>&value[N]=<VALUE_N>&version[N]=<VERSION_N> 

It is believed that actual data is always stored in the database. The script should process the data received at the input and return the serialized array with 3 keys at the output:

  1. delete - list of identifiers that came in the request and are not in the database
  2. update - list of values ​​and versions by identifiers, where the version in the database has become more than the version that came in the request
  3. new - a list of values ​​and versions by identifiers that are missing in the incoming request, but are in the database

An example of the structure of the array that can be obtained at the output of the script:

 array ( 'delete' => array ( 0 => 'ident1', 1 => 'iden3', ), 'update' => array ( 'ident2' => array ( 'value' => 'some value 1', 'version' => 56, ), ), 'new' => array ( 'ident4' => array ( 'value' => 'some value 44', 'version' => 1, ), 'ident5' => array ( 'value' => 'some value 567', 'version' => 2, ), ) ) 

Here is my script.

 <?php $db = mysql_connect("localhost", "root", ""); mysql_select_db( "test", $db ); mysql_query('SET NAMES utf8'); $sql_request = 'SELECT * FROM `data`'; $result = mysql_query( trim( $sql_request ) ); $update = array(); $delete = array(); $new = array(); while( $row = mysql_fetch_assoc( $result ) ) { if ( in_array( $row['ident'], $_GET['ident'] ) ) { $tmp_position = array_search( $row['ident'], $_GET['ident'] ); // Если есть такой идентификатор, проверяем версию "больше" if ( $row['version'] > $_GET['version'][ $tmp_position ] ) { $update[ $row['ident'] ] = array( 'value' => $row['value'], 'version' => $row['version'], ); } unset( $_GET['ident'][$tmp_position] ); unset( $_GET['value'][$tmp_position] ); unset( $_GET['version'][$tmp_position] ); } else { $new[ $row['ident'] ] = array( 'value' => $row['value'], 'version' => $row['version'], ); } } $delete = array(); if ( !empty( $_GET ) ) { foreach( $_GET['ident'] as $key => $line ) { $delete[] = $_GET['ident'][$key]; } } $all_result = array( 'delete' => $delete, 'update' => $update, 'new' => $new, ); print_r( $all_result ); 

RESULT:

 Array ( [delete] => Array ( [0] => [1] => ) [update] => Array ( ) [new] => Array ( [ident2] => Array ( [value] => some value 1 [version] => 56 ) [ident4] => Array ( [value] => some value 44 [version] => 1 ) [ident5] => Array ( [value] => some value 567 [version] => 2 ) ) ) 

Thank.

  • And what is the inaccuracy? Array delete? - ArchDemon pm
  • delete and update are not displayed. - Max

0