<?php
	$srv = "localhost"; // サーバー名
	$id = "accounts"; // ユーザーID
	$passwd = "copernicus"; // パスワード
	$dbn = "accounts"; // データベース名

	// データベースに接続する
	$db = mysql_connect($srv, $id, $passwd);
	mysql_select_db($dbn, $db);

?>
<?php
	$sql = "SELECT catID, catName FROM category WHERE valid=1 ORDER BY catID ASC;"; // SQL文


	$res = mysql_query($sql, $db);
	
	$categoryArray = array();
	while($dat = mysql_fetch_array($res, MYSQL_ASSOC)){
		$categoryArray[] = $dat;
	}


	mysql_free_result($res);
	$numCategoryArray = count($categoryArray);
	for($i=0; $i<$numCategoryArray;  $i++)
	{
		$sql = "SELECT subcatID, subcatName FROM subcategory WHERE catID=\"".$categoryArray[$i]["catID"]."\" AND valid=\"1\" ORDER BY subcatID;";
		$res = mysql_query($sql, $db);

		$subCategoryArray = array();
	        while($dat = mysql_fetch_array($res, MYSQL_ASSOC)){
			$subCategoryArray[] = $dat;
		}
		mysql_free_result($res);

	        print "\t\t".$categoryArray[$i]["catID"].": [ ";
		
		$numSubCategoryArray = count($subCategoryArray);
		for($j=0; $j<$numSubCategoryArray; $j++)
		{
			print "{ subcatID: ".$subCategoryArray[$j]["subcatID"].", name: \"".$subCategoryArray[$j]["subcatName"]."\" }";

			if ($j < $numSubCategoryArray-1)
			{
				print ", ";
			}
		}

		print " ]";

		if ($i < $numCategoryArray-1)
		{
			print ",\n";
		}

	}

	foreach ($categoryArray as $dat)
	{
	    printf("\t<option value=\"%s\">%s</option>\n", $dat["catID"], $dat["catName"]);
	}

?>	
