php面向对象编程:构建类
我正在考虑建立我的第一个真正的类,我已经玩了比特和bob,但它的时间来尝试真正的:) 我试图做的是创建一个表单类,它处理我提交的所有表单,检查输入的数据,并返回错误消息或成功消息 这是我的一张表格,(一页上有5张) 任何帮助都将不胜感激php面向对象编程:构建类,php,class,oop,Php,Class,Oop,我正在考虑建立我的第一个真正的类,我已经玩了比特和bob,但它的时间来尝试真正的:) 我试图做的是创建一个表单类,它处理我提交的所有表单,检查输入的数据,并返回错误消息或成功消息 这是我的一张表格,(一页上有5张) 任何帮助都将不胜感激 Luke我为数据库中的每个表生成一个类;生命太短暂,无法为每个数据库表实际编写该功能 每个字段都有它自己的对象,包含它的值、类型、maxlength等。这些字段也被添加到一个数组中,这样我就可以用它们做一些很酷的事情 每个表类都扩展了一个更大的类,允许我插入、
Luke我为数据库中的每个表生成一个类;生命太短暂,无法为每个数据库表实际编写该功能 每个字段都有它自己的对象,包含它的值、类型、maxlength等。这些字段也被添加到一个数组中,这样我就可以用它们做一些很酷的事情 每个表类都扩展了一个更大的类,允许我插入、更新、删除和显示为表和表单。。。我可以覆盖任何非标准的函数,尽管这非常罕见 对于遍历大约25000条大小良好的记录,性能开销约为0.001ms 例如,生成文档记录数据表的代码如下所示:
最好的一点是:每一步都可以自动完成:)因此您可以键入$objDocument->,所有的方法和属性都会弹出,包括所有字段对象,这样您就不会拼写错误
如果我有足够的兴趣(选票),我会把所有的东西都放在网上
不过,当你自己做的时候,这应该是值得深思的 我不会在那个类中直接使用
$\u POST
。这是测试用的毒药。最简单的改进是将post值作为构造函数参数传递。欢迎hek2mglYou提供帮助。。我可以告诉你很多关于如何改进的事情,但我认为你能为自己尝试一些东西是件好事。(+1)要有耐心,继续玩代码,一旦进入“大师级”。:)大多数情况下,“大师级”是通过从你犯的错误中学习而获得的。在这一点上,不要太在意完美。继续玩吧。。太好了!
<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>
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");
}
...
...
}
//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);