<?php
    include ('includes/db1config.php');
    include ('../includes/mysql_connect.php');
	include ('includes/admin-config.php');	//get site options	
	
?>
	
	<h3>Products</h3>
	<style>
		table td { border:solid 1px #ccc; font:normal 11px arial; padding:3px; text-align:left; vertical-align:top; }
	</style>
	<table style="border-collapse:collapse; border: solid 1px #ccc;">
	<?	
		$result = mysql_query("select * from products order by itemKey asc");
		$firstrow = true;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if ($firstrow) {
				$firstrow = false;
				echo "<tr>";
				foreach ($row as $colname => $value) {
					echo "<td>";
					echo $colname;
					echo "</td>";
				}
				echo "</tr>";
			}
			echo "<tr>";
			foreach ($row as $colname => $value) {
				echo "<td>";
				echo $value;
				echo "</td>";
			}
			echo "</tr>";
		}
	?>
	</table>
	
	<h3>Product Categories<h3>
	<table style="border-collapse:collapse; border: solid 1px #ccc;">
	<?	
		$result = mysql_query("select * from productCategory");
		$firstrow = true;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if ($firstrow) {
				$firstrow = false;
				echo "<tr>";
				foreach ($row as $colname => $value) {
					echo "<td>";
					echo $colname;
					echo "</td>";
				}
				echo "</tr>";
			}
			echo "<tr>";
			foreach ($row as $colname => $value) {
				echo "<td>";
				echo $value;
				echo "</td>";
			}
			echo "</tr>";
		}
	?>
	</table>
	
<table>
<?php
	// Returns map of categoryKey=>[ images ]
	function get_images_by_category() {
		$result = mysql_query("
				select 
					categoryKey, 
					imagePath, 
					thumbnail_path 
				from products 
				where imagePath <> '' and thumbnail_path <> ''");
		$images_by_category = array();
		if ($result) {
			while ($arr = mysql_fetch_array($result, MYSQL_ASSOC)) {
				if (!array_key_exists($arr[categoryKey], $images_by_category)) {
					$images_by_category[$arr[categoryKey]] = array();
				}
				$imagedata = array(
					"normal" => $arr[imagePath],
					"thumb" => $arr[thumbnail_path]
				);
				array_push($images_by_category[$arr[categoryKey]], $imagedata);
			}
		}
		return $images_by_category;
	}
	var_dump(get_images_by_category());	
	
	$result = mysql_query("select itemKey, categoryKey, imagePath, thumbnail_path from products where imagePath <> ''");
	if ($result) {
		$firstrow = true;
		while ($arr = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if ($firstrow) {
				echo "<tr>";
				$firstrow = false;
					foreach ($arr as $col => $val) {
					?> <th><?php echo $col; ?></th> <?php
				}
				echo "</tr>";
			}
			echo "<tr>";
			foreach ($arr as $col => $val) {
				?>
				<td><?php echo $val ?></td>
				<?php
			}
			echo "</tr>";
		}
	}
?>
</table>
<?php
	
// Change parent category
	$parent_cat_query = "SELECT 
								c.recordKey, 
								c.categoryName, 
								c.category_type, 
								c.parent_cat_num, 
								p.categoryName as parent_categoryName
						  FROM productCategory c
						  LEFT OUTER JOIN productCategory p
							   on p.recordKey = c.parent_cat_num
						  ORDER BY p.categoryName DESC
						  ";
    $parent_cat_result = mysql_query($parent_cat_query);
    $parent_cat_map = array();
    $cat_names_map = array();
    if ($parent_cat_result) {
    	while ($myarr = mysql_fetch_array($parent_cat_result)) {
    		$parent_cat_map[$myarr[recordKey]] = $myarr[parent_cat_num];
    		$cat_names_map[$myarr[recordKey]] = $myarr[categoryName];
    	}
    }
    
    function get_category_ancestry_label($num, $parent_cat_map, $cat_names_map) {
    	if ($num == "0") {
    		return "";
    	}
    	if (!array_key_exists($num, $cat_names_map)) {
    		return "";
    	}
    	$str = $cat_names_map[$num];
    	if (array_key_exists($num, $parent_cat_map)) {
    		$str = get_category_ancestry_label($parent_cat_map[$num], 
    										   $parent_cat_map, 
    										   $cat_names_map) . "-" . $str; 
    	}
    	return trim($str, "-");
    }
    
    foreach ($parent_cat_map as $k => $v) {
    	echo "<div>" . get_category_ancestry_label($k, $parent_cat_map, $cat_names_map) . " - ".$k." - ".$v."</div>";
    }
// end

	$result = mysql_query("select * from productCategory order by recordKey asc");
	
	if ($result) {
		while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
			foreach ($row as $i => $v) {
				echo " " . $v . ",";
			}
			echo "<br />";
		}
	}
	
	$testdata = array();
	
	while ($tmp = $testdata["bunk"]) {
		echo "No!";
		break;
	}

	
	// query product catalogs
	// $query = "SELECT 
	// 				c.recordKey, 
	// 				c.categoryName, 
	// 				c.category_type, 
	// 				c.parent_cat_num, 
	// 				p.categoryName as parent_categoryName
	// 		  FROM productCategory c
	// 		  LEFT OUTER JOIN productCategory p
	// 			   on p.recordKey = c.parent_cat_num
	// 		  ORDER BY p.categoryName DESC
	// 		  ";
	// $result = mysql_query($query);
	// if ($result) {
	// 	echo mysql_error();
		
	// 	while ($row = mysql_fetch_array($result)) {
	// 		echo "<div>".$row[recordKey]." - ".$row[parent_categoryName]." - ".$row[categoryName]."</div>";
	// 	}
	// } else {
	// 	echo "Error<br/>";
	// 	echo mysql_error();
	// }
?>