修正 dedecms dede:sql like模糊查询 多出'号

找到 \include\taglib\sql.lib.php,  复制,重命名为likesql.lib.php

修改函数名为--->function lib_ likesql(&$ctag,&$refObj)

修改:
    //$sql = str_replace($conditions[0][$key], "'".addslashes($refObj->Fields[$value])."'", $sql);
      $sql = str_replace($conditions[0][$key], addslashes($refObj->Fields[$value]), $sql);

<?php
if(!defined('DEDEINC'))
{
	exit("Request Error!");
}
function lib_likesql(&$ctag,&$refObj)
{
	global $dsql,$sqlCt;
	$attlist="sql|";
	FillAttsDefault($ctag->CAttribute->Items,$attlist);
	extract($ctag->CAttribute->Items, EXTR_SKIP);

	//传递环境参数
	preg_match_all("/~([A-Za-z0-9]+)~/s", $sql, $conditions);
	if(is_array($conditions))
	{
		foreach ($conditions[1] as $key => $value)
		{
			if(isset($refObj->Fields[$value]))
			{
				//$sql = str_replace($conditions[0][$key], "'".addslashes($refObj->Fields[$value])."'", $sql);
				$sql = str_replace($conditions[0][$key], addslashes($refObj->Fields[$value]), $sql);
			}
		}
	}

	$revalue = '';
	$Innertext = trim($ctag->GetInnerText());

	if($sql=='' || $Innertext=='') return '';
	if(empty($sqlCt)) $sqlCt = 0;

	$ctp = new DedeTagParse();
	$ctp->SetNameSpace('field','[',']');
	$ctp->LoadSource($Innertext);

	$thisrs = 'sq'.$sqlCt;
	$dsql->Execute($thisrs,$sql);
	$GLOBALS['autoindex'] = 0;
	while($row = $dsql->GetArray($thisrs))
	{
		$sqlCt++;
		$GLOBALS['autoindex']++;
		foreach($ctp->CTags as $tagid=>$ctag)
		{
			  if($ctag->GetName()=='array')
				{
						$ctp->Assign($tagid,$row);
				}
				else
				{
					if( !empty($row[$ctag->GetName()])) $ctp->Assign($tagid,$row[$ctag->GetName()]); 
				}
		}
		$revalue .= $ctp->GetResult();
	}
	return $revalue;
}
?>

模板调用:
{dede:likesql sql="select * from #@__keywords where keyword like '%~typename~%' limit 25";}
     <a href='[field:rpurl/]' title="[field:keyword/]" target="_blank">[field:keyword/]</a>
    [field:global.autoindex runphp='yes']@me=(@me%5==0)?'<br>':'';[/field:global.autoindex]
{/dede:likesql}

GameOver!!!   最后记得上传至 \include\taglib\ 目录下


方法二:
新建 rel.lib.php[名字可任意取],内容如下:
<?php
/*
	author:miaoyaoyao
	QQ:429660248
*/
if(!defined("DEDEINC"))
{
	exit("Request Error!!!");
}
function lib_rel(&$ctag,&$refObj) {
	global $dsql,$envs;
	$innertext=$ctag->getInnerText();
	$revalue='';
	$ctp = new DedeTagParse();
	$ctp->SetNameSpace('field', '[', ']');
	$keyword=$_GET['keyword'];
	//print_r($refObj->Fields);
	//注意看下面的sql语句,很多都通过这些改
	//特别要会使用下$refObj->Fields这个强大的数组,大小写也特别注意下
	$sql = "select * from #@__archives as a join #@__addonarticle as b on a.id=b.aid where ((a.title like '%$keyword%')and(b.body like '%$keyword%')) order by a.click desc limit 0,8";
	$dsql->execute('me',$sql);
	while($row=$dsql->getArray('me')){
		$row['title']=str_replace($keyword,"<em>$keyword</em>",$row['title']);
		$row['description']=cn_substr($row['description'],76);
		$row['description']=str_replace($keyword,"<em class='noline'>$keyword</em>",$row['description']);
		
		//$row['description']=mb_substr($row['description'],0,80,'GBK');
		$arr=GetOneArchive($row['id']);
		$row['arcurl']=$arr['arcurl'];
		$row['fulltitle']=strip_tags($row['title']);
		$ctp->loadsource($innertext);
		foreach($ctp->CTags as $tagid=>$ctag){
			if(!empty($row[strtolower($ctag->getName())])){
				$ctp->Assign($tagid,$row[$ctag->getName()]);
			}
		
		}
		$revalue .= $ctp->GetResult();
	}
	return $revalue;
}
上传至/include/taglib/下,模版调用---->
{dede:rel}
     <a href='[field:rpurl/]' title="[field:keyword/]" target="_blank">[field:keyword/]</a>
    [field:global.autoindex runphp='yes']@me=(@me%5==0)?'<br>':'';[/field:global.autoindex]
{/dede:rel}

你可能感兴趣的:(dedecms,修正,dede模糊查询,like模糊查询,多出,dede:sql)