<?php

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

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

if (!$table) $table = 5;

#---------------------------------------------#
#                            処理実行
#---------------------------------------------#
// debug-s kyoh Top banner
//$query = "select " . $CONT_QUERY_CELL . " from " . $CONT_TABLE . " where " . $CONT_QUERY_WHERE . $CONT_QUERY_KEY;
$sub_category = ',sub_category';     // 追加分
$shop_info = ',shop_info';
$query = "select " . $CONT_QUERY_CELL . $sub_category . $shop_info . " from " . $CONT_TABLE . " where " . $CONT_QUERY_WHERE . $CONT_QUERY_KEY;
// debug-e kyoh
$DATALOG = loadsqldata($query, "", "");
// debug-s kyoh
//var_dump($query);
//echo '<br>';
//echo '<br>';
//var_dump($DATALOG);
// debug-e kyoh

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

//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;
            // debug-s kyoh Top banner
            if ($value['ct_cp'] === 'fuzoku') {
                $ASU[$toshi][$genre] = 1;

                $PRT[$toshi][$genre] .= '<li>';
                $PRT[$toshi][$genre] .= '<div class="banner-list-name">';
                $PRT[$toshi][$genre] .= "<p>{$value['name']}</p>";    // 店名
                $PRT[$toshi][$genre] .= '</div>';

                $PRT[$toshi][$genre] .= '<div class="banner-list-info">';
                if ($genre === "デリバリーヘルス") {
                    $PRT[$toshi][$genre] .= '<p class="shop-category">' . $area . '/' . 'デリヘル' . ' ' . (($value['sub_category']) ? $value['sub_category'] : ' ') . '</p>'; // 都市とジャンル
                } else {
                    $PRT[$toshi][$genre] .= '<p class="shop-category">' . $area . '/' . $genre . ' ' . (($value['sub_category']) ? $value['sub_category'] : ' ') . '</p>'; // 都市とジャンル
                }
//                $PRT[$toshi][$genre] .= '<p class="shop-type">' . (($value['sub_category']) ? $value['sub_category'] : '○○系') . '</p>'; // サブカテゴリ
                $PRT[$toshi][$genre] .= '</div>';

                $PRT[$toshi][$genre] .= '<div class="banner-list-img">';
                // バナーリンク
                $org_file = "ban_" . ($value["id_p"] + 0) . "-" . $wtab;
                $http_file = BannerImg($org_file, $value, "", "");
                $PRT[$toshi][$genre] .= ($http_file) ? $http_file : "";
                $PRT[$toshi][$genre] .= '</div>';

                // ショップインフォメーション
                $PRT[$toshi][$genre] .= '<div class="banner-list-text">';
                $PRT[$toshi][$genre] .= '<p>' . (($value['shop_info']) ? $value['shop_info'] : ' ') . '</p>';
                $PRT[$toshi][$genre] .= '</div>';

                $PRT[$toshi][$genre] .= '</li>';
            } else {// if ($value['ct_cp'] === 'fuzoku') {
                // 九州風俗サイト以外は通常通りの表示
                $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;
            }
            // debug-e kyoh
        }
    }
};

$t = 0;
foreach ($PRINTAREA as $toshi => $area) {
    foreach ($PRINTGEN as $genre) {
        if (!$ASU[$toshi][$genre]) continue;
// edit 170708
//		$PRINT.="<div class='list-title'><a name='ban".$t."'></a>★".$toshi."/".$genre."</div>";
        // debug-s kyoh Top Banner
        if ($value['ct_cp'] === 'fuzoku') {
            if ($genre === "デリバリーヘルス") {
                $PRINT .= "<div class='pc-category-title'><a id='ban" . $t . "'  name='ban" . $t . "'></a><h1>" . $toshi . "/" . "デリヘル" . "</h1></div>";
            } else {
                $PRINT .= "<div class='pc-category-title'><a id='ban" . $t . "'  name='ban" . $t . "'></a><h1>" . $toshi . "/" . $genre . "</h1></div>";
            }
            $PRINT .= '<ul class="pc-banner-list">';
        }else{
            // 九州風俗サイト以外は通常通りの表示
            $PRINT.="<div class='list-title'><a id='ban".$t."'  name='ban".$t."'></a>★".$toshi."/".$genre."</div>";
            $PRINT.="<table border='0' width=100%><tr align='center' class='banner' valign='top'>\n";
        }
        // debug-e kyoh
        $PRINT .= $PRT[$toshi][$genre];

        // debug-s kyoh Top Banner
        if ($value['ct_cp'] === 'fuzoku') {
            $PRINT .= "</ul><br>";
        } else {// if ($value['ct_cp'] === 'fuzoku') {
            // 九州風俗サイト以外は通常通りの表示
            $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";
        }
        // debug-e kyoh
        $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;
}

?>
