实际上,这实际上是一类功能。您需要一个看起来像这样的类接口
class ProductDiscount { static public function create($pre, $discount, $options = NULL); static public function validate($pre); private $_pre; // string private $_discount; // float private $_expire; // unix timestamp (see time()) or null if unlimited private $_count; // int or null if unlimited private function __construct(); public function getCode(); // return string public function getDiscount(); // return float public function isLimited(); // return bool; true if $_count || $_expire is not null public function getCount(); // returns int; how many of this discount is left or null if unlimited public function getExpireDate();// returns int (unix timestamp) or null if unlimited public function consume(); // apply this discount now public function consumeAll(); // invalidate this discount for future use}
数据库表可能如下所示
id UNSIGNED INT(10) NOT NULL AUTOINCREMENT -- (Primary Key)pre VARCHAr(12) NOT NULL -- (Indexed, unique)discount UNSIGNED INT(3) NOT NULL-- percent value 0..100expire DATETIME DEFAULT NULL -- unlimited by defaultcount INT(10) DEFAULT 1 -- can be NULL
注意: 验证过程可能只是一个简单的
SELECt声明:
SELECT * FROM tblproductdiscount WHERe pre = {$pre} -- $pre = mysql_real_escape_string($pre) AND (count IS NULL OR count > 0) AND (expire IS NULL or expire > NOW())
if (!empty($discountCode)) { $discount = ProductDiscount::validate($discountCode); if ($discount !== null) { $price *= (1 - $discount->getDiscount()); $discount->consume(); }}
好吧,这就是我要怎么做。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)