我正在考虑建立我的第一个真正的类(class),我玩过点点滴滴,但现在是真正尝试的时候了:)
我想要做的是有一个表单类来处理我所有的表单提交,检查输入的数据并返回错误消息或成功消息。
这是我的一个表格,(我在一页上有 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。在类里面,它所做的第一件事就是检查提交的表单,然后检查已提交的数据并对其执行必要的操作
我的问题是,一旦我的类(class)知道提交了哪个表单,检查提交数据的最佳方法是什么?我应该在函数中创建变量以获取所有发布数据并从那里获取它还是应该将实际函数中的变量作为参数传递? , 还是有关系?
这是我当前的类文件,有点空空如也
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");
}
...
...
}
任何帮助将不胜感激
卢克
最佳答案
我为数据库中的每个表生成一个类;生命太短暂了,无法真正为每个数据库表编写该功能!
每个字段都有自己的对象,包含它的值、类型、最大长度等,并且这些字段也被添加到一个数组中,所以我可以用它们做一些很酷的事情。
每个表类都扩展了一个更大的类,允许我插入、更新、删除和显示为表和表单...我可以覆盖任何非标准函数,尽管这种情况很少见。
迭代约 25000 条大小合适的记录的性能开销约为 0.001 毫秒。
例如,我生成文档记录数据表的代码如下所示:
//创建对象
$objDocument = new cls__CMS_Document();
//设置要在数据表中显示的字段
$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);
//包括来自 ID 字段的超链接
$objDocument->objID->HyperLink('/drilldown.php');
$objDocument->objID->Post(true);
//通过格式化函数传递一个字段
$objDocument->objAuthorID->OutputFunction('getAuthorFromID');
$result .= $objDocument->displayTable($sqlConditions);
取消设置($objDocument);
最好的一点:每一步,自动完成工作:)所以你输入 $objDocument-> 弹出所有方法和属性,包括所有字段对象,这样你就不会拼错它们。
如果我得到足够的兴趣(投票),我会把整个东西放到网上。
当你自己制作时,这应该是值得深思的。
关于php oop 编程 : building a class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20293402/