<?php
include_once('global.php');/*
更新全部新闻列表
从make_news_list.php复制代码过来,稍为更改
*/
//$fid=$_GET['fid'];
//$table=$db->selectTable($fid);
//建立一个函数来获取下拉列表中的页数值
function selectPage($allPage,$childclass)
{
$selectpage='';
$index2=bin2hex($childclass);
$url='';
for($p=1;$p<$allPage+1;$p++)
{
$url=$index2.$p.'.htm';
$selectpage.='<option value='.$url.'>'.$p.'</option>';
}
return $selectpage;
}
$count=$db->showCount('newsclass');
for($fid=1;$fid<$count+1;$fid++)
{
$table=$db->selectTable($fid);
//取得父类的名字
$sql_name="select `class` from newsclass where `fid`='$fid'";
$res_name=mysql_query($sql_name);
list($parentclass)=mysql_fetch_row($res_name);
$sql_class="select `class` from $table group by `class` order by id desc";
$res_class=mysql_query($sql_class);
$count_class=0;
$childclass=array();
$child_class='';
while($row_class=mysql_fetch_assoc($res_class))
{
$childclass[$count_class]=$row_class['class'];
$index=bin2hex($childclass[$count_class]);
//当前的子类为第一个时
if($count_class==0){$url='index.htm';}
//不是第一个时
else{$url=$index.'1.htm';}
$child_class.='<tr><td><a href='.$url.'>'.$childclass[$count_class].'</a></tr></td>';
$count_class++;
}
mysql_free_result($res_class);
/*三个循环
要点:从第一个循环开始,一层一层的去执行
循环太多,连我自己都难以理解了,反正已经实现自己想要的效果
*/
//echo $count_class;
//第一个循环,是通过子类的数目来进行循环
for($i=0;$i<$count_class;$i++)
{
/*下列语句得到子类中的新闻列表的总数*/
//echo $childclass[$i];
$sql_count="SELECT COUNT(*) FROM $table where `class`='$childclass[$i]'";
$res_count=mysql_query($sql_count);
list($recordCount)=mysql_fetch_row($res_count);
mysql_free_result($res_count);
/*通过总数来获取记录数和下标值等*/
$pagesize=15;
$allPage=ceil($recordCount/$pagesize);
//转化了十六进制,每个子类有着不同的名字,所以每个文件目录有着不同的列表索引
$index=bin2hex($childclass[$i]);
//第二个子循环是生成新闻列表,然后再进行模板替换
for($page=1;$page<$allPage+1;$page++)
{
$startIndex=($page-1)*$pagesize;
$sql="select * from $table where `class`='$childclass[$i]' order by id desc limit $startIndex,$pagesize";
$res=mysql_query($sql);
$titlelist='';
//对记录集进行保存
while($row=mysql_fetch_assoc($res))
{
$path=$row['remark'];
$replace=$table.'/';
$path=str_replace($replace,'',$path);
$title=$row['title'];
$titlelist.='<tr><td><a href='.$path.' target=_blank>'.$title.'</a></td></tr>';
}
//读取模板文件
//通过已经建立的方法来获取下拉列表中的页数
$selectPage=selectPage($allPage,$childclass[$i]);
$file=file_get_contents('templates/listTmp.htm');
$file=str_replace('{childclass}',$child_class,$file);
$file=str_replace('{titlelist}',$titlelist,$file);
$file=str_replace('{selectPage}',$selectPage,$file);
$file=str_replace('{allpage}',$allPage,$file);
$file=str_replace('{page}',$page,$file);
$file=str_replace('{parentclass}',$parentclass,$file);
$file=str_replace('{childname}',$childclass[$i],$file);
$previouspage=$index.($page-1).'.htm';
$nextpage=$index.($page+1).'.htm';
if ($page==1){$previouspage='?';}
if($page==2&&($i==0)){$previouspage='index.htm';}
if ($page==$allPage){$nextpage='?';}
$file=str_replace('{previouspage}',$previouspage,$file);
$file=str_replace('{nextpage}',$nextpage,$file);
$savepath='../admin/'.$table.'/'.$index.$page.'.htm';
//当前的列表是第一个子类,且当前页面为1时
if($page==1&&$i==0) {$savepath='../admin/'.$table.'/'.'index.htm';}
file_put_contents($savepath,$file);
}
}
}
?>