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

$t1_name		 	= trim($_POST['t1_name']);
$t1_match_col		= trim($_POST['t1_match_col']);

$year_from_col		= trim($_POST['year_from']);
$year_to_col		= trim($_POST['year_to']);


echo "<br />'SPAN YEARS' STARTED<br />";
$t2_name = '1_MOD_YEARS_'.date('dmy');

$coldefinitions = array("id"=>"int auto_increment", "item"=>"varchar(32)", "year"=>"int");
createTable($t2_name, $coldefinitions);

$res = $DB->execute("select * from `{$t1_name}` order by id ASC");
if(!is_null($res)){
	$res = $res[0];
	while($W = $DB->nextRow($res)){
		$item = $W[$t1_match_col];
		$yearfrom = $W[$year_from_col];
		$yearto = $W[$year_to_col];

		for($y=$yearfrom; $y<=$yearto; $y++){
			if($DB->simpleQuery("insert into `{$t2_name}` (item, year) values ('{$item}','{$y}')")){
				echo "<br />Done at #{$item} - {$y}";
			}else{
				echo "<br />Error at #{$item} - {$y}";
			}
		}
	}
	echo "<br />ENDED<br />";
}

function createTable($tblname, $coldefinitions){
	global $DB;	$sqlpart = "(";
	foreach($coldefinitions as $col => $definition){ $sqlpart .= "`{$col}` {$definition},"; }
	$sqlpart .= "PRIMARY KEY(id)) ENGINE=InnoDB";
	$DB->silentExecute("DROP TABLE IF EXISTS {$tblname}");
	if($DB->silentExecute("CREATE TABLE {$tblname} {$sqlpart}")) return true;
	else return false;
}

?>