打開織夢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"); $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; } } } //end
然后找到代碼:
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 ) ); } //end
最后找到function GetArcList這個函數(shù),在其后添加一個可以通過傳入sql參數(shù)獲取指定數(shù)據(jù)源的方法,代碼如下:
/** * 通過listsql標簽中sql屬性傳入的參數(shù)來獲得一個單列的文檔列表 * */ 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') { //傳遞整個數(shù)組,在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; } //end
總共就添加三段代碼,每一段代碼基本都參考它緊接著的上面那段原始代碼,而無需改變它原來任何一個地方的代碼,應該算是比較完美了,接下來在模板文件中的使用方法就跟一開始思路中所提到的那樣,分頁標簽依舊沿用原來的,調(diào)用范例:
{dede:listsql sql='select ID,post_title from wp_posts' pagesize='10'}
<li><a href="[field:ID/].html">[field:post_title/]</a></li>
{/dede:listsql}
<!--分頁-->
{dede:pagelist listsize='2' listitem='index pre pageno next end '/}
注:以上解決方案適用于dedecms5.6-5.7版本。
版權(quán)聲明: 本站資源均來自互聯(lián)網(wǎng)或會員發(fā)布,如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除!謝謝!
轉(zhuǎn)載請注明: 織夢用{dede:sql}標簽實現(xiàn)分頁的方法