Php oop programming: creating a class

I look at creating my first real class, I played with beats and beans, but its time to try for real :)

what I'm trying to do is to have a form class that processes all my forms, validates the input and returns it with an error message or a successful message.

so here is one of my forms (I have 5 of them on 1 page)   

<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>

for each of the actions of the form set to /mform.php, which contains my class. inside a class, one of the first things it does is check to see which form has been submitted, and the idea is to check the data that has been submitted and do something necessary with them.

, , , ? , , ?, ?

, 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();

"" , .

,

    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");  
    }
    ...
    ...
}

+4
1

; , !

, , , maxlength .., , .

, , , ... , .

0,001 25 000 .

, , , :

    //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- > , , , .

(), .

, .

0

All Articles