最近忙于项目,碰到一个问题:在使用action="lists"调用内容模型的数据列表时,如果加上where条件语句,会造成其他条件失效,今天就列表标签where条件失效问题与大家分享修正方法。
比如调用语句:
{pc:content action="lists" catid="$catid" where="posids`!='0'"num="10" order="id DESC" page="$page"} <ul> {loop $data $r} <li><span>{date('Y-m-d H:i:s',$r[inputtime])}</span>·<a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a></li> {/loop} </ul> <div class="pagenavi">{$pages}</div> {/pc}
上面的action="lists"调用语句里边出现了 where="posids`!='0'",于是造成其他条件失效。
通过查找源头php写法,找到/phpcms/modules/content/classes/content_tag.class.php文件,具体修改如下:
if(isset($data['where'])) { $sql = $data['where']; } else { $thumb = intval($data['thumb']) ? " AND thumb != ''" : ''; if($this->category[$catid]['child']) { $catids_str = $this->category[$catid]['arrchildid']; $pos = strpos($catids_str,',')+1; $catids_str = substr($catids_str, $pos); $sql = "status=99 AND catid IN ($catids_str)".$thumb; } else { $sql = "status=99 AND catid='$catid'".$thumb; } }
修改为:
if(isset($data['where'])) { $where = (isset($data['where'])&&(!empty($data['where'])))?' AND '.$data['where']:''; $thumb = intval($data['thumb']) ? " AND thumb != ''" : ''; if($this->category[$catid]['child']) { $catids_str = $this->category[$catid]['arrchildid']; $pos = strpos($catids_str,',')+1; $catids_str = substr($catids_str, $pos); $sql = "status=99".$where." AND catid IN ($catids_str)".$thumb; } else { $sql = "status=99".$where." AND catid='$catid'".$thumb; } } else { $thumb = intval($data['thumb']) ? " AND thumb != ''" : ''; if($this->category[$catid]['child']) { $catids_str = $this->category[$catid]['arrchildid']; $pos = strpos($catids_str,',')+1; $catids_str = substr($catids_str, $pos); $sql = "status=99 AND catid IN ($catids_str)".$thumb; } else { $sql = "status=99 AND catid='$catid'".$thumb; } }