2011-10-15 68 views
0

我正在開發一個沒有使用集體「解決方案」的購物網站。 我希望客戶從您的帳戶和他的帳戶面板收到郵件中的優惠券,他有一張可用於打印的優惠券。 發生的每個優惠券,您必須爲每個客戶擁有唯一的代碼。 有沒有人有想法該怎麼辦?創建優惠券代碼

回答

1

他的電子郵件ID的哈希值可以用作優惠券代碼。還將其存儲在數據庫中以避免欺詐。

0

使用PHP條碼製造商,這就是我會做什麼

2

我使用此代碼

while (true) { 
    $ccode = "CP" . strtoupper (dechex (rand (1000000000, 9999999999))) . strtoupper (chr(rand(65, 90))) . chr(rand(65, 90)); 
    $check = mysql_query("SELECT coupon_code FROM item_coupons WHERE coupon_code = '$ccode'"); 
    if (mysql_num_rows($check) != 0) { 
     //duplicate 
    } else { 
     break 1; 
    } 
} 
+0

這是PHP等等它應該工作 – Ergec

0

首先,你可以在數據庫中創建一個coupon表是這樣的:

create table `coupon`(
    `code` char(32) primary key, 
    `email` varchar(255), 
    `used` tinyint(1) default 0 
) ENGINE=MYISAM; 

現在您可以完全隨機生成優惠券,然後將其保存在表格中供以後檢查:

function generate_coupon_for($email) 
{ 
    do 
    { 
     $cpn = md5(rand()); 
     if(mysql_query("insert into `coupons`(`code`, `email`) values('$cpn', '$email');")) 
     { 
      return $cpn; 
     } 
    }while(mysql_errno() == 1062); 
    //We had an error while trying to insert the coupon 
} 

而這樣一來,就可以檢查優惠券:

function check_coupon($code, $email) 
{ 
    $q = mysql_query("update `coupons` set `used` = 1 where `email`='$email' and `code`='$code' and `used`=0;"); 
    return mysql_affected_rows() > 0; 
} 

這將返回true或false ...

我希望這些可以幫助你......