<?php

#---------------------------------------------#
#                            初期設定
#---------------------------------------------#

//共通任意のplugin
require_once(plug_dir."plug-selectarea.php");

if(!$table) $table=5;

#---------------------------------------------#
#                            処理実行
#---------------------------------------------#

$query="select ".$CONT_QUERY_CELL." from ".$CONT_TABLE." where ".$CONT_QUERY_WHERE.$CONT_QUERY_KEY;
$DATALOG=loadsqldata($query,"","");


#---------------------------------------------#
#                           メイン処理
#---------------------------------------------#

//CSS
$cs="banner.css";
if(!$PAGE_css && file_exists(css_dir.$cs)) $PAGE_css=css_full.$cs;
if($PAGE_css) $PRINT.="<link rel='stylesheet' href='".$PAGE_css."' type='text/css' media='all'>";

$PRT=array();
$ASU=$MAX=$KQ=array();
foreach($DATALOG as $value){
	$karea=explode(",",$value["area_p"]);$kgenre=explode(",",$value["genre_p"]);
	foreach($karea as $area){
		if(!$area) continue;
		$flag=0;foreach($PRINTAREA as $toshi => $ARE) if(in_array($area,$ARE)){ $flag=1;break; };
		if(!$flag) continue;
		$wtab=$value["data1_ct"]+1;
		//$wtab=$value["data1_ct"];
		if(!$wtab) continue;
		foreach($kgenre as $genre){
			if(!$genre) continue;
			$max=$MAX[$toshi][$genre];if(!$max) $max=0;
			$q=$KQ[$toshi][$genre];if(!$q) $q=0;

			if(($table-$max)<$wtab){
				list($print,$q)=waku_disp($q);
				$PRT[$toshi][$genre].=$print;
				$PRT[$toshi][$genre].="</tr><tr align='center' class='banner' valign='top'>\n";
				$max=0;
			}

			//バナーファイル名
			$INO=($value["id_p"]+0);
			$PRT[$toshi][$genre].="<td width=".floor(100/$table*$wtab)."% colspan='".$wtab."'>\n";
			$org_file="ban_".$INO."-".$wtab;

			//バナー呼び出し
			$ban_w=$SET_sc['pho'];
			$http_file=BannerImg($org_file,$value,$value["name"],$ban_w[$value["data1_ct"]]);
			if($http_file) $PRT[$toshi][$genre].=$http_file;
			$PRT[$toshi][$genre].="</td>\n";
			$q+=$wtab;$max+=$wtab;

			$MAX[$toshi][$genre]=$max;$KQ[$toshi][$genre]=$q;
			$ASU[$toshi][$genre]=1;
		}
	}
};

$t=0;
foreach($PRINTAREA as $toshi => $area){
	foreach($PRINTGEN as $genre){
		if(!$ASU[$toshi][$genre]) continue;
		$PRINT.="<div class='list-title'><a name='ban".$t."'></a>★".$toshi."/".$genre."</div>";
		$PRINT.="<table border='0' width=100%><tr align='center' class='banner' valign='top'>\n";
		$PRINT.=$PRT[$toshi][$genre];
		$max=$MAX[$toshi][$genre];if(!$max) $max=0;
		$q=$KQ[$toshi][$genre];if(!$q) $q=0;
		if(($table-$max)>0){
			list($print,$q)=waku_disp($q);
			$PRINT.=$print;
		};
		$PRINT.="</tr></table><br>\n";
		$t++;
	}
}


//余剰枠表示
function  waku_disp($q) 
{
	global $table,$BANNER_DEFO;
	if(!$BANNER_DEFO) $BANNER_DEFO=SPACER;

	$TEMP="";
	$t = $q % $table;
	for($p =$q+1; $t; $p++){
		$t = $p % $table; 
		$TEMP.="<td width=".floor(100/$table)."%><img src='".$BANNER_DEFO."' width='135' height='45' border='0'></td>\n";
	}
	$print=array($TEMP,($p-1));

	return $print;
}

?>