DEDE文档关键词批量导入

DEDE 2019-10-23 19:40:34 评论

dede/article_keywords_main.php

if(empty($keyword)) 上面添加

//文件导入式 批量添加关键词
elseif($dopost == 'fileKeywords')
{
    if(is_uploaded_file($filename)) 
    {
        $newFileName    = date('YmdHis', time() ) .'.txt';
        $newDirPath     = $cfg_basedir . '/uploads/keywords';
        $newFilePath    =  $newDirPath .'/'.$newFileName;
        //目录不存在则创建
        if(!file_exists( $newDirPath )) 
        {
            mkdir($newDirPath, 0777, true);
        }
        move_uploaded_file($filename, $newFilePath);
        //读取上传的文件
        $handle = fopen($newFilePath, 'r') or ShowMsg("上传文件读取失败",-1); 
        $keyArr = array();
        if($handle) 
        {
            while(!feof($handle)) 
            {
                $buffer = fgets($handle, 4096);
                $buffer = str_replace(',', ',', $buffer);
                $buffer = str_replace(array("\n"), '', $buffer);
                $buffer = trim($buffer);
                if(!empty($buffer))
                {
                    $keyArr[] = explode(',', $buffer);
                }
            }
            fclose($handle);
            @unlink( $newFilePath );
        }
        //取出数据库所有的关键词,和新添加关键词比较,如果新添加的关键字,数据中存在则过滤
        $dsql->SetQuery( "SELECT `keyword` FROM `dede_keywords` " );//将SQL查询语句格式化
        $dsql->Execute();//执行SQL操作
        //通过循环输出执行查询中的结果
        $dataKeyArr = array();
        while($row = $dsql->GetArray() )
        {
            $dataKeyArr[] = $row['keyword'];
        }
        //拼接mysql 语句
        $i = 0;

        foreach($keyArr as $k => $v) 
        {
            $key = trim( $v[0] );
			//$key = $v[0];
			//$key =  mb_convert_encoding($key, 'utf-8', 'gbk');
            //把读取txt文档的数据转utf-8编码
			//$key = ($cfg_soft_lang != 'utf-8') ? iconv( "UTF-8", "gb2312//IGNORE" , $key) : $key;
            //如果数据库中存在该关键词,则跳出本次循环。 注意: keyword字段在数据库中长度16字符,需要更更长点
			//ALTER TABLE `dede_keywords` CHANGE `keyword` `keyword` CHAR( 255 ) NOT NULL DEFAULT ''
            if( in_array( $key, $dataKeyArr ) )
                continue;
            //拼接mysql语句
            $rpurl = trim($v[1], ' ');
            $rank = trim($v[2], ' ');
            $sql = "INSERT INTO `dede_keywords`(keyword,`rank`,sta,rpurl) VALUES ('{$key}', '{$rank}', '1', '{$rpurl}')";
            $res = $dsql->ExecuteNoneQuery2($sql);
            if( $res != -1)
                $i ++;
        }
        //如果导入的文件所有的关键词都添加过了,则停止执行;
        if(empty($i))
        {
            ShowMsg("警告:你提交的关键词,早已经添加过了!!!",-1); 
            die();
        }
        else
        {
            ShowMsg("你成功导入{$i}条数据",-1); 
        }
    }
}
//清空关键字表里的所有数据 
else if($dopost == 'delAll')
{
   $dsql->ExecuteNoneQuery('TRUNCATE TABLE `dede_keywords`') ? ShowMsg("你已成功清除所有的关键词!!", $ENV_GOBACK_URL ) : ShowMsg("清除关键词失败",-1) ;

}

/dede/templets/article_keywords_main.htm

</script>

在它的上面加入

//删除选中的
function delSel()
{

  var celements = document.getElementsByClassName('del');
  for( i = 0; i < celements.length; i++ ) 
  {
    if(!celements[i].checked) 
      celements[i].checked = true;
    else 
      celements[i].checked = false;
  }

}
function delAll ()
{
  if( confirm( '你确定要清空所有的关键词,清除以后不可恢复!!' ) )
  {
    location.href="article_keywords_main.php?dopost=delAll";
  }
}

继续找到

<form name='form1' action="article_keywords_main.php">

在它的上面加入

<form action="article_keywords_main.php" method="post" enctype="multipart/form-data">
	<td 
		<strong>关键词维护</strong>
		<strong>&nbsp;&nbsp;</strong>
		<strong><input type="file"  name="filename"></strong>
		<input type="hidden"  name="dopost"  value="fileKeywords" />
		<strong><input type="submit" value="提交"></strong>
	</td>
</form>

继续找到

<input type='checkbox' name='isdel_{dede:field.aid/}' class='np' value='1' />

改成

<input type='checkbox' name='isdel_{dede:field.aid/}' class='np del' value='1' />

继续找到

<td 
	    &nbsp;
	    <input type="button" name="dl1" class="coolbg np" value="反选" onClick="selAll()" />
	    &nbsp;
	    <input type="button" name="dl2" class="coolbg np" value="保存更改" onClick="this.form.submit();" />
	  </td>

修改

<td height="28" colspan="4">
	&nbsp;
	<input type="button" name="dl1" class="coolbg np" value="反选" onClick="selAll()" />
	&nbsp;
	<input type="button" name="dl2" class="coolbg np" value="保存更改" onClick="this.form.submit();" />
</td>
<td height="28" align="center" >
	<input type="button"  value="清空所有关键词" onClick="delAll()" />
	&nbsp;
	<input type="button" name="dl1" class="coolbg np" value="删除全选" onClick="delSel()" />
</td>

评论

说点什么吧
  • 全部评论(0
    还没有评论,快来抢沙发吧!