<?php require_once("../classes/DBConnection.php"); $DB = new DBConnection();

$t1_name		 	= trim($_POST['t1_name']);
$t1_find			= trim($_POST['t1_find']);
$t1_replace			= trim($_POST['t1_replace']);

$t2_name			= trim($_POST['t2_name']);
$t2_find			= trim($_POST['t2_find']);
$t2_replace			= trim($_POST['t2_replace']);

$t2_sequencial		= trim($_POST['t2_sequencial']);

echo "<style>html,body{ color:#FFFFFF; font-family:monospace; font-size:15px; }</style>";

if(true){
	echo "<br />'PICK AND REPLACE' STARTED<br />";
	//get unique finds from table 1
	$res = $DB->execute("select `{$t1_find}` from `{$t1_name}` group by `{$t1_find}`");
	if(!is_null($res)){
		$unique_finds = $res[0];
		while($W = $DB->nextRow($unique_finds)){
			$unique_find = $W[$t1_find];

			//get replaces for each unique_find, and continue
			$res2 = $DB->execute("select `{$t1_replace}` from `{$t1_name}` where `{$t1_find}` = '{$unique_find}' order by `{$t1_replace}` ASC");
			if(!is_null($res2)){
				$unique_find_replaces = $res2[0];

				//before continuing, fetch matching sequentials in table 2 into temporary table
				$DB->simpleQuery("CREATE TABLE IF NOT EXISTS temp_uniques_19841010 (`{$t2_sequencial}` VARCHAR(64) NOT NULL)");
				$DB->simpleQuery("delete from temp_uniques_19841010");
				$DB->simpleQuery("insert into temp_uniques_19841010 (select `{$t2_sequencial}` from `{$t2_name}` where `{$t2_find}`='{$unique_find}' group by `{$t2_sequencial}`)");

				//fetch them back for loop updating
				$res3 = $DB->execute("select * from temp_uniques_19841010 order by `{$t2_sequencial}` ASC");
				if(!is_null($res3)){
					$sequencials = $res3[0];

					//update table 2 for each sequencial with a unique_find_replace
					while($W3 = $DB->nextRow($sequencials)){
						$sequencial = $W3[$t2_sequencial];
						$replacement = 'NO REPLACEMENT FOUND';
						if($W4 = $DB->nextRow($unique_find_replaces)){ $replacement = $W4[$t1_replace]; }

						//effect the update
						if($DB->simpleQuery("update `{$t2_name}` set `{$t2_replace}` = '{$replacement}' where `{$t2_find}` = '{$unique_find}' and `{$t2_sequencial}` = '{$sequencial}'")){
							echo "<br />Done for {$unique_find}";
						}else{
							echo "<br />Error for {$unique_find}";
						}
					}
				}
			}
		}
	}
	echo "<br />ENDED<br />";
}else{
	echo "<br />SIZE MISMATCH ERROR!<br />Please check<br />";
}

// function holePegStr($holes, $pegs){
// 	global $DB;
// 	$hpstr = "";
// 	for($i=0; $i<sizeof($holes); $i++){
// 		$hpstr .= ",`{$holes[$i]}`='".$DB->escape_string($pegs[$i])."'";
// 	}
// 	$hpstr = substr($hpstr,1);
// 	return $hpstr;
// }

// function andCondition($names, $values){
// 	global $DB;
// 	$cond = "";
// 	for($i=0; $i<sizeof($names); $i++){
// 		$cond .= " and `{$names[$i]}`='".$DB->escape_string($values[$i])."'";
// 	}
// 	$cond = substr($cond, 5);
// 	return $cond;
// }

// function toCSL($arr){
// 	global $DB;
// 	$ret = "";
// 	foreach($arr as $a){ $ret .= ",`".$DB->escape_string($a)."`"; }
// 	$ret = substr($ret,1);
// 	return $ret;
// }
?>