织梦做商城,按销量,价格等自定义模型字段排序列表解决方案
做织梦(5.7)商城,会用到按销量,价格等自定义模型字段排序列表,这时就需使用freelist标签。但freelist标签
不支持按照自定义字段排序,实现此功能的解决办法如下:
以添加按商品价格(自定义字段名:price)排序为例
1、打开后台模板dede/templets/freelist_add.htm
找到大概243行<select name="orderby" id="orderby" style="width:120">,为其添加一个选项到最后
结果如下:
<select name="orderby" id="orderby" style="width:120">
<option value="sortrank">置顶权限值</option>
<option value="pubdate" selected>发布时间</option>
<option value="senddate">录入时间</option>
<option value="click">点击量</option>
<option value="id">文档ID</option>
<option value="lastpost">最后评论时间</option>
<option value="postnum">评论总数</option>
<option value="price">商品价格</option>
</select>
保存关闭
2、打开后台模板dede/templets/freelist_edit.htm
找到大概219行 $sorta = "sortrank,置顶权限值;pubdate,发布时间;... ,为其添加一个选项到最后
结果如下:
$sorta = "sortrank,置顶权限值;pubdate,发布时间;senddate,录入时间;click,点击量;id,文档ID,lastpost,最后评论时间;postnum,评论总数;rand,随机获取;price,商品价格";
然后在下方<select name="orderby" id="orderby" style="width:120">最后同样添加一个选项
<select name="orderby" id="orderby" style="width:120">
<?php
echo "<option value=\"$orderby\" selected>{$sortarrs[$orderby]}</option>\r\n";
?>
<option value="sortrank">置顶权限值</option>
<option value="pubdate">发布时间</option>
<option value="senddate">录入时间</option>
<option value="click">点击量</option>
<option value="id">文档ID</option>
<option value="lastpost">最后评论时间</option>
<option value="postnum">评论总数</option>
<option value="price">商品价格</option>
</select>
保存关闭
3、添加自由列表时,就可以选择“商品排序”这个选项了。
4、打开include/arc.freelist.class.php
找到
//排序方式
$ordersql = "";
if($orderby=="senddate")
这段代码,在最后一个else前面添加一段代码,结果如下:
//排序方式
$ordersql = "";
if($orderby=="senddate")
{
$ordersql=" ORDER BY arc.senddate $orderWay";
}
else if($orderby=="pubdate")
{
$ordersql=" ORDER BY arc.pubdate $orderWay";
}
else if($orderby=="id")
{
$ordersql=" ORDER BY arc.id $orderWay";
}
else if($orderby=="hot"||$orderby=="click")
{
$ordersql = " ORDER BY arc.click $orderWay";
}
else if($orderby=="lastpost")
{
$ordersql = " ORDER BY arc.lastpost $orderWay";
}
else if($orderby=="scores")
{
$ordersql = " ORDER BY arc.scores $orderWay";
}
else if($orderby=="rand")
{
$ordersql = " ORDER BY rand()";
}
else if($orderby=="price") //自定义商品频道按价格排序
{
$ordersql = " ORDER BY ".$addtable.".price";
}
else
{
$ordersql=" ORDER BY arc.sortrank $orderWay";
}
最后最重要的一点:把下面一段代码位置提前到排序这段代码的前面来,否则无法取到$addtable的值
查询语句会出错。调换以后的结果为:
//获得附加表的相关信息
$addField = "";
$addJoin = "";
if(is_object($this->ChannelUnit))
{
$addtable = $this->ChannelUnit->ChannelInfos['addtable'];
if($addtable!="")
{
$addJoin = " LEFT JOIN $addtable ON arc.id = ".$addtable.".aid ";
$addField = "";
$fields = explode(",",$this->ChannelUnit->ChannelInfos['listfields']);
foreach($fields as $k=>$v)
{
$nfields[$v] = $k;
}
foreach($this->ChannelUnit->ChannelFields as $k=>$arr)
{
if(isset($nfields[$k]))
{
if(!empty($arr['rename']))
{
$addField .= ",".$addtable.".".$k." as ".$arr['rename'];
}
else
{
$addField .= ",".$addtable.".".$k;
}
}
}
}
}
//排序方式
$ordersql = "";
if($orderby=="senddate")
{
$ordersql=" ORDER BY arc.senddate $orderWay";
}
else if($orderby=="pubdate")
{
$ordersql=" ORDER BY arc.pubdate $orderWay";
}
else if($orderby=="id")
{
$ordersql=" ORDER BY arc.id $orderWay";
}
else if($orderby=="hot"||$orderby=="click")
{
$ordersql = " ORDER BY arc.click $orderWay";
}
else if($orderby=="lastpost")
{
$ordersql = " ORDER BY arc.lastpost $orderWay";
}
else if($orderby=="scores")
{
$ordersql = " ORDER BY arc.scores $orderWay";
}
else if($orderby=="rand")
{
$ordersql = " ORDER BY rand()";
}
else if($orderby=="price") //自定义商品频道按价格排序
{
$ordersql = " ORDER BY ".$addtable.".price";
}
else
{
$ordersql=" ORDER BY arc.sortrank $orderWay";
}
保存关闭。
不支持按照自定义字段排序,实现此功能的解决办法如下:
以添加按商品价格(自定义字段名:price)排序为例
1、打开后台模板dede/templets/freelist_add.htm
找到大概243行<select name="orderby" id="orderby" style="width:120">,为其添加一个选项到最后
结果如下:
<select name="orderby" id="orderby" style="width:120">
<option value="sortrank">置顶权限值</option>
<option value="pubdate" selected>发布时间</option>
<option value="senddate">录入时间</option>
<option value="click">点击量</option>
<option value="id">文档ID</option>
<option value="lastpost">最后评论时间</option>
<option value="postnum">评论总数</option>
<option value="price">商品价格</option>
</select>
保存关闭
2、打开后台模板dede/templets/freelist_edit.htm
找到大概219行 $sorta = "sortrank,置顶权限值;pubdate,发布时间;... ,为其添加一个选项到最后
结果如下:
$sorta = "sortrank,置顶权限值;pubdate,发布时间;senddate,录入时间;click,点击量;id,文档ID,lastpost,最后评论时间;postnum,评论总数;rand,随机获取;price,商品价格";
然后在下方<select name="orderby" id="orderby" style="width:120">最后同样添加一个选项
<select name="orderby" id="orderby" style="width:120">
<?php
echo "<option value=\"$orderby\" selected>{$sortarrs[$orderby]}</option>\r\n";
?>
<option value="sortrank">置顶权限值</option>
<option value="pubdate">发布时间</option>
<option value="senddate">录入时间</option>
<option value="click">点击量</option>
<option value="id">文档ID</option>
<option value="lastpost">最后评论时间</option>
<option value="postnum">评论总数</option>
<option value="price">商品价格</option>
</select>
保存关闭
3、添加自由列表时,就可以选择“商品排序”这个选项了。
4、打开include/arc.freelist.class.php
找到
//排序方式
$ordersql = "";
if($orderby=="senddate")
这段代码,在最后一个else前面添加一段代码,结果如下:
//排序方式
$ordersql = "";
if($orderby=="senddate")
{
$ordersql=" ORDER BY arc.senddate $orderWay";
}
else if($orderby=="pubdate")
{
$ordersql=" ORDER BY arc.pubdate $orderWay";
}
else if($orderby=="id")
{
$ordersql=" ORDER BY arc.id $orderWay";
}
else if($orderby=="hot"||$orderby=="click")
{
$ordersql = " ORDER BY arc.click $orderWay";
}
else if($orderby=="lastpost")
{
$ordersql = " ORDER BY arc.lastpost $orderWay";
}
else if($orderby=="scores")
{
$ordersql = " ORDER BY arc.scores $orderWay";
}
else if($orderby=="rand")
{
$ordersql = " ORDER BY rand()";
}
else if($orderby=="price") //自定义商品频道按价格排序
{
$ordersql = " ORDER BY ".$addtable.".price";
}
else
{
$ordersql=" ORDER BY arc.sortrank $orderWay";
}
最后最重要的一点:把下面一段代码位置提前到排序这段代码的前面来,否则无法取到$addtable的值
查询语句会出错。调换以后的结果为:
//获得附加表的相关信息
$addField = "";
$addJoin = "";
if(is_object($this->ChannelUnit))
{
$addtable = $this->ChannelUnit->ChannelInfos['addtable'];
if($addtable!="")
{
$addJoin = " LEFT JOIN $addtable ON arc.id = ".$addtable.".aid ";
$addField = "";
$fields = explode(",",$this->ChannelUnit->ChannelInfos['listfields']);
foreach($fields as $k=>$v)
{
$nfields[$v] = $k;
}
foreach($this->ChannelUnit->ChannelFields as $k=>$arr)
{
if(isset($nfields[$k]))
{
if(!empty($arr['rename']))
{
$addField .= ",".$addtable.".".$k." as ".$arr['rename'];
}
else
{
$addField .= ",".$addtable.".".$k;
}
}
}
}
}
//排序方式
$ordersql = "";
if($orderby=="senddate")
{
$ordersql=" ORDER BY arc.senddate $orderWay";
}
else if($orderby=="pubdate")
{
$ordersql=" ORDER BY arc.pubdate $orderWay";
}
else if($orderby=="id")
{
$ordersql=" ORDER BY arc.id $orderWay";
}
else if($orderby=="hot"||$orderby=="click")
{
$ordersql = " ORDER BY arc.click $orderWay";
}
else if($orderby=="lastpost")
{
$ordersql = " ORDER BY arc.lastpost $orderWay";
}
else if($orderby=="scores")
{
$ordersql = " ORDER BY arc.scores $orderWay";
}
else if($orderby=="rand")
{
$ordersql = " ORDER BY rand()";
}
else if($orderby=="price") //自定义商品频道按价格排序
{
$ordersql = " ORDER BY ".$addtable.".price";
}
else
{
$ordersql=" ORDER BY arc.sortrank $orderWay";
}
保存关闭。
至此,大功告成,如需添加其他字段,按照此方法处理即可。