function get_attachment_by_id( $id )
{
if( $id == '' || $id == 0 )
{
exit("Function :get_attachment_by_id parame $id Is Not Legaled");
}
$number = 6;
$size = 2;
$string = sprintf("%06d", $id);
$path = array();
for( $i = 0 ;$i < $number/$size ; $i ++)
{
$path[] = substr($string, $i*$size, $size);
}
return implode('/', $path);
}
(1)找到 $html .= ($val['attr_type'] == 1 || $val['attr_type'] == 2) ,修改函数build_attr_html,大约在714行.
在这句后增加下面代码:
$attr_color_images_html = '';
if( $val['goods_attr_id'] && $val['goods_attr_id'] != 0 && $val['goods_attr_id'] != '' )
{
$attr_color_images_dir = "../".DATA_DIR.'/color/'.get_attachment_by_id($goods_id)."/0-0-".$val['goods_attr_id'].".jpg";
file_exists($attr_color_images_dir) && $attr_color_images_html = '<span style="border:1px solid #FF6600;"><img src="'.$attr_color_images_dir.'" width="15" height="15" align="absmiddle"></span>';
}
$html .= $val['attr_name'] == '颜色' ? '<span> 属性图片:<input type="file" name="attr_images_list[]" value="' . $val['attr_images_list'] . '" /></span> '.$attr_color_images_html : '<span style="display:none;"><input type="file" name="attr_images_list[]" value="" /></span>';
(2)修改函数get_attr_list中的$sql变量,增加提取的数据项 v.goods_attr_id
3.修改admin/goods.php,
(1)找到 $attr_price = $_POST['attr_price_list'][$key]; 大约在976行,后面添加:
$attr_images = array('name' => $_FILES['attr_images_list']['name'][$key] , 'type' => $_FILES['attr_images_list']['type'][$key] , 'tmp_name' => $_FILES['attr_images_list']['tmp_name'][$key] , 'error' => $_FILES['attr_images_list']['error'][$key], 'size' => $_FILES['attr_images_list']['size'][$key] );
(2)找到$goods_attr_list[$attr_id][$attr_value]['attr_price'] = $attr_price;后两处,大约在983行和989行,后面分别添加:
$goods_attr_list[$attr_id][$attr_value]['attr_images'] = $attr_images;
(3)找到$db->query($sql);大约在1026,这个有多处(请注意行数),在后面增加:
$goods_attr_id = $info['sign'] == 'insert' ? $db->insert_id() : $info['goods_attr_id'];
if( $info['sign'] == 'insert' || $info['sign'] == 'update' )
{
if( $info['attr_images']['tmp_name'] != '' )
{
$attr_images_dir = 'color/'.get_attachment_by_id($goods_id);
$attr_images_scoure_name = "0-0-$goods_attr_id.jpg";
$color_scoure_images = $image->upload_image($info['attr_images'] , $attr_images_dir , $attr_images_scoure_name ); // 原始图片
}
}
else
{
$attr_color_images_dir = ROOT_PATH.DATA_DIR.'/color/'.get_attachment_by_id($goods_id)."/0-0-".$info[goods_attr_id].".jpg";
file_exists($attr_color_images_dir) && unlink( $attr_color_images_dir );
}
![未命名11111.jpg](https://i-blog.csdnimg.cn/blog_migrate/e7272850a87a534c178339e4fc940a04.png)
前台效果图:
![未命名2222.jpg](https://i-blog.csdnimg.cn/blog_migrate/f2618411cf517dba5661453cd7b55b0b.png)
前台显示:
方法一:
1. 模板的includes\lib_goods.php文件中:
查找:'id' => $row['goods_attr_id']);
修改成:'id' => $row['goods_attr_id'],
'attr' => "../".DATA_DIR.'/color/'.get_attachment_by_id($goods_id)."/0-0-".$row['goods_attr_id'].".jpg");
2.在good.dwt中调用
<img src="{$value.attr}" alt="{$value.label}"/>
方法二:
1.在goods.php中找到: 198行左右
$smarty->assign('categories', get_categories_tree($goods['cat_id'])); // 分类树
在后面添加:
$smarty->assign('attr_url', get_attachment_by_id($goods_id)); // 自定义属性图片
2. 在goods.dwt中调用
<img src="../data/color/{$attr_url}/0-0-{$value.id}.jpg" />
前台效果,我现在还没有做。根据函数get_attachment_by_id ,已经商品id和goods_attr_id 就能取出相对应的颜色图片.
前台完善,我在发效果图..
效果无法关联相册。这是一遗憾。