工作中文章转发到另一个栏目下的方法
1、思路:Mysql update语句,将原文章所属目录id号更新为新的目录id
2、需要修改的程序:
(1)在admin目录中的news模版页面中的index.phtml增加转发按钮,代码:
<a href="<?php echo $this->baseUrl;?>/admin/news/trans/?type=trans&id=<?php echo $info['ID'];?>">转发</a>
(1)在admin目录中的news控制器中增加trans控制器,和edit控制器类似,代码:
/*********文章转发*********/ function transAction() { $db = Zend_Registry::get("dbAdapter"); $auth = Zend_Auth::getInstance(); if( $auth->hasIdentity() ) { $type = $this->_request->getParam("type"); $id = $this->_request->getParam("id"); if( $type == 'trans' ) { //类别 $sqlCategory = $db->quoteInto('SELECT CID,CNAME,CPATH,CDEPTH FROM SYS_CLASS WHERE FLAGS = ? ORDER BY CORDERS ASC',1); $queryCategory = $db->query($sqlCategory); $resultCategory = $queryCategory->fetchAll(); $this->view->category = $resultCategory; //print_r($resultCategory);exit; //修改的内容 $queryInfo = $db->query('SELECT * FROM SYS_NEWS WHERE ID = :param', array('param'=>$id)); $resultInfo = $queryInfo->fetchAll(); //print_r($resultInfo);exit; for( $i = 0; $i < count($resultCategory); $i++ ) { if( $resultCategory[$i]['CID'] == $resultInfo[0]['CLASSID'] ) { $cname = $resultCategory[$i]['CNAME']; } } $this->view->cname = $cname; $this->view->cid = $resultInfo[0]['CLASSID']; $this->view->Info = $resultInfo[0]; $this->render(); }elseif( $type == 'update' ){ //查询原有文章数据 $ID = @$_POST['ID']; $sql = ""; $sql = "SELECT * FROM SYS_NEWS WHERE ID = $ID"; $query = ''; $query = $db->query($sql); $result = ''; $result = $query->fetchAll(); //$resultInfo = $queryInfo->fetchAll(); //print_r($result);exit; //把新数据插入到新栏目 //把原有数据插入到新栏目,只改变CLASSID,其它不变 $sqlInsert = ""; $sqlInsert = "insert into SYS_NEWS set CLASSID = '".$_POST['CLASSID']."', TYPE = '".$result[0]['TYPE']."', TITLE = '".$result[0]['TITLE']."', CONTENT = '".$result[0]['CONTENT']."', AUTHOR = '".$result[0]['AUTHOR']."', USERID = '".$result[0]['USERID']."', ADDTIME = '".$result[0]['ADDTIME']."', ISCHECKED = '".$result[0]['ISCHECKED']."', CHECKUSER = '".$result[0]['CHECKUSER']."', IMAGES = '".$result[0]['IMAGES']."', CLICKS = '".$result[0]['CLICKS']."'"; //print_r($sqlInsert);exit; $queryInsert = ""; $queryInsert = $db->query($sqlInsert); /* //移除不要的数组值 function key_compare_func($key1, $key2) { if($key1 == $key2) { return 0; } return ($key1 > $key2) ? 1 : -1; } $array1 = $_POST; $array2 = array('Submit2'=>1,'type'=>2,'ID'=>3); $datas = array_diff_ukey($array1, $array2, 'key_compare_func'); */ if( $queryInsert ) { echo '<div align="center" title="提示信息框"> <span style="BACKGROUND:#EEEEFF;COLOR:#FF6633;margin: 10px;border:1px dotted #FF6633;font-weight:bold;padding:8px;width=140"> 文章转发成功!</span> </div><br><center><input type="button" class="BigButton" value="返回" onclick="location.href=\''.$this->_request->getBaseUrl().'/admin/news/index/class/'.$_POST['CLASSID'].'/page/1\';"></center>'; }else{ echo '<script>alert("更新失败!");history.back(-1);</script>'; } exit; }else{ echo '<script language="javascript">alert("权限错误!");history.back(-1);</script>'; exit; } }else{ $url = $this->_request->getBaseUrl()."/admin"; echo "<script language='javascript'>top.location.href='".$url."'</script>"; exit; } }
(2)在admin目录下的new模版页面中增加trans.phtml模版页面,和edit.phtml类似,删掉不必要的代码:
<!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=gb2312" /> <title><?php echo $this->title;?></title> <link href="/public/styles/style.css" rel="stylesheet" type="text/css" /> <script language="javascript" src="/public/scripts/jquery.js"></script> <script charset="utf-8" src="/public/kindeditor/kindeditor.js"></script> <script charset="utf-8" src="/public/kindeditor/lang/zh_CN.js"></script> <script language="javascript"> var editor; KindEditor.ready(function(K) { editor = K.create('textarea[name="CONTENT"]', { allowFileManager : true }); }); $(document).ready(function(){ sel("<?php echo $this->Info['TYPE'];?>") }); function sel(val) { if( val == "0" ) { document.getElementById("MyTable").rows[3].style.display = "none"; document.getElementById("MyTable").rows[4].style.display = ""; document.getElementById("MyTable").rows[5].style.display = ""; document.getElementById("MyTable").rows[6].style.display = ""; document.getElementById("MyTable").rows[13].style.display = ""; document.getElementById("MyTable").rows[14].style.display = ""; }else if( val == "1" ){ document.getElementById("MyTable").rows[3].style.display = ""; document.getElementById("MyTable").rows[4].style.display = "none"; document.getElementById("MyTable").rows[5].style.display = "none"; document.getElementById("MyTable").rows[6].style.display = "none"; document.getElementById("MyTable").rows[13].style.display = "none"; document.getElementById("MyTable").rows[14].style.display = "none"; } } function chkBox(obj) { if( obj.TITLE.value == '' ) { alert("标题不能为空!"); obj.TITLE.focus(); return false; } } </script> </head> <body id="right" class="main1"> <div class="ttitle"> <h1>【<?php echo $this->title;?>】</h1> </div> <div class="linebox botm01"> </div> <form name="sortFrom" id="sortFrom" method="post" action="<?php echo $this->baseUrl;?>/admin/news/trans" onsubmit="return chkBox(this);" enctype="multipart/form-data"> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tablethree" name="MyTable" id="MyTable"> <tr> <td width="10%"><strong>栏目名称</strong></td> <td width="90%"> <select name="CLASSID" id="CLASSID"> <?php foreach($this->category as $category):?> <?php $preFix = ''; if( $category['CDEPTH'] > 1 ) { for($ii = 0; $ii < $category['CDEPTH']-1; $ii++) { $nbsp = ''; for( $iii = 0; $iii <= $ii; $iii++ ) { $nbsp .= ' '; } $preFix .= "—"; } $preFix = $nbsp.'|'.$preFix; } ?> <?php if($category['CID'] == $this->Info['CLASSID']):?> <option value="<?php echo $category['CID'];?>" selected><?php echo $preFix.$category['CNAME'];?></option> <?php else:?> <option value="<?php echo $category['CID'];?>"><?php echo $preFix.$category['CNAME'];?></option> <?php endif;?> <?php endforeach;?> </select> </td> </tr> <tr> <td colspan="2"> <input type="hidden" name="type" value="update"> <input type="hidden" name="ID" value="<?php echo $this->Info['ID'];?>"> <input name="Submit2" type="submit" class="btn2" value="提交" /> <input name="Submit2" type="button" class="btn2" value="返回" onclick="javascript:history.back(-1);"/></td> </tr> </table> </form> </body> </html>
3、总结:这样写肯定比较复杂,简单的方法应该直接用update语句,如下
1 upate SYS_NEWS set CLASSID = $_POST['CLASSID'] where ID = $_POST['ID'];
但是这样提交的数据只有classid,其它的标题,内容等因为在模版页面删去,所以提交时为空,肯定是不对的,还没有想好解决的方法,暂时就用上面已经成功的吧。