RLisboa
RLisboa

Reputation: 19

Create Coupon Code

I am developing a shopping site without the use of collective "solutions" ready. I want the client to receive the voucher in the mail from your account and the Panel of his account, he has a coupon available for printing. Each coupon that happens, you must have a unique code per customer. Does anyone have an idea how to do?

Upvotes: 0

Views: 3032

Answers (4)

Sgn.
Sgn.

Reputation: 1696

First of all you can create a coupon table in your database like this :

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

Now you can generate coupon completely random and then save it in the table for later checking :

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
}

And this way, you can check 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;
}

It will return true or false...

I hope these can help you...

Upvotes: 0

Ergec
Ergec

Reputation: 11824

I use this code

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

Upvotes: 2

Adam Fowler
Adam Fowler

Reputation: 1751

Use a php barcode maker, Thats what I would do

Upvotes: 0

Sreenath Nannat
Sreenath Nannat

Reputation: 2019

A Hash of his email id can be used as coupon code. Also store it in database to avoid fraud.

Upvotes: 1

Related Questions