fazzfarrell Posted May 21, 2007 Share Posted May 21, 2007 I have this code <?php $english_format_number = number_format($number); $number1 = $row_rsProd['P_Retail_Price']; $number2 = $row_rsUser['Percent']; echo number_format ($number1 / $number2 * 100); ?> the '$row_rsUser['Percent']' is pulling a percentage (117.5 etc) from a database when the user logs in, which gives the user a discount. if they don't log in want it to be a '0' percent so that the full price displays. anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/ Share on other sites More sharing options...
akitchin Posted May 21, 2007 Share Posted May 21, 2007 then it's just a matter of using a conditional to assign the price the user sees: if (user is logged in) { $price = discounted_price; } else { $price = actual_price; } or is your question how to calculate the discounted price? Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258062 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 HI Yes I need a condional, how do I apply this to the code I have? thanks Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258063 Share on other sites More sharing options...
akitchin Posted May 21, 2007 Share Posted May 21, 2007 well how are you tracking user logins? in a session? Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258069 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 Yeah the user is from a session MM_Username Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258071 Share on other sites More sharing options...
akitchin Posted May 21, 2007 Share Posted May 21, 2007 then a useful conditional would be to check if that variable is set: if (isset($_SESSION['MM_Username'])) { $price = discounted_price; } else { $price = normal_price; } Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258080 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 So this <?php $english_format_number = number_format($number); $number1 = $row_rsProd['P_Retail_Price']; if (isset($_SESSION['MM_Username'])) { $number2 = $row_rsUser['Percent']; }else{ $number2 = 0; } echo number_format ($number1 / $number2 * 100); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258085 Share on other sites More sharing options...
akitchin Posted May 21, 2007 Share Posted May 21, 2007 that will yield a divide by zero error. you may want to re-evaluate how you're calculating your discounted price, as it doesn't make much sense. Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258091 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 well spotted updated <?php $english_format_number = number_format($number); $number1 = $row_rsProd['P_Retail_Price']; if (isset($_SESSION['MM_Username'])) { $number2 = $row_rsUser['Percent']; }else{ $number2 = 1; } echo number_format ($number1 / $number2 * 100); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258096 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 That works great, how can I change it so that the price without logging in becomes You Pay £41.50 etc opposed to £41,50? An when I login for my discount the price becomes £38 (example) instead of £38.00? Is it to do with the english format? Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258118 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 OK this was written on the fly so better test it <?php $english_format_number = number_format($number); $number1 = $row_rsProd['P_Retail_Price']; $number2 = $row_rsUser['Percent']; if (isset($_SESSION['MM_Username'])) { $save = ($number2 * 100); }else{ $save = 1; } $retail = ($number1 / $save ); echo FormatPrice($retail)." save'd $save% (".FormatPrice($number2).")"; function FormatPrice($price) { $price = preg_replace("/[^0-9\.]/", "", str_replace(',','.',$price)); if (substr($price,-3,1)=='.') { $sents = '.'.substr($price,-2); $price = substr($price,0,strlen($price)-3); } elseif (substr($price,-2,1)=='.') { $sents = '.'.substr($price,-1); $price = substr($price,0,strlen($price)-2); } else { $sents = '.00'; } $price = preg_replace("/[^0-9]/", "", $price); return number_format($price.$sents,2,'.',''); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258125 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 Seem to get an error You pay:£ Fatal error: Call to undefined function: formatprice() in /home/sites/octopus8.co.uk/public_html/products.php on line 413? Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258131 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 did you add the function FormatPrice($price) { $price = preg_replace("/[^0-9\.]/", "", str_replace(',','.',$price)); if (substr($price,-3,1)=='.') { $sents = '.'.substr($price,-2); $price = substr($price,0,strlen($price)-3); } elseif (substr($price,-2,1)=='.') { $sents = '.'.substr($price,-1); $price = substr($price,0,strlen($price)-2); } else { $sents = '.00'; } $price = preg_replace("/[^0-9]/", "", $price); return number_format($price.$sents,2,'.',''); } remember if your inside a function then your need to place the above code outside of it ie <?php function dostuff() { echo FormatPrice($retail); } //function closes //above code goes here ?> Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258134 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 I tried it like this <?php $english_format_number = number_format($number); $number1 = $row_rsProd['P_Retail_Price']; $number2 = $row_rsUser['Percent']; if (isset($_SESSION['MM_Username'])) { $save = ($number2 * 100); }else{ $save = 1; } $retail = ($number1 / $save ); echo FormatPrice($retail)." save'd $save% (".FormatPrice($number2).")"; { echo FormatPrice($retail); } //function closes function FormatPrice($price) { $price = preg_replace("/[^0-9\.]/", "", str_replace(',','.',$price)); if (substr($price,-3,1)=='.') { $sents = '.'.substr($price,-2); $price = substr($price,0,strlen($price)-3); } elseif (substr($price,-2,1)=='.') { $sents = '.'.substr($price,-1); $price = substr($price,0,strlen($price)-2); } else { $sents = '.00'; } $price = preg_replace("/[^0-9]/", "", $price); return number_format($price.$sents,2,'.',''); } ?> But still getting an error? Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258139 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 This should work.. do you know if your in a class could you post the full script (remove passwords etc) or PM it to me or maybe the first 10 lines (mayhelp) <?php $english_format_number = number_format($number); $number1 = $row_rsProd['P_Retail_Price']; $number2 = $row_rsUser['Percent']; if (isset($_SESSION['MM_Username'])) { $save = ($number2 * 100); }else{ $save = 1; } $retail = ($number1 / $save ); echo FormatPrice($retail)." save'd $save% (".FormatPrice($number2).")"; function FormatPrice($price) { $price = preg_replace("/[^0-9\.]/", "", str_replace(',','.',$price)); if (substr($price,-3,1)=='.') { $sents = '.'.substr($price,-2); $price = substr($price,0,strlen($price)-3); } elseif (substr($price,-2,1)=='.') { $sents = '.'.substr($price,-1); $price = substr($price,0,strlen($price)-2); } else { $sents = '.00'; } $price = preg_replace("/[^0-9]/", "", $price); return number_format($price.$sents,2,'.',''); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258141 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 the first few lines of code <?php // IntelliCART MX - icPoles // (c)2002. Tim Green. All rights reserved. ob_start(); require_once("./classes/icPoles_IC.php"); ?> <?php session_start(); $_SESSION['Date'] = date('Y-m-d') ."\n"; ?> <?php require_once('Connections/poles.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258145 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 try moving the function ie <?php // IntelliCART MX - icPoles // (c)2002. Tim Green. All rights reserved. ob_start(); require_once("./classes/icPoles_IC.php"); ?> <?php session_start(); $_SESSION['Date'] = date('Y-m-d') ."\n"; ?> <?php require_once('Connections/poles.php'); ?> <?php function FormatPrice($price) { $price = preg_replace("/[^0-9\.]/", "", str_replace(',','.',$price)); if (substr($price,-3,1)=='.') { $sents = '.'.substr($price,-2); $price = substr($price,0,strlen($price)-3); } elseif (substr($price,-2,1)=='.') { $sents = '.'.substr($price,-1); $price = substr($price,0,strlen($price)-2); } else { $sents = '.00'; } $price = preg_replace("/[^0-9]/", "", $price); return number_format($price.$sents,2,'.',''); } ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; then just use <?php $english_format_number = number_format($number); $number1 = $row_rsProd['P_Retail_Price']; $number2 = $row_rsUser['Percent']; if (isset($_SESSION['MM_Username'])) { $save = ($number2 * 100); }else{ $save = 1; } $retail = ($number1 / $save ); echo FormatPrice($retail)." save'd $save% (".FormatPrice($number2).")"; ?> any error please post the exact error Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258149 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 please note i just updated the above post Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258151 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 this has gone way above my head! I get this displayed on the page You pay:£ 92553191489362.00 save'd 11750% (117.50) Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258154 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 switch the * and / around if (isset($_SESSION['MM_Username'])) { $save = ($number2 / 100); }else{ $save = 1; } $retail = ($number1 * $save ); Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258156 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 still getting this: You pay:£ 12778125.00 save'd 1.175% (117.50) Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258162 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 can you echo the $number1 & 2 $retail = ($number1 / $save ); echo "number1 = $number1<br>number2 = $number2<br>"; echo FormatPrice($retail)." save'd $save% (".FormatPrice($number2).")"; Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258163 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 yes noe we are getting this: number1 = 108.75 number2 = 117.5 92553191489362.00 save'd 1.175% (117.50) Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258178 Share on other sites More sharing options...
fazzfarrell Posted May 21, 2007 Author Share Posted May 21, 2007 Brill, sorted it, that was really good! thank you Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258185 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 Is $row_rsUser['Percent'] a percentage ? or the normal retail price? as 117.5 is saving more then the cost of the item! Ahh ok, whats was the problem ? also please click solved (if solved) Quote Link to comment https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/#findComment-258187 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.