Page 1 of 1

Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 4:58 am
by billteale
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


This is probably so easy to do, but I have been trying for two days to find out what I have been doing wrong.

The scenario is - I am setting up a t-shirt company's website, and he wants to be able to offer certain customers a certain percentage discount off the price of a shirt.

I have set up a login screen where people put in their code, which is registered as a session variable, and passed through to the page. I linked this to the cutstomer details page successfully, so that I can show the discount, the customer name etc, but when I try to take the discount off the price, the number does not show up - it is driving me mad!

Here is the code snippet, the recordset LIST is the product details, the recordset DISCOUNT contains the discount details.

Code: Select all

 
    <?php 
// Show IF Conditional region3 
if (@$row_LIST['ID'] != "") {
?>
        <p><span class="style2"><strong><a href="images/<?php echo $row_LIST['IMAGE']; ?>" target="_blank"><img src="images/th<?php echo $row_LIST['IMAGE']; ?>" alt="<?php echo $row_LIST['PRODUCT_NAME']; ?>" border="0" class="floatLeftClear" /></a><?php echo $row_LIST['PRODUCT_NAME']; ?> 
 
<?php
function percent() {
 
$price = $row_LIST['COST'];
$discount = $row_DISCOUNT['DISCOUNT'];
 
$count1 = ($price / 100);
$count2 = (100 - $discount);
$count3 = ($count1 * $count2);
$total += number_format($count3, 2);
 
echo ("- $".$total." ");
}
?>
        
        </strong></span> </p>
        <p><span class="style3">*Shipping costs will be shown before confirmation of order</span><br />
          <span class="style3"><em>(tax is charged for sales within North Carolina)</em></span></p>
        <p class="style2"><?php echo $row_LIST['DESCRIPTION']; ?></p>
        <?php 
// else Conditional region3
} else { ?>
        Currently there are no products to view in this category.
  <?php } 
?>
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 5:10 am
by onion2k
I think this is a scope problem. When you call a function (eg percent() ) it runs in it's own little world. It can't see any of your variables. You need to pass them to it so it can see them. Your function needs to see the price and the discount ... so you need to change the function declaration to be:

Code: Select all

function percent($price, $discount) {
That means the function will expect 2 arguments when you call it.

You'll need to remove the code that sets $price and $discount in the function, so delete these two lines:

Code: Select all

$price = $row_LIST['COST'];
$discount = $row_DISCOUNT['DISCOUNT'];
Lastly you'll need to call the function with the 2 arguments:

Code: Select all

percent($row_LIST['COST'], $row_DISCOUNT['DISCOUNT']);
Now you can move the function somewhere else, and every time you want to print a discount you can use your percent() function.

I suggest you read the PHP manual function() page too. It'll help.

Note to others: Any mention of globals in this thread will be deleted. ;)

Re: Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 5:23 am
by billteale
Thanks for such a quick reply, I will try it out and let you know.

Thanks fo ryour help, I really appreciate it!

Re: Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 1:29 pm
by billteale
It works! Thanks very much indeed for your assistance!

I only yhave one hurdle to climb now, can you help me find a way to force the end result to two decimal places? For example, if a discount leaves a result of 6.8 I need it to read 6.80 (currency style).

Any thoughts? I looked around the forums, and found some stuff, but it was way over my head!

Bill.

Re: Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 3:02 pm
by onion2k
number_format()

If you want to find out how to use it look in the manual.

Re: Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 4:36 pm
by billteale
onion2k wrote:number_format()

If you want to find out how to use it look in the manual.
Thanks - I have looked, and in my limited knowledge of the subject, I don't think that is where the problem is.

the number_format() seems to be working correctly, as in the number coming back has a maximum of two decimal places, but what I am wanting to do is to ouput two decimal places when there are less than two (5 becomes 5.00 - 5.1 becomes 5.10 etc).

Believe me, I have had a good look around, and tried changing things in the function, like changing the number_format to a money format, and I have tried using sprintf in the function, but nothing seems to work.

I think I may need to change something in the ouput line of

Code: Select all

<?php echo percent($row_LIST['COST'], $row_DISCOUNT['DISCOUNT']); ?>
I tried adding

Code: Select all

<?php sprintf ('%.2f',  percent($row_LIST['COST'], $row_DISCOUNT['DISCOUNT'])); ?>
but variations of this either didn't work, or produced things like 5.95.00 for a 5.95 price

Unfortunately, this is the line I really haven't grasped, and I am struggling. Can anybody help me out?

Thanks

Bill.

Re: Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 4:42 pm
by anto91

Code: Select all

 
number_format($varible,2);
 
Two is the number of numbers are the dot

Re: Trying to create a percentage from two variables

Posted: Thu Apr 03, 2008 4:44 pm
by billteale
I tried this, but it seems not to affect numbers that have less than two decimals, it simply rounds them up

I need something to ouptut to two decimal places even it the number does not contain them