Log in

View Full Version : discount code script problems needs a loop ??



jonnyfreak
06-11-2013, 08:58 AM
to let you know i did post this problem i am having on another forum a while back but to no avail...so i thought someone here may be able to help.

The way i have set this up is in the backend of the site i can add my own discount codes with the following rules we have a % and a value, in this case £

the % is represented by f and the £ = p so when ever you make a new code you enter either f or p followed by the value of the discount so examples

lot123p5 ( this is £5 off )
or
lot123f10 ( this 10% off )

the voucher code is as follows


// voucher code
if (isset($_POST['vouchCode']) && $_POST['vouchCode'] == $row_rsVoucher['VCode']) {
$mycode = $row_rsVoucher['VCode'];
$spos = strpos($mycode, "f");
if ($spos !== false) {
$myvalue = substr($mycode, $spos+1);
$myvalue = $XCart_sumTotal * $myvalue / 100;
} else {
$spos = strpos($mycode, "p");
if ($spos !== false) {
$myvalue = substr($mycode, $spos+1);
}
}
$myTotal = $XCart_sumTotal - $myvalue;
$_SESSION['vouchCode'] = $myvalue;
} else unset($_SESSION['vouchCode']);

function DoFormatCurrency($num,$dec,$sdec,$sgrp,$sym,$cnt) {
setlocale(LC_MONETARY, $cnt);
if ($sdec == "C") {
$locale_info = localeconv();
$sdec = $locale_info["mon_decimal_point"];
$sgrp = $sgrp!="" ? $locale_info["mon_thousands_sep"] : "";
$sym = $cnt!="" ? $locale_info["currency_symbol"] : $sym;
}
$thenum = $sym.number_format($num,$dec,$sdec,$sgrp);
return $thenum;
}

what is happening is i have echoed out

<?php echo $myvalue ?><br />
<?php echo $_POST['vouchCode']; ?><br />
<?php echo $row_rsVoucher['VCode']; ?> <br />


the <?php echo $row_rsVoucher['VCode']; ?> is always showing the lot123p5 which is the first record on the DB

when i submit the value lot123p5 i get the following

<?php echo $myvalue ?> shows 5
<?php echo $_POST['vouchCode']; ?> shows lot123p5
and <?php echo $row_rsVoucher['VCode']; ?> shows lot123p5

so thats working

when i try the percentage and submit lot123f10 i get the following


<?php echo $myvalue ?> shows nothing
<?php echo $_POST['vouchCode']; ?> shows lot123f10
and <?php echo $row_rsVoucher['VCode']; ?> shows lot123p5

and therefor isnt working

i have added another discount code to the £ discount to see if it was and issue with %

lottiep3

to show £3 discount but that isnt working either so the script must only be calling the first record in the database

since this post began i have found out more information so can expand on what i think is need.

because the issue is the script is only checking the first record of the database an loop is needed to check all the records. But i am unclear on how to do this


mysql_select_db($database_lotties, $lotties);
$query_rsVoucher = "SELECT * FROM LOTTIE_vouchercode";
$rsVoucher = mysql_query($query_rsVoucher, $lotties) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher);
$totalRows_rsVoucher = mysql_num_rows($rsVoucher);

is where the information is coming from

so a loop is required to produce all the results but dont know how to do it.

i did look at some tutorial that were suggested


mysql_select_db($database_lotties, $lotties);
$query_rsVoucher = "SELECT * FROM LOTTIE_vouchercode";
$rsVoucher = mysql_query($query_rsVoucher, $lotties) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher) or die(mysql_error());

while($row_rsVoucher = mysql_num_rows($rsVoucher)){
echo $row_rsVoucher['VCode'];
echo "<br/>";
}
$totalRows_rsVoucher = mysql_num_rows($rsVoucher);

but this produced the same results

the form that check to see if the code is present is


<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
<tr>
<td class="mediumPinkHeaders">Voucher Code:</td>
<td><input type="text" name="vouchCode" value="<?php echo @$_POST['vouchCode']; ?>" size="32" /></td>
<td><input type="submit" value="update" /></td>
</tr>
</form>