«
封装对文章的各种操作(插入数据库、从数据库取出等)

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  <code><?php<br />//<br />// +----------------------------------------------------------------------+<br />// | 文章类                               &#160;|<br />// +----------------------------------------------------------------------+<br />// | Copyright (c) 2001 NetFish Software                 |<br />// |                                   |<br />// | Author: whxbb(whxbb@21cn.com)                    |<br />// +----------------------------------------------------------------------+<br />//<br />// $Id: whxbb_article.class.php,v 0.1 2001/8/11 22:18:13 yf Exp $<br />//<br />// 禁止直接访问该页面<br />if (basename($HTTP_SERVER_VARS['PHP_SELF']) == "whxbb_article.class.php") {<br />  header("HTTP/1.0 404 Not Found");<br />}<br />/**<br />* 文章类<br />* Purpose<br />* 封装对文章的各类操作<br />*<br />*<br />* @author : whxbb(whxbb@21cn.com)<br />* @version : 0.1<br />* @date  : 2001/8/1<br />*/<br />class WHXBB_Article extends WHXBB<br />{<br />  /** 分页对象 */<br />  var $pager;<br />  function Article()<br />  {<br />    $this->WHXBB();<br />  }<br />  /**<br />  &#160;* 文章写入数据库<br />  &#160;* @param $title 文章标题<br />  &#160;* @param $author 文章作者<br />  &#160;* @param $content 文章内容<br />  &#160;* @return 操作出错:一个WHXBB_Error对象 成功:true<br />  &#160;* @access public<br />  &#160;*/<br />  function Insert($title, $author, $content)<br />  {<br />    new WHXBB_Debug("Insert() Start");<br />    // 处理传入的参数<br />    WHXBB::OperateString(&$title, 'in');<br />    WHXBB::OperateString(&$author, 'in');<br />    WHXBB::OperateString(&$content, 'in');<br />    $sql = "insert into article(title,author,content) values('$title','$author','$content')";<br />    if( !@mysql_query($sql, $this->_conn) )<br />    {<br />      return new WHXBB_Error("Insert() Failed.($sql)", 1021);<br />    }<br />    new WHXBB_Debug("Insert() Completed");<br />    return true;<br />  }<br />  /**<br />  &#160;* 删除指定的记录<br />  &#160;* @param $id 要删除记录的id<br />  &#160;* @return 操作出错:一个WHXBB_Error对象 成功:true<br />  &#160;* @access public<br />  &#160;*/<br />  function Del($id)<br />  {<br />    new WHXBB_Debug("Del($id) Start");<br />    $sql = "delete from article where id=$id)";<br />    if( !@mysql_query($sql, $this->_conn) )<br />    {<br />      return new WHXBB_Error("Del() Failed.($sql)", 1024);<br />    }<br />    new WHXBB_Debug("Dle($id) Completed");<br />    return true;<br />  }<br />  /**<br />  &#160;* 得到文章的总数<br />  &#160;* @param $condition   查询条件<br />  &#160;* @return 操作出错:一个WHXBB_Error对象 成功:true<br />  &#160;* @access public<br />  &#160;*/<br />  function GetCount($condition = '')<br />  {<br />    new WHXBB_Debug("GetCount() Start");<br />    $sql = "select count(id) from article where 1=1 $condition";<br />    if( !$result = @mysql_query($sql, $this->_conn))<br />    {<br />      return new WHXBB_Error("GetCount() Failed.($sql)", 1000);<br />    }   &#160;<br />    list($count) = @mysql_fetch_array($result);<br />    @mysql_free_result($result);<br />    new WHXBB_Debug("GetCount() Completed");<br />    return $count;<br />  }<br />  /**<br />  &#160;* 得到某一篇文章的所有字段信息<br />  &#160;* @param $id 文章id号<br />  &#160;* @return 操作出错:一个WHXBB_Error对象 成功:返回一个关联数组 找不到信息:返回0<br />  &#160;* @access public<br />  &#160;*/<br />  function GetInfo($id )<br />  {<br />    new WHXBB_Debug("GetInfo($id) Start");<br />    $sql = "select id, title, content, author from article where id=$id";<br />    $result = @mysql_query($sql, $this->_conn);<br />    if( !$result)<br />      return new WHXBB_Error("GetInfo($id) Failed.($sql)", 1002);<br />    if(@mysql_num_rows($result) == 0)<br />      return 0;<br />    $info = @mysql_fetch_array($result);<br />    while (list($var, $key) = each($info))<br />    {<br />      WHXBB::OperateString(&$info[$var], 'out');<br />    }<br />    reset($info);<br />    @mysql_free_result($result);<br />    new WHXBB_Debug("GetInfo($id) Completed");<br />    return $info;<br />  }<br />  /**<br />  &#160;* 得到所有author为指定作者名的所有记录<br />  &#160;* @param $items 每页显示条数,如果为0则表示取出所有记录<br />  &#160;* @param page &#160;当前页码<br />  &#160;* @param author 作者名<br />  &#160;* @param $orderBy 排序方式<br />  &#160;* @return 操作出错:一个WHXBB_Error对象 成功:返回一个数组 找不到信息:返回0<br />  &#160;* @access public<br />  &#160;*/<br />  &#160;function GetNInfoByAuthor($items, $page, $author, $orderBy = 'order by id desc')<br />  &#160;{<br />    WHXBB::OperateString(&$author, 'in');<br />    $condition = " and author='$author' ";<br />    $result = $this->GetNInfo($items, $page, $condition, $orderBy);<br />    return $result;<br />  &#160;}<br />  &#160;}<br />  /**<br />  &#160;* 列出所有记录<br />  &#160;* @param $items 每页显示条数,如果为0则表示取出所有记录<br />  &#160;* @param $page 当前页码<br />  &#160;* @param $condition 查询条件<br />  &#160;* @param $orderBy 排序方式<br />  &#160;* @return 操作出错:一个WHXBB_Error对象 成功:返回一个二维数组 找不到信息:返回0<br />  &#160;* @access public<br />  &#160;*/<br />  function GetNInfo($items, $page, $condition = '', $orderBy = 'order by id desc')<br />  {<br />    new WHXBB_Debug("GetNInfo() Start");<br />    $limit = '';<br />    //取记录总数<br />    $infoCount = $this->GetCount($condition);<br />    if ($infoCount == 0)<br />      return 0;<br />    if ($items != 0)<br />    {<br />     &#160;// 新建一个分页器<br />      $this->pager = new Pager($infoCount, $items, $page);<br />      $startPos  = $this->pager->startPos;<br />      $limit = " limit ".$startPos.", ".$items;<br />    }<br />    $sql = "select id, title, author from article where 1=1 $condition $orderBy $limit";<br />    $result = @mysql_query($sql, $this->_conn);<br />    if( !$result )<br />      return new WHXBB_Error("GetNInfo() Failed.($sql)", 1001);<br />    if(@mysql_num_rows($result) == 0)<br />      return 0;<br />    $i = 0;<br />    while ($arr = @mysql_fetch_array($result))<br />    {<br />      while(list($var, $key) = each($arr))<br />      {<br />        WHXBB::OperateString(&$arr[$var], 'out');<br />      }<br />      reset($arr);<br />      $info[$i]      = $arr;<br />      $i++;<br />    }<br />    @mysql_free_result($result);<br />    new WHXBB_Debug("GetNInfo() Completed");<br />    return $info;<br />  }<br />}<br />?></code></p>