dede:sql實現分頁

思路是把dede:list標籤進行改造, 列表頁專用標籤的工做原理大體是先經過欄目變量id獲取到對應的數據源再呈現到頁面上來,那麼 就能夠讓它不單單經過欄目變量id還能夠經過指定的sql語句來獲取數據源 能夠另外嵌入一個相似{dede:listsql sql='select * from wp_posts' pagesize='10'}的標籤來使用。 php

打開include/arc.listview.class.php這個文件html

找到: sql

 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");
$cquery = preg_replace("/SELECT(.*?)FROM/is", " SELECT count(*) as dd FROM ", $cquery);
$cquery = preg_replace("/ORDER(.*?)SC/is", "", $cquery);
$row = $this->dsql->GetOne($cquery);
if(is_array($row))
{
$this->TotalResult = $row['dd'];
}
else
{
$this->TotalResult = 0;
}
}
}
而後找到:
if($ctag->GetName()=="list")
{
$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->GetArcList(
$limitstart,
$row,
$ctag->GetAtt("col"),
$ctag->GetAtt("titlelen"),
$ctag->GetAtt("infolen"),
$ctag->GetAtt("imgwidth"),
$ctag->GetAtt("imgheight"),
$ctag->GetAtt("listtype"),
$ctag->GetAtt("orderby"),
$InnerText,
$ctag->GetAtt("tablewidth"),
$ismake,
$ctag->GetAtt("orderway")
)
);
}
這一段,在其後添加以下代碼:
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 GetArcList這個方法,在其後添加一個能夠經過傳入sql參數獲取指定數據源的方法,代碼以下:
curl

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}";

$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;
}
總共就添加三段代碼,調用範例:
{dede:listsql sql='select ID,post_title from wp_posts' pagesize='10'}
<li><a href="http://gump.me/[field:ID /].html">[field:post_title /]</a></li>
{/dede:listsql}
<!--分頁-->
{dede:pagelist listsize='2' listitem='index pre pageno next end '/}post

{dede:sql}和{dede:listsql}的文章連接地址<a href="[field:id runphp='yes'] $id=@me ;@me='';$url=GetOneArchive($id);@me=$url['arcurl'];[/field:id]"> this

相關文章
相關標籤/搜索