Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/52312-help-with-an-alternative-option/
Share on other sites

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?

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

 

 

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

 

 

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,'.','');
}
?>

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

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?

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,'.','');
}
?>

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;

 

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

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.