这是在做网站的时候遇到的一个不起眼但却很不好解决的问题,先把出问题的代码贴出来:
- {loop subcat($catid) $k $v}
- {php $subcatid[] = $k;}
- {/loop}
- {php $subcatid = implode(',', $subcatid);}
- <?php
- $sql="SELECT * from ".gettablepre()."enpicture where status=99 and catid in ($subcatid) order by id DESC";
- ?>
- {pc:get sql="$sql" page="$page" return="data"}
- {loop $data $r}
- <div class="cat_pic_bd">
- <a href="{$r[url]}"><p class="picborder2"><img src="{thumb($r[thumb],200,150)}" width="200" height="150" /></p>
- <p>{$r[title]}</p></a>
- </div>
- {/loop}
- {/pc}
这段代码的功能是把当前栏目下所有子栏目的信息调取并显示出来,但是,不管怎么调取都调取不出来.
echo var_dump(subcat($catid))返回NULL;
我拉个擦来去,郁闷了,无奈,怀疑是不是subcat()函数参数传递不对,于是打开phpcms/libs/functions/global.func.php找subcat()函数,代码如下:
- /**
- * 获取子栏目
- * @param $parentid 父级id
- * @param $type 栏目类型
- * @param $self 是否包含本身 0为不包含
- * @param $siteid 站点id
- */
- function subcat($parentid = NULL, $type = NULL,$self = '0', $siteid = '') {
- if (emptyempty($siteid)) $siteid = get_siteid();
- $category = getcache('category_content_'.$siteid,'commons');
- foreach($category as $id=>$cat) {
- if($cat['siteid'] == $siteid && ($parentid === NULL || $cat['parentid'] == $parentid) && ($type === NULL || $cat['type'] == $type)) $subcat[$id] = $cat;
- if($self == 1 && $cat['catid'] == $parentid && !$cat['child']) $subcat[$id] = $cat; //Vevb.com
- }
- return $subcat;
- }
傻眼了,果然,参数传递不正确,没有传递栏目类型和站点id,而在开启站群功能后,分站必须要传递$siteid参数,修改后的代码如下:
- {loop subcat($catid,0,0,$siteid) $k $v}
- {php $subcatid[] = $k;}
- {/loop}
- {php $subcatid = implode(',', $subcatid);}
- <?php
- $sql="SELECT * from ".gettablepre()."enpicture where status=99 and catid in ($subcatid) order by id DESC";
- ?>
- {pc:get sql="$sql" page="$page" return="data"}
- {loop $data $r}
- <div class="cat_pic_bd">
- <a href="{$r[url]}"><p class="picborder2"><img src="{thumb($r[thumb],200,150)}" width="200" height="150" /></p>
- <p>{$r[title]}</p></a>
- </div>
- {/loop}
- {/pc}
完美解决问题.
新闻热点
疑难解答