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

$table		= trim($_POST['table']);
$focuscol		= trim($_POST['focuscolumn']);
$updatecol		= trim($_POST['updatecolumn']);
$reference	= explode('-',trim($_POST['reference']));

if($table!="" && $focuscol!="" && $updatecol!="" && $reference!=""){
	$increment = -1;
	echo "<br />'REFERENCE MASTER' STARTED<br />";
	$res = $DB->execute("select * from `{$table}` group by `{$focuscol}` order by `{$focuscol}`");
	if(!is_null($res)){
		$res = $res[0];
		while($W = $DB->nextRow($res)){
			$colv = $W[$focuscol];
			$increment++;
			$ref = $reference[0]."-".( ((int) $reference[1]) + $increment );
			if($DB->simpleQuery("update `{$table}` set `{$updatecol}`='{$ref}'")){
				echo "<br />Done for #{$ref}";
			}else{
				echo "<br />Error for #{$ref}";
			}
		}
		echo "<br />ENDED<br />";
	}
}else{
	echo "<br />SIZE MISMATCH ERROR!<br />Please check<br />";
}

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;
}
?>