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. coupon email - bit of code help required
    By Diva in forum General Questions
    Replies: 5
    Last Post: 6 Sep 2008, 09:24 PM
  2. Random Product Script - missing product_id
    By unisupuk in forum General Questions
    Replies: 1
    Last Post: 14 Jun 2006, 06:37 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
  •