<?php
include ('admin_global.php');

$cmd = $_GET["cmd"];
if($cmd=="list"){
    $args=$_POST;
    $t_sql="select id from newsinfo where";
//select n.*,u.username,nc.name as newsclassname from newsinfo as n,user as u,newsclass as nc where n.userid=u.id and  newsid=nc.id
    $sql="select n.*,u.username,nc.name as newsclassname from newsinfo as n,user as u,newsclass as nc where n.userid=u.id and  newsid=nc.id and ";
    $search=iconv("utf-8", "gbk", $args[keywords]);
    if($search==null || $search==""){
        $sql.=" 1=1 ";
        $t_sql.=" 1=1 ";
    }else{
        $sql.=" title like '%$search%' ";
        $t_sql.=" title like '%$search%' ";
    }

    $total=mysql_num_rows($db->query($t_sql));  //计算结果集总条数用于分页

    if($args[limit]!=""){
        if($args[start]!="" || $args[start]!=0){
         $start=$args[start];
        }else{
            $start=0;
        }
         $sql.="order by n.id desc limit ".$start.",$args[limit]";
    }
    $query=$db->query($sql);
    while ($row=$db->fetch_array($query)) {
        $i=0;
        $data.="{id:".$row[id].",newsid:$row[newsid],newsclassname:'".iconv("GBK", "utf-8", $row[newsclassname])."',title:\"".iconv("GBK", "utf-8", $row[title])."\",content:\"".iconv("GBK", "utf-8", $row[content])."\",posttime:'".date('m-d H:i:s',$row[posttime])."',userid:$row[userid],username:'$row[username]',keywords:'".iconv("GBK", "utf-8", $row[keywords])."'}";
        if($i<  (count($row)-1)){
           $data.=",";
        }
        $i++;
    }
    $str="{rowCount:".$total.",result:[".$data."]}";
    echo $str;
}

if($cmd=="save"){
    $args=$_POST;
    $title=iconv("utf-8", "gbk", $args[title]);
    $content=iconv("utf-8", "gbk", $args[content]);
    $keywords=iconv("utf-8", "gbk", $args[keywords]);
    $sql="insert into newsinfo values(null,$args[newsid],'$title','$content','".mktime()."',$uid,'$keywords')";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'新增成功'}";
    }else{
        echo "{failure:true,info:'新增失败'}";
    }
}

if($cmd=="update"){
    $args=$_POST;
    $title=iconv("utf-8", "gbk", $args[title]);
    $content=iconv("utf-8", "gbk", $args[content]);
    $keywords=iconv("utf-8", "gbk", $args[keywords]);
    $sql="update newsinfo set newsid=$args[newsid],title='$title',content='$content',posttime='".mktime()."',userid=1,keywords='$keywords' where id=$args[id]";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'修改成功'}";
    }else{
        echo "{failure:true,info:'修改失败'}";
    }
}

if($cmd=="remove"){
    $sql="delete from newsinfo where id=$_POST[id]";
    $query=$db->query($sql);
    if($query){
        echo "{success:true,info:'删除成功'}";
    }else{
        echo "{failure:true,info:'删除失败'}";
    }
}
?>
