ecshop实现文章扩展分类

2009-09-25 18:20 来源:www.chinab4c.com 作者:ecshop专家

1:模板article_info.htm中增加以下程序,用于扩展ecshop文章分类
 <tr>
            <td class="label">扩展分类</td>
            <td>
              <input type="button" value="{$lang.add}" onclick="addOtherCat(this.parentNode)" class="button" />
              {foreach from=$article.other_cat item=cat_id}
              <select name="other_cat[]"><option value="0">{$lang.select_please}</option>{$other_cat_list.$cat_id}</select>
              {/foreach}
            </td>
          </tr>

2:模板article_info.htm中增加以下JS,控制扩展分类
function addOtherCat(conObj)
  {
      var sel = document.createElement("SELECT");
      var selCat = document.forms['theForm'].elements['article_cat'];

      for (i = 0; i < selCat.length; i++)
      {
          var opt = document.createElement("OPTION");
          opt.text = selCat.options[i].text;
          opt.value = selCat.options[i].value;
          if (Browser.isIE)
          {
              sel.add(opt);
          }
          else
          {
              sel.appendChild(opt);
          }
      }
      conObj.appendChild(sel);
      sel.name = "other_cat[]";
      sel.onChange = function() {checkIsLeaf(this);};
  }

3:article.php中增加以下函数
编辑和扩展分类
function handle_article_other_cat($article_id, $cat_list)
{
    /* 查询现有的扩展分类 */
    $sql = "SELECT cat_id FROM " . $GLOBALS['ecs']->table('article_category') .
            " WHERE article_id = '$article_id'";
    $exist_list = $GLOBALS['db']->getCol($sql);

    /* 删除不再有的分类 */
    $delete_list = array_diff($exist_list, $cat_list);
    if ($delete_list)
    {
        $sql = "DELETE FROM " . $GLOBALS['ecs']->table('article_category') .
                " WHERE article_id = '$article_id' " .
                "AND cat_id " . db_create_in($delete_list);
        $GLOBALS['db']->query($sql);
    }

    /* 添加新加的分类 */
    $add_list = array_diff($cat_list, $exist_list, array(0));
    foreach ($add_list AS $cat_id)
    {
        // 插入记录
        $sql = "INSERT INTO " . $GLOBALS['ecs']->table('article_category') .
                " (article_id, cat_id) " .
                "VALUES ('$article_id', '$cat_id')";
        $GLOBALS['db']->query($sql);
    }
}


4:article.php中的act=insert中加入
if (isset($_POST['other_cat']))
    {
        handle_article_other_cat($article_id, array_unique($_POST['other_cat']));
    }
用于写入扩展分类ID和文章ID

5:article.php中的act=edit中加入
$other_cat_list = array();
        $sql = "SELECT cat_id FROM " . $ecs->table('article_category') . " WHERE article_id = '$_REQUEST[id]'";
        $article['other_cat'] = $db->getCol($sql);
      
        foreach ($article['other_cat'] AS $cat_id)
        { 
            $other_cat_list[$cat_id] = article_cat_list(0, $cat_id);
        }
        $smarty->assign('other_cat_list', $other_cat_list);

6:article.php中的act=update加以下代码
 if (isset($_POST['other_cat']))
     {
         handle_article_other_cat($_POST[id], array_unique($_POST['other_cat']));
     }

用来编辑扩展分类

6:建立数据表article_category
CREATE TABLE `ecs_article_category` (
  `article_id` int(1) DEFAULT NULL,
  `cat_id` int(1) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk;