jonnyfortis Posted June 17, 2013 Share Posted June 17, 2013 (edited) 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 examplesfor testing i havelot123p5 ( this is £5 off )orlot123f10 ( 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 June 17, 2013 by jonnyfortis Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/ Share on other sites More sharing options...
Dathremar Posted June 17, 2013 Share Posted June 17, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1436379 Share on other sites More sharing options...
jonnyfortis Posted June 17, 2013 Author Share Posted June 17, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1436382 Share on other sites More sharing options...
litebearer Posted June 17, 2013 Share Posted June 17, 2013 are you looping thru the results? your line 66 indicates you are only 'grabing' 1 row $row_rsVoucher = mysql_fetch_assoc($rsVoucher); Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1436401 Share on other sites More sharing options...
jonnyfortis Posted June 17, 2013 Author Share Posted June 17, 2013 are you looping thru the results? your line 66 indicates you are only 'grabing' 1 row $row_rsVoucher = mysql_fetch_assoc($rsVoucher); oh so i need to add a loop here, is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1436405 Share on other sites More sharing options...
jonnyfortis Posted June 17, 2013 Author Share Posted June 17, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1436416 Share on other sites More sharing options...
litebearer Posted June 17, 2013 Share Posted June 17, 2013 Look at this tutorial... http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1436419 Share on other sites More sharing options...
jonnyfortis Posted June 17, 2013 Author Share Posted June 17, 2013 Look at this tutorial... http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php 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 Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1436427 Share on other sites More sharing options...
DavidAM Posted June 21, 2013 Share Posted June 21, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/279254-discount-code-script-problem/#findComment-1437267 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.