首页 > 开发 > PHP > 正文

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

2024-05-04 22:55:56
字体:
来源:转载
供稿:网友
<?php
//
// +----------------------------------------------------------------------+
// | 文章类                                                               |
// +----------------------------------------------------------------------+
// | copyright (c) 2001 netfish software                                  |
// |                                                                      |
// | author: whxbb([email protected])                                        |
// +----------------------------------------------------------------------+
//
// $id: whxbb_article.class.php,v 0.1 2001/8/11 22:18:13 yf exp $
//

// 禁止直接访问该页面
if (basename($http_server_vars['php_self']) == "whxbb_article.class.php") {
    header("http/1.0 404 not found");
}


/**
* 文章类
* purpose
*  封装对文章的各类操作
*
*
* @author  : whxbb([email protected])
* @version : 0.1
* @date    :  2001/8/1
*/

class whxbb_article extends whxbb
{
    /** 分页对象 */
    var $pager;

    function article()
    {
        $this->whxbb();
    }
    /**
     * 文章写入数据库
     * @param $title 文章标题
     * @param $author 文章作者
     * @param $content 文章内容
     * @return 操作出错:一个whxbb_error对象 成功:true
     * @access public
     */
    function insert($title, $author, $content)
    {
        new whxbb_debug("insert() start");

        // 处理传入的参数
        whxbb::operatestring(&$title, 'in');
        whxbb::operatestring(&$author, 'in');
        whxbb::operatestring(&$content, 'in');

        $sql = "insert into article(title,author,content) values('$title','$author','$content')";
        if( [email protected]_query($sql, $this->_conn) )
        {
            return new whxbb_error("insert() failed.($sql)", 1021);
        }
        new whxbb_debug("insert() completed");
        return true;
    }
    /**
     * 删除指定的记录
     * @param $id 要删除记录的id
     * @return 操作出错:一个whxbb_error对象 成功:true
     * @access public
     */
    function del($id)
    {
        new whxbb_debug("del($id) start");

        $sql = "delete from article where id=$id)";
        if( [email protected]_query($sql, $this->_conn) )
        {
            return new whxbb_error("del() failed.($sql)", 1024);
        }
        new whxbb_debug("dle($id) completed");
        return true;
    }
    /**
     * 得到文章的总数
     * @param $condition      查询条件
     * @return 操作出错:一个whxbb_error对象 成功:true
     * @access public
     */
    function getcount($condition = '')
    {
        new whxbb_debug("getcount() start");
        $sql = "select count(id) from article where  1=1 $condition";
        if( !$result = @mysql_query($sql, $this->_conn))
        {
            return new whxbb_error("getcount() failed.($sql)", 1000);
        }        
        list($count) = @mysql_fetch_array($result);
        @mysql_free_result($result);
        new whxbb_debug("getcount() completed");
        return $count;
    }

    /**
     * 得到某一篇文章的所有字段信息
     * @param $id 文章id号
     * @return 操作出错:一个whxbb_error对象 成功:返回一个关联数组 找不到信息:返回0
     * @access public
     */
    function getinfo($id )
    {
        new whxbb_debug("getinfo($id) start");
        $sql = "select  id, title, content, author from article where id=$id";
        $result = @mysql_query($sql, $this->_conn);
        if( !$result)
            return new whxbb_error("getinfo($id) failed.($sql)", 1002);

        if(@mysql_num_rows($result) == 0)
            return 0;

        $info = @mysql_fetch_array($result);
        while (list($var, $key) = each($info))
        {
            whxbb::operatestring(&$info[$var], 'out');
        }
        reset($info);
        @mysql_free_result($result);
        new whxbb_debug("getinfo($id) completed");
        return $info;
    }

    /**
     * 得到所有author为指定作者名的所有记录
     * @param $items 每页显示条数,如果为0则表示取出所有记录
     * @param page   当前页码
     * @param author 作者名
     * @param $orderby 排序方式
     * @return 操作出错:一个whxbb_error对象 成功:返回一个数组 找不到信息:返回0
     * @access public
     */
     function getninfobyauthor($items, $page, $author, $orderby = 'order by id desc')
     {
        whxbb::operatestring(&$author, 'in');
        $condition = " and author='$author'  ";
        $result = $this->getninfo($items, $page, $condition, $orderby);
        return $result;
     }


     }
    /**
     * 列出所有记录
     * @param $items 每页显示条数,如果为0则表示取出所有记录
     * @param $page  当前页码
     * @param $condition 查询条件
     * @param $orderby 排序方式
     * @return 操作出错:一个whxbb_error对象 成功:返回一个二维数组 找不到信息:返回0
     * @access public
     */
    function getninfo($items, $page, $condition = '', $orderby = 'order by id desc')
    {
        new whxbb_debug("getninfo() start");
        $limit = '';
        //取记录总数
        $infocount = $this->getcount($condition);
        if ($infocount == 0)
            return 0;

        if ($items != 0)
        {
           // 新建一个分页器
            $this->pager = new pager($infocount, $items, $page);
            $startpos    = $this->pager->startpos;
            $limit = " limit ".$startpos.", ".$items;
        }
        $sql = "select  id, title, author from article where 1=1 $condition $orderby $limit";

        $result = @mysql_query($sql, $this->_conn);
        if( !$result )
            return new whxbb_error("getninfo() failed.($sql)", 1001);

        if(@mysql_num_rows($result) == 0)
            return 0;
        $i = 0;
        while ($arr = @mysql_fetch_array($result))
        {
            while(list($var, $key) = each($arr))
            {
                whxbb::operatestring(&$arr[$var], 'out');
            }
            reset($arr);
            $info[$i]            = $arr;
            $i++;
        }
        @mysql_free_result($result);
        new whxbb_debug("getninfo() completed");
        return $info;
    }
}
?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表