<article.php>
<?
if(!isset($news))
{
header("location: newsarticle.action.php?action=listnews");
return;

}
?>
<!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=utf8" />
<title>无标题文档</title>
<link href="images/css.css" rel="stylesheet" type="text/css">
<script src="../include/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function doAction(a,id){
ids = 0;
if(a=='deleteAll'){
if(confirm('请确认是否删除!')){
$.ajax({
url:'article.action.php',
type: 'POST',
data:'act=delete&id='+getCheckedIds('checkbox'),
success: function(data){
window.location.href = window.location.href;
}
});
}
}
if(a=='delete'){
if(confirm('请确认是否删除!')){
$.ajax({
url:'article.action.php',
type: 'POST',
data:'act=delete&id='+id,
success: function(data){
window.location.href = window.location.href;
}
});
}
}
if(a=='moveAll'){
scid = document.getElementById("selectCid").value;
if(confirm('请确认是否转移!')){
$.ajax({
url:'article.action.php',
type: 'POST',
data:'act=move&scid='+scid+'&id='+getCheckedIds('checkbox'),
success: function(data){
window.location.href = window.location.href;
}
}
);
}
}
}
//全选/取消
function checkAll(o,checkBoxName){
var oc = document.getElementsByName(checkBoxName);
for(var i=0; i<oc.length; i++) {
if(o.checked){
oc[i].checked=true;
}else{
oc[i].checked=false;
}
}
checkDeleteStatus(checkBoxName)
}
//检查有选择的项,如果有删除按钮可操作
function checkDeleteStatus(checkBoxName){
var oc = document.getElementsByName(checkBoxName);
for(var i=0; i<oc.length; i++) {
if(oc[i].checked){
document.getElementById('DeleteCheckboxButton').disabled=false;
document.getElementById('updateCategoryButton').disabled=false;
document.getElementById('selectCid').disabled=false;

return;
}
}
document.getElementById('DeleteCheckboxButton').disabled=true;
document.getElementById('updateCategoryButton').disabled=true;
document.getElementById('selectCid').disabled=true;
}//获取所有被选中项的ID组成字符串
function getCheckedIds(checkBoxName){
var oc = document.getElementsByName(checkBoxName);
var CheckedIds = "";
for(var i=0; i<oc.length; i++) {
if(oc[i].checked){
if(CheckedIds==''){
CheckedIds = oc[i].value;
}else{
CheckedIds +=","+oc[i].value;
}

}
}
return CheckedIds;
}
  function aaa(){
  if(document.getElementById('newtypeid').value=='0'){
    alert('请选择要添加的栏目'); document.getElementById('newtypeid').focus(); return false
  }
}
</script></head><body>
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
  <tr>
    <td valign="top" style="padding:10px;"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="table_head">
        <tr>
          <td height="30">新闻管理 
            &nbsp;&nbsp;&nbsp;</td>
         
        </tr>
      </table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_form">
 <tr>
            <form name="addnews" action="addnews.php" method="GET" onsubmit='return aaa()'>
  <th width="90%" align="right">
<div align="right">
  <select name="newtypeid" id="newtypeid">
    <option value="0">--所有栏目--</option>
    <?php  
$sql = "select * from newtype"; 
$conn = mysql_connect("localhost","root","123456");

$st = mysql_select_db("install",$conn);
mysql_query("set names gbk");
$rs = mysql_query($sql);;
while ($row = mysql_fetch_array($rs, MYSQL_BOTH)) 
{

echo "<option value=".$row[typeid].">".$row["typetitle"]."</option>"; }
mysql_free_result($rs);
mysql_close($conn);

?>
              </select>
          </div></th>
<th width="10%" align="right"><input name="addnews" type="submit" value="添加新闻"></th>
</form>
 </tr>
  </table>   <table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_form">
        <tr>
          <th width="40"><input type="checkbox" name="checkbox11"
value="checkbox" onClick="checkAll(this,'checkbox')"></th>
  <th height="26">ID</th>
          <th height="26">新闻标题</th>
          <th width="150" height="26">发布时间</th>
          <th width="80">所属栏目</th>
          <th width="80" height="26">操作</th>
        </tr>
           <?php
  foreach($news as $new)
  {
  ?>
        <tr onMouseOver="this.className='relow'" onMouseOut="this.className='row'" class="row">
          <td align="center">
          <input type="checkbox" name="checkbox" value="<?php echo $new['newid'];?>" onClick="checkDeleteStatus('checkbox')"></td>
  <td height="26" align="center"><?php echo $new['newid']?></td>
          <td height="26"><?php echo $new['newtitle']?></td>
          <td height="26" align="center"><?php echo $new['newtime']?></td>
          <td align="center"><?php echo $new['typetitle']?></td>
          <td height="26" align="center"><a
href="article.add.php?act=edit&cid=<?php echo $new['typeid'];?>&id=<?php echo $new['newid'];?>"><img
src="images/edit.gif" alt="修改" border="0"></a> <img src="images/del.gif" alt="删除"
onClick="doAction('delete',<?php echo $new['newid'];?>)"
style="cursor: pointer"></td>
        </tr>
          <?php
}
?>
      </table>
      <table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_footer">
        <tr>
          <td height="29" style="text-align: left; padding-left: 10px"><div style="float: left;">
              <input type="button" id="DeleteCheckboxButton" value="批量删除" disabled="disabled" onClick="doAction('deleteAll')">
             </div>
            <div style="float: right; padding-right: 50px"><?php
include_once("pager.inc.php");
?></div></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
-==============================================
=================================================

解决方案 »

  1.   

    <newsarticle.action.php>
    <?php
    $action = $_REQUEST['action'];///TODO: action验证include_once("model/NewsManager.php");
    ///TODO: 可以考虑通过XML来配置下述action,类似Struts2
    switch($action)
    {
    case "addnews":
    addNews();
    break;
    case "listnews":
    listNews();
    break;
    //case "searchbook":
    //searchBook();
    //break;
    default:
    echo "<font color='red'>未知的ACTION!</font>";
    }///ACTIONS:
    function addNews()
    {
    $newtitle = $_REQUEST['newtitle'];
    $newpic = $_REQUEST['newpic'];
    $newcontent = $_REQUEST['newcontent'];
    $name = $_REQUEST['typeid'];

    $bmgr = new NewsManager();
    $result = $bmgr->add($newtitle, $newpic, $newcontent,$name);
    if($result>0)header("Location: result.php?no=addbook200");
    else header("Location: result.php?no=addbook500");
    }
    /*
    function deleteBook()
    {
    }
    function deleteBatchBook()
    {
    $bnos = $_REQUEST['bnos'];
    $bmgr = new BookManager();
    $count = $bmgr->deleteBatch($bnos);
    if($count > 0)header("Location: result.php?no=deletebatchbook200");
    else header("Location: result.php?no=deletebatchbook500");
    }*/
    function listNews()
    {
    $bmgr = new NewsManager();
    $pageSize = 10;
    $recordCount = $bmgr->getRecordCount()-1;
    $curPage = $_REQUEST['pageno']>0?$_REQUEST['pageno']:1;
    $news = $bmgr->showInPage($pageSize,$curPage);

    include("article.php");
    }
    /*function searchBook()
    {
    $keyword = $_REQUEST['keyword'];

    $bmgr = new BookManager();
    $books = $bmgr->queryBookByName($keyword);
    include("searchbook.php");  //用于显示查询结果
    }*/
    ?>
    ===============================
    ============================
    <NewsManager.php>
    <?php
    class NewsManager
    {
    var $conn;
    function __construct()
    {
    include("db_conf.inc.php");
    $this->conn = $conn;
    }
    function add($newtitle,$newpic,$newcontent,$name)
    {
    $sql = "INSERT INTO newtypedetails(newtitle,newpic,newcontent,typid) VALUES(?,?,?,?)";
    $stmt = $this->conn->Prepare($sql);
    $result = $this->conn->Execute($stmt, array($newtitle, $newpic, $newcontent,$name));
    $this->conn->Close();
    if($result)
    {
    if($id = $this->conn->Insert_ID()) return $id; //返回生成的自增号
    else if($count = $this->conn->Affected_Rows())return $count; //返回影响的行数
    else return 200; //返回200
    }
    else return -500;
    }
    /*function delete()
    {

    }
    function queryBookByName($keyword)
    {
    $sql = "SELECT bno,bname,price,pdate FROM books WHERE bname LIKE ?";
    $stmt = $this->conn->Prepare($sql);
    $rs = $this->conn->Execute($stmt, array("%{$keyword}%"));//返回值$rs是ADORecordSet类的对象
    $books = $rs->GetRows();
    return $books;

    }*/
    function showInPage($pageSize, $pageNo)
    {
    $sql = "select * from newtypedetails inner join newtype where newtypedetails.typeid=newtype.typeid";
    $rs = $this->conn->SelectLimit($sql, $pageSize, ($pageNo-1)*$pageSize);
    $news = $rs->GetRows();
    return $news;
    }
    function deleteBatch($bnos)
    {
    if(count($bnos)>0)
    {
    $nos = "";
    foreach($bnos as $no)$nos .= $no.",";
    $nos = rtrim($nos, ','); 
    $sql = "DELETE FROM books WHERE bno IN ({$nos})";
    $result = $this->conn->Execute($sql);
    if($result)return $this->conn->Affected_Rows();
    else return -2; //数据库执行错误
    }
    else return -1; //参数传递错误
    }
    function getRecordCount()
    {
    $sql = "SELECT count(*) FROM books";
    $rs = $this->conn->Execute($sql);
    return $rs->fields[0];
    }
    }

    /***********************
     * 栏目分类下拉框 <option></option>
     *
     * @param int $pid
     * @param int $id
     * @param int $level
     */
    /*function getCategorySelect($select_id=0,$id = 0,$level = 0){
    global $db;
    $category_arr = $db->getList ( "select * from cms_category where pid = " . $id . " order by seq" );
    for($lev = 0; $lev < $level * 2 - 1; $lev ++) {
    $level_nbsp .= " ";
    }
    if ($level++) $level_nbsp .= "┝";
    foreach ( $category_arr as $category ) {
    $id = $category ['id'];
    $name = $category ['name'];
    $selected = $select_id==$id?'selected':'';
    echo "<option value=\"".$id."\" ".$selected.">".$level_nbsp . " " . $name."</option>\n";
    getCategorySelect ($select_id, $id, $level );
    }
    }
    }
    **********************************/
    ?>
    这是一个分类添加,首先在select里选择某个栏目,然后进行添加,添加成功后,所添加的内容属于选择的那个栏目里的信息
    问  : 如何得到select里的id进行添加呢?
      

  2.   

    就是怎么去select的id来进行添加
    <article.php>
    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_form">
     <tr>
                <form name="addnews" action="addnews.php" method="GET" onsubmit='return aaa()'>
      <th width="90%" align="right">
    <div align="right">
      <select name="newtypeid" id="newtypeid">
        <option value="0">--所有栏目--</option>
        <?php  
    $sql = "select * from newtype"; 
    $conn = mysql_connect("localhost","root","123456");

    $st = mysql_select_db("install",$conn);
    mysql_query("set names gbk");
    $rs = mysql_query($sql);;
    while ($row = mysql_fetch_array($rs, MYSQL_BOTH)) 
    {

    echo "<option value=".$row[typeid].">".$row["typetitle"]."</option>"; }
    mysql_free_result($rs);
    mysql_close($conn);

    ?>
                  </select>
              </div></th>
    <th width="10%" align="right"><input name="addnews" type="submit" value="添加新闻"></th>
    </form>
     </tr>
      </table>
    ===========
    ======
    <addnews.php>
    <?php
    //include ("admin.add.php");
    include ("../include/fckeditor/fckeditor.php");
    $name = $_GET["newtypeid"];
    echo $name;
    ?>
    ============
    =========
    <NewsManager.php>
    function add($newtitle,$newpic,$newcontent,$name)
    {
    $sql = "INSERT INTO newtypedetails(newtitle,newpic,newcontent,typid) VALUES(?,?,?,?)";
    $stmt = $this->conn->Prepare($sql);
    $result = $this->conn->Execute($stmt, array($newtitle, $newpic, $newcontent,$name));
    $this->conn->Close();
    if($result)
    {
    if($id = $this->conn->Insert_ID()) return $id; //返回生成的自增号
    else if($count = $this->conn->Affected_Rows())return $count; //返回影响的行数
    else return 200; //返回200
    }
    else return -500;
    }
      

  3.   

    <addnews.php>
    <?php
    //include ("admin.add.php");
    include ("../include/fckeditor/fckeditor.php");
    $name = $_GET["newtypeid"];
    echo $name;
    ?>//这里的 $name 输出对吗??是这样获取的啊,得到的是 分类对应的 id。如果你想获取 名:<select name="newtypeid" id="newtypeid">
    <option value="0">--所有栏目--</option>
    <?php   
    $sql = "select * from newtype";  
    $conn = mysql_connect("localhost","root","123456");$st = mysql_select_db("install",$conn);
    mysql_query("set names gbk");
    $rs = mysql_query($sql);;
    while ($row = mysql_fetch_array($rs, MYSQL_BOTH))  
    {echo "<option value=".$row[typetitle].">".$row["typetitle"]."</option>";//改在这样}
    mysql_free_result($rs);
    mysql_close($conn);?>
      </select>