Jump to content

discount code script problem


jonnyfortis

Recommended Posts

Hello i have a discount code the partly works but does not fully do what i want it too.

 

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


for testing i have

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 seems to be happening is it is only reading the first record in the database, therefore not allowing me to add any more discounts.

Edited by jonnyfortis
Link to comment
Share on other sites

I am sorry, but I don't see where you pull anything from the db. Could you explain a bit better where/what is the problem?

hello sorry about that i will include that part of the script

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

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

Link to comment
Share on other sites

are you looping thru the results?

your line 66 indicates you are only 'grabing' 1 row

$row_rsVoucher = mysql_fetch_assoc($rsVoucher);

so you mean?

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);
{
while($row_rsVoucher = mysql_fetch_assoc($rsVoucher));
}
$totalRows_rsVoucher = mysql_num_rows($rsVoucher);
Link to comment
Share on other sites

thanks for you help so far, ok i followed the example to see what i results i got

 

this is where the data is pulled from

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

voucher code

// 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']);

the form that validates

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

the echoed results

<?php echo DoFormatCurrency($myvalue, 2, ',', '.', '£ ', ''); ?>

so i tried another code that is 2nd in the database and got the following (lottiep3)

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

i nothing is shown from the echo

echo $row_rsVoucher['VCode'];
echo "<br/>";

so a bit stuck 

Link to comment
Share on other sites

The standard way to process a query is (psuedo code):

$sql = 'SELECT ...';
$res = mysql_query($sql);
# Test to be sure the query did not fail

# If you need the number of rows ...
$rowCount = mysql_num_rows($res);

# retrieve and process each row
while ($row = mysql_fetch_assoc($res) {
 # do something with each row of data
}
mysql_free_result($res);
You seem to have num_rows() and fetch_assoc() in the wrong places.

 

Also, the mysql library extension in PHP has been deprecated (it is going away). For new development you should be using the improved version mysqli (there's an "i" on the end).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.