1. 程式人生 > >織夢用dede:sql實現列表頁分頁教程方法

織夢用dede:sql實現列表頁分頁教程方法

將dede:list標籤進行改造,使用SQL標籤實現靜態分頁,在自定義表單呼叫的分頁用他就很方便

例如會員列表的模板標籤寫法

{dede:listsql sql="select * from myblog_member" pagesize="10"}
<li><a href="https://www.dedehtml.com/[field:XX /].html">[field:XX /]</a></li>
{/dede:listsql}
<!--分頁-->
{dede:pagelist/}

實現教程

開啟include/arc.listview.class.php 找到

if(!is_object($ctag))
{
	$ctag = $this->dtp->GetTag("list");
}

在下面加入

if(!is_object($ctag))
{
    $ctag = $this->dtp->GetTag("listsql");
    if(is_object($ctag))
    {
        $cquery = $ctag->GetAtt("sql");
        //$sql = str_replace('~reid~',$this->ReID,$cquery); 這是另一個客戶要求的獲取url第2個引數才加的。
        $cquery = preg_replace("/SELECT(.*?)FROM/is", " SELECT count(*) as dd FROM ", $sql);
        $cquery = preg_replace("/ORDER(.*?)SC/is", "", $cquery);
        $row = $this->dsql->GetOne($cquery);
        if(is_array($row))
        {
            $this->TotalResult = $row['dd'];
        }
        else
        {
            $this->TotalResult = 0;
        }
    }
}

繼續找到

else if($ctag->GetName()=="pagelist")

在它上面加入

else if($ctag->GetName()=="listsql")
{
    $limitstart = ($this->PageNo-1) * $this->PageSize;
    $row = $this->PageSize;
    if(trim($ctag->GetInnerText())=="")
    {
        $InnerText = GetSysTemplets("list_fulllist.htm");
    }
    else
    {
        $InnerText = trim($ctag->GetInnerText());
    }
    $this->dtp->Assign($tagid,
    $this->GetSqlList(
    $limitstart,
    $row,
    $ctag->GetAtt("sql"),
    $InnerText
    ));
}

最後找到

function GetPageListST(

在它上面加入

function GetSqlList($limitstart = 0, $row = 10, $sql = '', $innertext)
{
    global $cfg_list_son;
    $innertext = trim($innertext);

    if ($innertext == '')
    {
        $innertext = GetSysTemplets('list_fulllist.htm');
    }
    //處理SQL語句
    $limitStr = " LIMIT {$limitstart},{$row}";
    $sql = str_replace('~reid~',$this->ReID,$sql);
    
    $this->dsql->SetQuery($sql . $limitStr);
    $this->dsql->Execute('al');
    $t2 = ExecTime();

    //echo $t2-$t1;
    $sqllist = '';
    $this->dtp2->LoadSource($innertext);
    $GLOBALS['autoindex'] = 0;

    //獲取欄位
    while($row = $this->dsql->GetArray("al"))
    {
        $GLOBALS['autoindex']++;
        if(is_array($this->dtp2->CTags))
        {
            foreach($this->dtp2->CTags as $k=>$ctag)
            {
                if($ctag->GetName()=='array')
                {
                    //傳遞整個陣列,在runphp模式中有特殊作用
                    $this->dtp2->Assign($k,$row);
                }
                else
                {
                    if(isset($row[$ctag->GetName()]))
                    {
                        $this->dtp2->Assign($k,$row[$ctag->GetName()]);
                    }
                    else
                    {
                        $this->dtp2->Assign($k,'');
                    }
                }
            }
        }

        $sqllist .= $this->dtp2->GetResult();

    }//while

    $t3 = ExecTime();
    //echo ($t3-$t2);
    $this->dsql->FreeResult('al');

    return $sqllist;
}

完成,注意程式碼放置的位置,有的是在上面有的是在下面。