Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2006
    Posts
    57
    Plugin Contributions
    0

    Default random code generator

    Found this nice code generator in Zen. I was thinking of creating something similar to it but would generate 4 digits for my table's int(4) primary key. Any suggestions?


    ////
    // Create a Coupon Code. length may be between 1 and 16 Characters
    // $salt needs some thought.

    function zen_create_coupon_code($salt="secret", $length = SECURITY_CODE_LENGTH) {
    global $db;
    $ccid = md5(uniqid("","salt"));
    $ccid .= md5(uniqid("","salt"));
    $ccid .= md5(uniqid("","salt"));
    $ccid .= md5(uniqid("","salt"));
    srand((double)microtime()*1000000); // seed the random number generator
    $random_start = @rand(0, (128-$length));
    $good_result = 0;
    while ($good_result == 0) {
    $id1=substr($ccid, $random_start,$length);
    $query = "select coupon_code
    from " . TABLE_COUPONS . "
    where coupon_code = '" . $id1 . "'";

    $rs = $db->Execute($query);

    if ($rs->RecordCount() == 0) $good_result = 1;
    }
    return $id1;
    }

  2. #2
    Join Date
    Jun 2006
    Posts
    57
    Plugin Contributions
    0

    Default Re: random code generator

    Okay, never thought it would be that simple. The int(4) made it easier, having no results with zeros at the start of the code. Here's my fix, using the zen_rand() function.


    ////
    // Create a Custom Code. Length is 4 Digits starting from 1000

    function zen_create_custom_code() {
    global $db;
    $code = zen_rand(1000,9999);
    $good_result = 0;
    while ($good_result == 0) {
    $query = "select temp_id
    from " . TABLE_TEMP . "
    where temp_id = '" . $code . "'";

    $rs = $db->Execute($query);

    if ($rs->RecordCount() == 0) $good_result = 1;
    }
    return $code;
    }

 

 

Similar Threads

  1. v151 Automatic Random Code Generation for a product?
    By Rick5150 in forum Discounts/Coupons, Gift Certificates, Newsletters, Ads
    Replies: 5
    Last Post: 10 Jun 2013, 09:40 PM
  2. Random code generator for email?
    By LRS in forum All Other Contributions/Addons
    Replies: 1
    Last Post: 23 May 2010, 12:39 AM
  3. Help with Random Code appearing in Product List
    By Oozle in forum General Questions
    Replies: 10
    Last Post: 29 May 2009, 03:04 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR