欢迎光临
我们一直在努力

四 文章类 封装对文章的各种操作(插入数据库、从数据库取出等等)-PHP教程,PHP应用

建站超值云服务器,限时71元/月

<?php
//
// +———————————————————————-+
// | 文章类                                                               |
// +———————————————————————-+
// | copyright (c) 2001 netfish software                                  |
// |                                                                      |
// | author: whxbb(whxbb@21cn.com)                                        |
// +———————————————————————-+
//
// $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(whxbb@21cn.com)
* @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( !@mysql_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( !@mysql_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;
    }
}
?>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 四 文章类 封装对文章的各种操作(插入数据库、从数据库取出等等)-PHP教程,PHP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章