Jump to content
xauen

Help: custom voucher code module

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

Share this post


Link to post
Share on other sites

Your while loop is all jacked up.

 

for ($i=0; $i < (int)$params["configoption5"]; $i++) {
 // do stuff here
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines