写成类库的形式。比如连接数据库的时候用一个封装类来实现。
具体的就是MVC的实现了吧。
我最近也在学习怎么用MVC实现PHP的网站结构
我把源码发表一下,大家看看。请多指教.

解决方案 »

  1.   

    网站思路:把每个功能模块用类封装,用控制器实现调用
    用到的类:mysql.inc.php //连接数据库的类
             item.inc.php //模型,实现对数据库记录的添加,删除,取得记录等功能。
             item_view.inc.php //视图.将取得的数据与HTML绑定。并输出。
             index.php        //最后是控制器了。将输出变量与模板整合,显示结果。
    <?php
    //index page use /template/index.htm as its HTML template
    require_once("config.inc.php");
    require_once("class/template.inc.php");
    require_once("class/mysql.inc.php");
    require_once("class/item.inc.php");
    require_once("class/member.inc.php");session_start();
    if (!isset($HTTP_SESSION_VARS["member"]))
    { // the user has not login and can't post message
    $msg  = "请您在发布信息之前先登录.";
    $msg .= "<br><br>如果您还没有注册,点击<a href='reg.php'><font color='red'>这里</font></a>注册";
    $tpl = new template($TEMPLATE_DIR);
    $tpl->set_file("error", "login.htm");
    $tpl->set_var("data", $msg);
    $tpl->pparse("out", "error");
    exit();
    }
    if (isset($HTTP_POST_VARS["action"]) && $HTTP_POST_VARS["action"] == "post")
    { //form has been submitted
    //initialize the class object
    $m       = $HTTP_SESSION_VARS['member'];
    $db      = & new mysql();
    $item    = & new item($db);
    $id      = "";
    $brand   = $HTTP_POST_VARS["brand"];
    $type    = $HTTP_POST_VARS["type"];
    $others  = $HTTP_POST_VARS["others"];
    $mode    = $HTTP_POST_VARS["mode"];
    $poster  = $m->user_name;
    $postime = date("Y-m-d H:i:s");
    $res     = $item->add($id, $brand, $type, $poster, $postime, $others, $mode);
    if ($res)
    { // add item success
    $msg  = "<b>发布成功!</b>.";
    $mode=='sale'?$info="供方信息":$info="求购信息";
    $msg .= "<br><br>您可以到".$info."里查看您刚才发表的信息.<br>";
    $msg .= "<br><a href='index.php'>返回首页</a>";
    $tpl  = new template($TEMPLATE_DIR);
    $tpl->set_file("error", "msg.htm");
    $tpl->set_var("msg", $msg);
    $tpl->pparse("out", "error");
    }
    else
    { // add item failed
    $msg  = "<b>发布失败!</b>.";
    $msg .= "<br><br>服务器在处理数据时出错,请重新发布您的信息.<br>";
    $msg .= "<br><a href='index.php'>返回首页</a>";
    $tpl  = new template($TEMPLATE_DIR);
    $tpl->set_file("error", "msg.htm");
    $tpl->set_var("data", $msg);
    $tpl->pparse("out", "error");
    }
    }
    else
    { // no submit ,echo the post form
    //output use template
    $tpl  = new template($TEMPLATE_DIR);
    $tpl->set_file('post', 'post.htm');
    $tpl->set_var(array('bulletin' => nl2br($BULLETIN_DATA),
    'help'     => nl2br($HELP_DATA),
    ));
    $tpl->pparse('output', 'post');
    }?>
      

  2.   

    上面的代码发错了,下面的是:<?php
    /***************************
    *** mysql database driver***
    *** create by ice_berg16 ***
    *** at 2003.12.10        ***
    ***************************///make sure all sytax error are reported.
    //error_reporting(E_ALL);class mysql
    {
    var $host     = "";
    var $user     = "";  
    var $pwd      = "";
    var $db_name  = "";
    var $link_id  = 0;
    var $query_id = 0;
    var $errno    = 0;
    var $error    = "";
    var $record   = array();
    //function list

    /* constructor
    ** this function will aoto connect the db
    */
    function mysql()
    {
    $this->host    = "localhost";
    $this->user    = "root";
    $this->pwd     = "format";
    $this->db_name = "mysite";
    $this->connect();//take out this line if you want to
     //connect the db yourself
    }
    /* function connect
    ** return 0 :error 
    ** link_id :ok 
    */
    function connect($host = "", $user = "", $pwd = "", $db_name = "")
    {
    if ("" == $host)
    $host = $this->host;
    if ("" == $user)
    $user = $this->user;
    if ("" == $pwd)
    $pwd = $this->pwd;
    if ("" == $db_name)
    $db_name = $this->db_name;
    //now connect to the database
    $this->link_id = mysql_pconnect($host, $user, $pwd);
    if (!$this->link_id)
    {
    $this->halt();
    return 0;
    }
    if (!mysql_select_db($db_name, $this->link_id))
    {
    $this->halt();
    return 0;
    }
    return $this->link_id;
    }

    /** function query 
    ** return 0 : error 
    */
    function query($sql)
    {
    if (!$this->link_id)
    {
    $this->halt("no connect is active.\n");
    return 0;
    }
    if ("" == $sql)
    {
    $this->halt("the query string is empty.\n");
    return 0;
    }
    $this->query_id = mysql_query($sql, $this->link_id);
    if (!$this->query_id)
    {
    $this->halt();
    return 0;
    }
    return $this->query_id;
    }

    /* function get_record
    ** return 0 : error
    ** return one record
    */
    function get_record()
    {
    if (!$this->link_id || !$this ->query_id)
    {
    $this->halt("no query record.\n");
    return 0;
    }
    $this->record = mysql_fetch_array($this->query_id);
    return $this->record;

    /* function data()
    ** return 0 : error
    ** return data in the record
    */
    function data($id)
    {
    return $this->record[$id];
    }
    /* fucntion total_record()
    ** get the number of the record
    */
    function total_record()
    {
    return mysql_num_rows($this->query_id);
    }
    /* get the number of the fields
    */
    function num_fields()
    {
    return mysql_num_fields($this->query_id);
    }
    /* if the operate is success
    ** can be used afer insert,update,delete etc.
    ** can not use when query is "select"
    */
    function success()
    {
    if (mysql_affected_rows($this->link_id))
    return 1;
    else
    {
    $this->halt("operate failed.\n");
    return 0;
    }
    }
    /* function halt
    ** process all error and echo the error
    */
    function halt($err_msg="")
    {
    if ("" == $err_msg)
    {
    $this->errno = mysql_errno();
    $this->error = mysql_error();
    echo "<b>mysql error:<b><br>";
    echo $this->errno.":".$this->error."<br>";
    }
    else
    {
    echo "<b>mysql error:<b><br>";
    echo $err_msg."<br>";
    }
    }
    }
    ?>