5年前,剛開始接觸帝國cms的時候,用新聞模型建了一個文章閱讀站,最近網站轉型,想發一些長篇文章,需要將新聞模型轉為文章模型,搜了一下論壇和互聯網,都沒有能用于7.2的相關插件或工具,只好自己動手寫一個。這里把代碼和步驟分享出來,希望能幫助到其它人。
非常重要。萬一在轉換過程中出現意外,確保老數據安全部丟失。
將以下代碼保存為一個php文件,文件編碼格式與你網站編碼相同,代碼中的參數設置部分按照實際情況來填寫。
假設你為此新建了一個名為 zh.php 的文件,那么將此文件放到 e/update/ 文件夾中。那么你這時在瀏覽器中執行以下網址:
此程序會分組轉換數據。
請注意:
(1)不能重復轉換,否則數據丟失。
(2)轉換完之后請立即在服務器上刪除此文件。
(3)此程序適用于帝國cms7.0及后續版本。
<?phperror_reporting(E_ERROR | E_WARNING | E_PARSE);@set_time_limit(1000);//********************* 參數設置開始 *********************$newstb="gushi"; //需要轉換的數據表$cf='newstext';//需要轉換的字段名$fa=1;//字段位置,0為主表,1為副表$changeline=50; //每組轉換數//********************* 參數設置結束 *********************if($_GET['tochange']==1){ include("../class/connect.php"); include("../class/db_sql.php"); include("../class/functions.php"); $link=db_connect(); $empire=new mysqlquery(); dp_ChangeNewsToArticle($_GET['start']);}//開始轉換function dp_ChangeNewsToArticle($start=0){ global $empire,$newstb,$cf,$fa,$changeline,$dbtbpre; $start=(int)$start; if($start==0){ $ckist=$empire->fetch1("select fid,savetxt from {$dbtbpre}enewsf where tbname='$newstb' and f='$cf' limit 1"); if(!$ckist['fid']){ exit("參數設置錯誤"); } if(!$ckist['savetxt']){ //字段表 $empire->query("update {$dbtbpre}enewsf set savetxt=1 where tbname='$newstb' and f='$cf' limit 1"); //組合存文本 TogSaveTxtF(1); //更新緩存 GetConfig(1); } } $b=0; $tbname=$fa?$newstb."_data_1":$newstb; $sql=$empire->query("select id,classid,".$cf." from {$dbtbpre}ecms_".$tbname." where id>$start order by id limit ".$changeline); while($r=$empire->fetch($sql)){ $b=1; $newid=$r['id']; $newstext=dp_ReturnChangeNewstextUrl($r[$cf],$r['id']); $empire->query("update {$dbtbpre}ecms_".$tbname." set ".$cf."='$newstext' where id='$newid' limit 1"); } if($b==0) { echo"恭喜您!轉換完畢。"; exit(); } echo"一組數據轉換完畢,正進入下一組 (<font color=red><b>".$newid."</b></font>)......<script>self.location.href='index.php?tochange=1&start=$newid';</script>"; exit();}//返回內容地址function dp_ReturnChangeNewstextUrl($value,$id){ global $public_r,$newstb,$cf; //存放文本 if(strstr($public_r['savetxtf'],",".$newstb.".".$cf.",")) { $truetime=time(); //建立目錄 $thetxtfile=GetFileMd5().$id; $truevalue=MkDirTxtFile(date("Y/md",$truetime),$thetxtfile); //寫放文件 EditTxtFieldText($truevalue,$value); $value=$truevalue; } else{ exit("參數設置錯誤"); } return $value;}?><html><head><title>新聞模型轉文章模型程序</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style>a:link { COLOR: #000000; TEXT-DECORATION: none }a:visited { COLOR: #000000 ; TEXT-DECORATION: none }a:active { COLOR: #000000 ; TEXT-DECORATION: underline }a:hover { COLOR: #000000 ; TEXT-DECORATION:underline }.home_top { border-top:2px solid #4798ED; }.home_path { background:#4798ED; padding-right:10px; color:#F0F0F0; font-size: 11px; }td, th, caption { font-family: "宋體"; font-size: 12px; color:#000000; LINE-HEIGHT: 165%; }.hrLine{MARGIN: 0px 0px; BORDER-BOTTOM: #807d76 1px dotted;}</style></head><body> <p><br> <br> </p> <form method="get" action="index.php" onsubmit="return confirm('確認要執行?');"> <table width="500" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#0472BC"> <tr> <td height="25"><div align="center"><font color="#FFFFFF" size="3"><strong>新聞模型轉文章模型程序</strong></font></div></td> </tr> <tr bgcolor="#FFFFFF"> <td height="50"> <div align="center"> <input type=submit name=ok value="點擊開始轉換"> <input type=hidden name="tochange" value=1> </div></td> </tr> </table></form></body></html>
如果你在之前模板中用php代碼調用過被轉換的字段,則此時需要用函數 GetTxtFieldText 來讀取。
例如,在之前你調用newstext用代碼:
<?=$navinfor['newstext']?>
newstext轉換成存文本之后必調用代碼是:
<?=GetTxtFieldText($navinfor['newstext'])?>
新聞熱點
疑難解答