Jump to content

Help: custom voucher code module


xauen

Recommended Posts

Hi,

 

I am coding a voucher code module but im encountering some problems with CreateAccount() function.

1. The function of the module it to create a voucher code then saving those code to the remote database. The quantity of the voucher code depends on what the client enter in the configurable option named "Voucher Quantity".

2. However, the problem is that It is not saving at all to the database and no voucher code is generated. Also, I doubt the looping is correct also.

 

3. Some small help would be greatly appreciated.

 

Below is the custom function and createAccount() function:

 

function voucher_ConfigOptions() {

# Should return an array of the module options for each product - maximum of 24

   $configarray = array(
 "Database Hostname" 	=> array( "Type" => "text", 	"Size" => "35", ),
 "Database Name"	 	=> array( "Type" => "text", 	"Size" => "35", ),
 "Database Username" 	=> array( "Type" => "text", 	"Size" => "35", ),
 "Database Password" 	=> array( "Type" => "password", "Size" => "35", ),
 "Voucher Qty"			=> array( "Type" => "text", 	"Size" => "35", ),
);

return $configarray;

}


function voucher_CreateAccount($params) {

# Connect to remote MySQL database
$link = mysql_connect($params["configoption1"], $params["configoption3"], $params["configoption4"]);
if (!$link) {
    return 'Could not connect to MySQL';
}

# Select DB
$db_select = mysql_select_db($params["configoption2"], $link);
if (!$db_select) {
    return 'Could not select database';
}

$i = 1;
while($i <= $params['configoptions'][$result_row['Voucher Quantity']])
{

		$voucher_code = voucher_vouchercode() . '-' . voucher_vouchercode() . '-' . voucher_vouchercode();
		//$result = $db->sql_query("SELECT code_name FROM vouchers WHERE code_name='$gen'");
		//$chk = $db->sql_numrows($result);
			$sql = mysql_query("INSERT INTO `vouchers` (`code_name`, `duration`) VALUES('".$voucher_code."', '2592000')", $link);
			if (!$sql) {
				return 'Could not insert row into database '.mysql_error($link);
			}
			break;

$i++;
}
return 'success';

}

function voucher_vouchercode() {
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
mt_srand((double)microtime()*1000000);
$i = 0;
while ($i <= 4)
{
	$num = mt_rand() % 33;
	$tmp = substr($chars, $num, 1);
	$pwd = $pwd . $tmp;
	$i++;
}
return $pwd;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated