<?php
include_once("../comm/db_sql.php");
include "../CheckUser.php";
include_once("../comm/config.php");
include_once("../comm/connect.php");
include_once("../comm/Function.php");
include_once("../down/Function.php");

//返回总行
function info_query_rows($db)
	{
				$info_query=new mysql_db();
				$info_query->table=$db."channel"; 
				$info_query->table_mod="5"; 
				$info_query->table_where="channel=2";
				$info_query->limit="0";       
				$info_query->page_size="2000000";
				$info_query->query_object="ID"; 
				$info_query->order_by="ID asc";
				$info_query->query();
				$rows=$info_query->rows;
				return $rows;
				$info_query=NULL;
	}
	$rows=info_query_rows($db);	
	echo $rows."页";

//返回当前频道行ID
function info_query_rows_id($s,$db)
	{
				$info_query=new mysql_db();
				$info_query->table=$db."channel"; 
				$info_query->table_mod="5"; 
				$info_query->table_where="channel=2";
				$info_query->limit=$s;       
				$info_query->page_size="1";
				$info_query->query_object="*"; 
				$info_query->order_by="Id asc";
				$info_query->query();
				$info_query->query_result();
				return $info_query->record['Id'];
				$info_query=NULL;
	}
//取得当前频道大类的总数
function category_query_rows_count($channel,$db)
	{
				$info_query=new mysql_db();
				$info_query->table=$db."download_category"; 
				$info_query->table_mod="5"; 
				$info_query->table_where="channel=$channel";
				$info_query->limit="0";       
				$info_query->page_size="2000000";
				$info_query->query_object="ID"; 
				$info_query->order_by="ID asc";
				$info_query->query();
				$rows=$info_query->rows;
				return $rows;
				$info_query=NULL;
	}
//取得当前大类行ID
function category_query_rows_id($s,$db,$channel)
	{
				$info_query=new mysql_db();
				$info_query->table=$db."download_category"; 
				$info_query->table_mod="5"; 
				$info_query->table_where="channel=$channel";
				$info_query->limit=$s;       
				$info_query->page_size="1";
				$info_query->query_object="*"; 
				$info_query->order_by="Id asc";
				$info_query->query();
				$info_query->query_result();
				return $info_query->record['Id'];
				$info_query=NULL;
	}
//取得当前大类的记录数
function category_page_channel($db,$page,$id)
	{
			$info_query=new mysql_db();
			$info_query->table=$db."download_info";  
			$info_query->table_where="big_cate_id=$id"; 
			$info_query->page_size="36";  
			$info_query->table_mod="6";   
			$info_query->page=$page;
			$info_query->query();
			
			$prepage=$info_query->prepage;
			$nextpage=$info_query->nextpage;
			$pages=$info_query->pages;
			$rows_count=$info_query->rows_count;
			return $pages;
		}

function create_html($page,$channel,$id)
	{      
	        $f_p = "../../download/category_".$page."_".$channel."_".$id.".html";
			
			$url='';
			if ($_SERVER["SERVER_PORT"]!="80")
			{
				$url=$url.'http://'.$_SERVER['SERVER_NAME'].":".$_SERVER["SERVER_PORT"];
			}
			else
			{
				$url=$url.'http://'.$_SERVER['SERVER_NAME'];
			}				
			$url = $url."/download/category.php?page=".$page."&channel=".$channel."&id=".$id;
			$contents = file_get_contents($url);
			
			$hdl = fopen ($f_p,"w");
			if(!is_writable ($f_p)){
        		echo "文件没有写的权限";
			}
			if (!fwrite ($hdl,$contents)){ 
        		echo "很抱歉！写入文件失败";  
			}
			fclose ($hdl);
	}


//返回各频道ID
function info_query_ID($rows,$db)
	{
				for($i=0;$i<$rows;$i++)
					{
						$s=$i;
						$channel=info_query_rows_id($s,$db); //返回各频道ID
						echo $channel."频道<BR>";
						$category_rows=category_query_rows_count($channel,$db); //取行大类行
						echo $category_rows."个大类<BR>";
							for($c=0;$c<$category_rows;$c++)
							{ 
									$category_id=category_query_rows_id($c,$db,$channel);
									echo $category_id."大类ID<BR>";
									//create_html($page,$channel)；
									$page=1;
									$allpages=category_page_channel($db,$page,$category_id);
									if($allpages == 0)
									{
										$allpages = 1;	
									}
									echo "当前大类共有".$allpages."页<Br>";
									for($p=1;$p<=$allpages;$p++)
									{
										if($p <> "" and $channel <>"" and $category_id<>"")
										{
											create_html($p,$channel,$category_id);
										}
									}
							}

						
					}
	}
	
info_query_ID($rows,$db);
?>