创建折扣代码系统(MySQLphp)

创建折扣代码系统(MySQLphp),第1张

创建折扣代码系统(MySQL / php)

实际上,这实际上是一类功能。您需要一个看起来像这样的类接口

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

好吧,这就是我要怎么做。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5084696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存