首页 > 代码库 > dede织梦批量导入关键词

dede织梦批量导入关键词

在后台替换对应的文件件即可

article_keywords_main.php

<?php
/**
 * 文档关键词管理
 *
 * @version        $Id: article_keywords_main.php 1 14:12 2010年7月12日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/config.php");
CheckPurview(‘sys_Keyword‘);
require_once(DEDEINC."/datalistcp.class.php");
setcookie("ENV_GOBACK_URL",$dedeNowurl,time()+3600,"/");
if(empty($dopost)) $dopost = ‘‘;



//保存批量更改
if($dopost==‘saveall‘)
{
    $ENV_GOBACK_URL = empty($_COOKIE[‘ENV_GOBACK_URL‘]) ? "article_keywords_main.php" : $_COOKIE[‘ENV_GOBACK_URL‘];
    if(!isset($aids))
    {
        ShowMsg("你没有选择要更改的内容!",$ENV_GOBACK_URL);
        exit();
    }
    foreach($aids as $aid)
    {
        $rpurl = ${‘rpurl_‘.$aid};
        $rpurlold = ${‘rpurlold_‘.$aid};
        $keyword = ${‘keyword_‘.$aid};

        //删除项目
        if(!empty(${‘isdel_‘.$aid}))
        {
            $dsql->ExecuteNoneQuery("DELETE FROM `#@__keywords` WHERE aid=‘$aid‘");
            continue;
        }

        //禁用项目
        $staold = ${‘staold_‘.$aid};
        $sta = empty(${‘isnouse_‘.$aid}) ? 1 : 0;
        if($staold!=$sta)
        {
            $query1 = "UPDATE `#@__keywords` SET sta=‘$sta‘,rpurl=‘$rpurl‘ WHERE aid=‘$aid‘ ";
            $dsql->ExecuteNoneQuery($query1);
            continue;
        }

        //更新链接网址
        if($rpurl!=$rpurlold)
        {
            $query1 = "UPDATE `#@__keywords` SET rpurl=‘$rpurl‘ WHERE aid=‘$aid‘ ";
            $dsql->ExecuteNoneQuery($query1);
        }
    }
    ShowMsg("完成指定的更改!",$ENV_GOBACK_URL);
    exit();
}
//增加关键字
else if($dopost==‘add‘)
{
    $ENV_GOBACK_URL = empty($_COOKIE[‘ENV_GOBACK_URL‘]) ? "-1" : $_COOKIE[‘ENV_GOBACK_URL‘];
    $keyword = trim($keyword);
    $rank = preg_replace("#[^0-9]#", ‘‘, $rank);
    if($keyword==‘‘)
    {
        ShowMsg("关键字不能为空!",-1);
        exit();
    }
    $row = $dsql->GetOne("SELECT * FROM `#@__keywords` WHERE keyword LIKE ‘$keyword‘");
    if(is_array($row))
    {
        ShowMsg("关键字已存在库中!","-1");
        exit();
    }
    $inquery = "INSERT INTO `#@__keywords`(keyword,rank,sta,rpurl) VALUES (‘$keyword‘,‘$rank‘,‘1‘,‘$rpurl‘);";
    $dsql->ExecuteNoneQuery($inquery);
    ShowMsg("成功增加一个关键字!",$ENV_GOBACK_URL);
    exit();
}
//文件导入式 批量添加关键词
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 `#@__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] );
            //把读取txt文档的数据转utf-8编码
            $key =  mb_convert_encoding($key, ‘utf-8‘, ‘gbk‘);
            //如果数据库中存在该关键词,则跳出本次循环。 注意: keyword字段在数据库中长度16字符,需要更更长点
            if( in_array( $key, $dataKeyArr ) )
                continue;
            //拼接mysql语句
            $rpurl  =   trim($v[1], ‘ ‘);
            $rank   =   trim($v[2], ‘ ‘);
            $sql = "INSERT INTO `#@__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); 
        }

       





    }




    
}

if(empty($keyword))
{
    $keyword = ‘‘;
    $addquery = ‘‘;
}
else
{
    $addquery = " WHERE keyword LIKE ‘%$keyword%‘ ";
}

$sql = "SELECT * FROM `#@__keywords` $addquery ORDER BY rank DESC";
$dlist = new DataListCP();
$dlist->pageSize = 20;
$dlist->SetParameter("keyword",$keyword);
$dlist->SetTemplate(DEDEADMIN."/templets/article_keywords_main.htm");
$dlist->SetSource($sql);
$dlist->Display();

function GetSta($sta)
{
    if($sta==1) return ‘‘;
    else return ‘ checked="1" ‘;
}

 

article_keywords_main.htm文件

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $cfg_soft_lang; ?>">
<title>关键字管理</title>
<link href="css/base.css" rel="stylesheet" type="text/css">
<link href="div.css" rel="stylesheet" type="text/css">
<style>
    .nnpp{
    border-bottom:1px solid #666666;
    border-top:1px solid #FFFFFF;
    border-left:1px solid #FFFFFF;
    border-right:1px solid #FFFFFF;
    filter:alpha(opacity=50);
 }
</style>
<script language=‘javascript‘>
function selAll()
{
    var celements = document.getElementsByName(aids[]);
    for(i=0;i<celements.length;i++)
    {
        if(!celements[i].checked) celements[i].checked = true;
        else celements[i].checked = false;
    }
}

//全选删除
function delAll()
{

  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;
  }

}
</script>
</head>
<body background=‘images/allbg.gif‘ leftmargin=‘8‘ topmargin=‘8‘>
<div class="main">
  <table width="98%" border="0" cellpadding="1" align=‘center‘ cellspacing="1" bgcolor="#D6D6D6">
    <tr>
      <td height="26" colspan="6" background="images/tbg.gif"><table width="96%" border="0">
          <tr>
            <form  action="article_keywords_main.php" method="POST" enctype="multipart/form-data"  >
            <td width="40%"  style="padding-left:10px;">
              <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>
            <form name=‘form1‘ action="article_keywords_main.php">
            <td width="75%" align="right">
               搜索:<input type=‘text‘ name=‘keyword‘ size=‘20‘ />
               <input type=‘submit‘ name=‘sb‘ value=‘确定‘ class="coolbg np" />
               &nbsp;
            <input class="coolbg np" name="n1" type="button" id="n1" value="分析系统内的关键字" onClick="location=‘article_keywords_make.php‘;" style="width:130px" />
            &nbsp;
            <input class="coolbg np" name="n11" type="button" id="n11" value="管理搜索的关键字" onClick="location=‘search_keywords_main.php‘;" style="width:125px" />
            </td></form>
          </tr>
        </table></td>
    </tr>
    <form name=‘form2‘ action="article_keywords_main.php">
        <input type=‘hidden‘ name=‘dopost‘ value=‘add‘ />
      <tr align="center" bgcolor="#ffffff">
        <td width="100%" height="30" colspan="6">
            <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="100" align="center">新增关键字:</td>
              <td width="140"><input name="keyword" type="text" id="keyword" style="width:130px" /></td>
              <td width="60">链接网址:</td>
              <td width="140"><input name="rpurl" type="text" id="rpurl" style="width:130px" /></td>
              <td width="40">频率:</td>
              <td width="50"><input name="rank" type="text" id="rank" style="width:30px" value="30" /></td>
              <td width="80"><input type="submit" name="Submit" value="保存" class="coolbg np" /></td>
            </tr>
          </table>
          </td>
      </tr>
    </form>
    <tr align="center" bgcolor="#FBFCE2" height="26">
      <td width="8%">选择</td>
      <td width="25%">关键字</td>
      <td width="35%">链接网址</td>
      <td width="10%">频率</td>
      <td width="22%">管理</td>
    </tr>
     <form name=‘form3‘ action="article_keywords_main.php" method="post">
        <input type=‘hidden‘ name=‘dopost‘ value=‘saveall‘ />
   {dede:datalist}
    <tr align="center" bgcolor="#FFFFFF" onm ouseMove="javascript:this.bgColor=‘#FCFDEE‘;" onm ouseOut="javascript:this.bgColor=‘#FFFFFF‘;">
      <td height="24">
          <input name="aids[]" type="checkbox" value="{dede:field.aid/}" class="np" checked=‘1‘ />
      </td>
      <td>
          <input type=‘hidden‘ value=‘{dede:field.keyword/}‘ name=‘keyword_{dede:field.aid/}‘ />
          {dede:field.keyword/}
      </td>
      <td style="padding:5px 0;">
          <input type=‘hidden‘ value=‘{dede:field.rpurl/}‘ name=‘rpurlold_{dede:field.aid/}‘ />
          <input type=‘text‘ style=‘width:90%‘ class=‘nnpp‘ value=‘{dede:field.rpurl/}‘ name=‘rpurl_{dede:field.aid/}‘ />
      </td>
      <td>
          <input type=‘text‘ size=‘5‘ class=‘nnpp‘ value=‘{dede:field.rank/}‘ name=‘rank_{dede:field.aid/}‘ />
      </td>
      <td>
          <input type=‘hidden‘ name=‘staold_{dede:field.aid/}‘ value=‘{dede:field.sta/}‘ />
          <input type=‘checkbox‘ name=‘isnouse_{dede:field.aid/}‘ class=‘np‘ value=‘1‘{dede:field.sta function="GetSta(@me)"/} />
          禁用
          &nbsp;
          <input type=‘checkbox‘ name=‘isdel_{dede:field.aid/}‘ class=‘np del‘ value=‘1‘ />
          删除
      </td>
    </tr>
{/dede:datalist}
     <tr bgcolor="#ffffff">
      <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" >
      &nbsp;
      <input type="button" name="dl1" class="coolbg np" value="删除全选" onClick="delAll()" />
    </td>

    </tr>
    </form>
    <tr align="center" bgcolor="#F9FCEF">
      <td height="28" colspan="6">
          {dede:pagelist listsize=3/}
      </td>
    </tr>
  </table>
</div>
</body>
</html>

 

dede织梦批量导入关键词