【问题标题】:php oop programming: building a classphp oop 编程:构建类
【发布时间】:2013-12-16 02:25:58
【问题描述】:

我正在考虑建立我的第一个真正的班级,我玩过一些零碎的东西,但现在是时候真正尝试一下了:)

我想要做的是有一个表单类来处理我所有的表单提交,检查输入的数据并返回错误消息或成功消息。

所以这是我的一种形式,(我在 1 页上有 5 个)

<form action="include/mform.php" method="post" name="business_listing">
    <input name="biz_name" type="text" value="Business or Place Name" />
    <select name="biz_department">
    <option value="">Business Sector</option>
        <?php
        $query = $user->database->query("SELECT * FROM tbl_sectors");
        while($row=$user->database->fetchArray($query))
        {
            $id = $row['sectorID'];
            $dept = $row['sectorName'];
            echo "<option value='$id'>$dept</option>";
        }?>
    </select>
    <input name="biz_address1" type="text" value="Street Name" />
                                <select name="job_location">
    <option value="">Location</option>
        <?php
        $query = $user->database->query("SELECT * FROM tbl_places");
        while($row=$user->database->fetchArray($query))
        {
            $id = $row['placeID'];
            $dept = $row['placeName'];
            echo "<option value='$id'>$dept</option>";
        }?>
    </select>

    <input name="biz_phone" type="text" value="Contact Number" />
    <input name="businessSubmit" type="submit" value="Submit" />
</form>
</div>

每个表单的操作都设置为包含我的类的 include/mform.php。在类中,它所做的第一件事就是检查提交的表单,然后检查已提交的数据并对其进行必要的处理

我的问题是,一旦我的班级知道提交了哪个表单,检查提交数据的最佳方法是什么?我应该在函数中创建变量以获取所有发布数据并从那里获取它,还是应该在实际函数中将它们作为参数传递? ,还是有关系?

这是我当前的类文件,它有点裸露的atm

class Mform
{
    private $values = array();  //Holds submitted form field values
    private $errors = array();  //Holds submitted form error messages
    private $num_errors;   //The number of errors in submitted form

    public function __construct()
    {

        if(isset($_POST['businessSubmit']))
        {
            $this->chkBusiness();
        }

        if(isset($_POST['jobSubmit']))
        {
            $this->chkJob();
        }

        if(isset($_POST['accommodationSubmit']))
        {
            $this->chkAccommodation();
        }

        if(isset($_POST['tradeSubmit']))
        {
            $this->chkTrade();
        }

        if(isset($_POST['eventSubmit']))
        {
            $this->chkEvent();
        }

    }

    public function chkBusiness()
    {
        $field = "business";

    }

    public function chkJob()
    {
        return "job";
    }

    public function chkAccommodation()
    {
        return "accommodation";
    }

    public function chkTrade()
    {
        return "trade";
    }

    public function chkEvent()
    {
        return "event";
    }

    /**
    * setValue - Records the value typed into the given
    * form field by the user.
    */
    public function setValue($field, $value)
    {
        $this->values[$field] = $value;
    }

    /**
    * setError - Records new form error given the form
    * field name and the error message attached to it.
    */
    public function setError($field, $errmsg)
    {
        $this->errors[$field] = $errmsg;
        $this->num_errors = count($this->errors);
    }

    /**
    * value - Returns the value attached to the given
    * field, if none exists, the empty string is returned.
    */
    public function value($field)
    {
        if(array_key_exists($field,$this->values))
        {
            return htmlspecialchars(stripslashes($this->values[$field]));
        }
        else
        {
            return "";
        }
    }

    /**
    * error - Returns the error message attached to the
    * given field, if none exists, the empty string is returned.
    */
    public function error($field)
    {
        if(array_key_exists($field,$this->errors))
        {
            return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>";
        }
        else
        {
            return "";
        }
    }

    /* getErrorArray - Returns the array of error messages */
    public function getErrorArray()
    {
        return $this->errors;
    }

}

/* Initialize mform */
$mform = new Mform();

大多数单独的函数只是返回“word”作为占位符,所以我不会忘记在以后执行该函数。

这就是我想为每个单独的表单功能做的事情

    public function chkBusiness()
{
    $field = "business";
    $name = $_POST['biz_name'];// all need to be sanitized!!
    $dept = $_POST['biz_dept'];
    $address = $_POST['biz_address'];
    $location = $_POST['biz_location'];
    $phone = $_POST['biz_phone'];

    //start checking the input
    if(!$name || strlen($name = trim($name)) == 0)
    {
        $this->mform->setError($field, "* Name not entered");  
    }
    ...
    ...
}

任何帮助将不胜感激

卢克

【问题讨论】:

  • 我不会在该课程中直接使用$_POST。这是测试的毒药。最简单的改进是将 post 值作为构造函数参数传递
  • 感谢您的帮助 hek2mgl
  • 不客气。我可以告诉你很多关于如何改进的事情,但我认为你自己尝试一些东西是件好事。 (+1)有耐心,继续玩代码,你就会进入“大师级别”.. :) 大多数“大师级别”是通过从你犯的错误中学习来获得的。在这一点上不要太在意完美。继续玩吧..这很好!

标签: php class oop


【解决方案1】:

我为数据库中的每个表生成一个类;生命太短,无法为每个数据库表实际编写该功能!

每个字段都有它自己的对象,包含它的值、类型、最大长度等,并且这些字段也被添加到一个数组中,所以我可以用它们做一些非常酷的事情。

每个表类都扩展了一个更大的类,它允许我插入、更新、删除和显示为表和表单...我可以覆盖任何非标准的函数,尽管这种情况非常罕见。

迭代大约 25000 条大小合适的记录的性能开销约为 0.001 毫秒。

例如,我生成文档记录数据表的代码如下所示:

    //Create object
    $objDocument = new cls__CMS_Document();

    //Set the fields you want to display in the datatable
    $objDocument->objID->Active(true);
    $objDocument->objDocument_Name->Active(true);
    $objDocument->objAuthorID->Active(true);        
    $objDocument->objVersion->Active(true);        
    $objDocument->objDate_Created->Active(true);
    $objDocument->objDate_Last_Edited->Active(true);

    //Include a hyperlink from the ID field
    $objDocument->objID->HyperLink('/drilldown.php');
    $objDocument->objID->Post(true);

    //Pass a field through a formatting function
    $objDocument->objAuthorID->OutputFunction('getAuthorFromID');

    $result .= $objDocument->displayTable($sqlConditions);

    unset ($objDocument);

最好的一点:自动完成的每一步都有效:) 所以你输入 $objDocument-> 并且所有的方法和属性都会弹出,包括所有的字段对象,这样你就不会拼错它们。

如果我得到足够的兴趣(投票),我会将整个内容发布到网上。

虽然在自己制作时,这应该是值得深思的。

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多