tz_com 2011-04-02 14:41:54 5259次浏览 7条回复 4 0 0

关于长内容分页,好多人都愁怎么弄,我把我写的代码拿出来让大家批评下 首先谢了一个截取的方法 我放在model里了

//$ptext = '<div style="page-break-after: always"><span style="display: none">&nbsp;</span></div>'   

分页符自己设置 我用的是FCKEditor的默认分页符

static function ContentsPages($url, $contents, $ptext = '<div style="page-break-after: always"><span style="display: none">&nbsp;</span></div>') {
    $arr = explode($ptext, $contents);        //数组长度&总页数
    $total = count($arr);        //当前页
    $nowpage = $_GET['page'] ? $_GET['page'] : 1;        //上页
    $prepage = $nowpage == 1 ? 1 : $nowpage - 1;        //下页
    $nextpage = $nowpage > $total - 1 ? $total : $nowpage + 1;        //最后一页
    $lastpage = $total;
    $pdiv = '<br /><div style=\"height:30px;width:500px;border:1px; blackground-color:#ff0000; \">';
    //$pdiv .= "第{$nowpage}页 / 总{$total}页&nbsp;&nbsp;";
    //首页链接
    $pdiv .= "<li style=\"display:inline;\"><a href=\"$url/1.html\" style=\"text-decoration:none;\">首页</a></li>";
    //上页链接
    $pdiv .= "<li style=\"display:inline;\"><a href=\"$url/$prepage.html\" style=\"text-decoration:none;\">&nbsp;&nbsp;上一页&nbsp;&nbsp;</a></li>";
    //显示分页列表
    $color = '';
    for ($i = 1; $i <= $total; $i++) {
        if ($i == $nowpage) {
            $color = "color:#ff0000;";
        } else {
            $color = '';
        }
        $pdiv .= "&nbsp;&nbsp;<li style=\"display:inline;\"><a href=\"$url/$i.html\" style=\"text-decoration:none;\"><span style=\"{$color}\">{$i}</span></a></li>";
    }
    //下页链接
    $pdiv .= "&nbsp;&nbsp;<li style=\"display:inline;\"><a href=\"$url/$nextpage.html\" style=\"text-decoration:none;\">&nbsp;&nbsp;下一页</a></li>";
    //末页链接
    $pdiv .= "<li style=\"display:inline;\"><a href=\"$url/$lastpage.html\" style=\"text-decoration:none;\">&nbsp;&nbsp;末页&nbsp;&nbsp;</a></li>";
    $pdiv .= '</div>';        //输出内容
    //不分页则不显示分页列表

    if ($total <= 1)
        $pdiv = '';
        //输出分页列表
        $business_info = array(
            'content' => $arr[$nowpage - 1],
            'page' => $pdiv
        );
        return $business_info;
}

在controller里面我重写了loadModel

public function loadModel($id) {
    //$c = new CDbCriteria();
    //$c->condition = 'article_id=:id';
    //$c->params = array(':id'=>(int)$id);
    //$model = Article::model()->find($c);
    $model = Article::model()->findByPk((int) $id);
    $c = new CDbCriteria();
    $c->condition = 'ainfo_id=:id';
    $c->params = array(':id'=>(int)$model->ainfo_id);
    $info = ArticleInfo::model()->find($c);
    $url = new CUrlManager();
    $model->Article_info->ainfo_content = ArticleInfo::ContentsPages(Article::formaturl($url->createUrl('article/view/', array('id' => $id))), $info->ainfo_content);
    if ($model === null)
        throw new CHttpException(404, '您请求的页面不存在……');
        return $model;
}

view中:

<div id="newsn_left3con">
    <?php echo $model->Article_info->ainfo_content['content']; ?>
</div>
<center>
    <?php echo $model->Article_info->ainfo_content['page']; ?>
</center>

我的思路是在需要分页的地方插入分页符,在读出的时候用分页符将内容分割成数组,再读出。手动分页虽然说麻烦一些,但是不会出现将标签给分开的状况,我还是认为这样较好。当然,还可以用js做假分页,可惜我不会,求高手贴出来~~

申请加精~~

您需要登录后才可以回复。登录 | 立即注册