<?php


///////////////////////////////////////////////////////////////////////////////////////////////////
// LOOP RECORDS AND ADD NEW VALUES ////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

// LOOP RECORDS ///////////////////////////////////////////////////////////////////////////////////

// results count
$recordsCountSQL="SELECT COUNT(*) FROM `$reportTable`";
$recordsCount=$db->GetOne($recordsCountSQL);
if ($db->ErrorNo()) killReport("Could not run SQL in report. SQL: ".$recordsCountSQL);

// the size of the bulk group records
$groupSize=4000;

for($startGroup=0;$startGroup<$recordsCount;$startGroup+=$groupSize) {

	refreshReportStatus('loopRecords', 'updateRecords', $startGroup/$recordsCount*100);

	// get the bulk data
	$listResultsSQL="SELECT * FROM `$reportTable` LIMIT $startGroup, $groupSize";
	$listResults=$db->GetAll($listResultsSQL);
	if ($db->ErrorNo()) killReport("Could not run SQL in report. SQL: ".$listResultsSQL);

	// LOOP THE RECORDS IN THIS GROUP /////////////////////////////////////////////////////////////
	foreach($listResults as $row) {
		$updateFieldsArr= [];

		// extraData
		if ($tableColumns['extraDataList'] && $row['extraDataList']){
			$thisExtraDataStr="";
			$thisExtraData=unserialize($row['extraDataList']);
			foreach($thisExtraData as $name=>$value){
				$thisExtraDataStr.=htmlClean($name).": ".htmlClean($value)."&#10;";
			}

			$updateFieldsArr[]="`extraDataList`='".$thisExtraDataStr."'";
		}

		// update report
		if ($updateFieldsArr) {
			$updateFieldsSQL="UPDATE `$reportTable` SET ".implode(', ', $updateFieldsArr)." WHERE id='".$row['id']."'";
			$updateFieldsEx=$db->Execute($updateFieldsSQL);
			if ($db->ErrorNo()) killReport("Could not run SQL in report. SQL: ".$updateFieldsSQL);
		}

	}

}

unset($listResults);

?>