SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rounding numbers down in a sum

    I have this sum which shows poeple how many points they have collected after they have entered the details. basically for ever 200 spent they receive a 5 gift voucher. here is the sum I am using..... but say if someone has spent 700 this means they have 17.5 in vouchers. We would never send out 17.5 it would always be rounded down to the nearest 1 spent...in this case 17. Is there a way to include this in the script. ie. is there a way to remove the decimal point?????


    PHP Code:
    $value 40;


    $sql "SELECT * FROM $table_name WHERE customer_num = '$customer_num' AND name = '$name'"
    echo 
    "<p>$sql</p>"

    $result mysql_query($sql$connection) or die ("couldn't execute query. Error: " mysql_error() ); 
    $cnt mysql_num_rows($result); 
    $row mysql_fetch_array($result); 
    $points $row['points']; 
    $vouchers $points $value;


    if (
    $cnt !=0){
     
    print 
    "$points<br>$vouchers"


    else{ 
    print 
    "TRY AGAIN"



    ?> 

  2. #2
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $vouchers = floor($points / $value);

    stay good
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  3. #3
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://uk.php.net/manual/en/function.floor.php

    Maybe what you're looking for.

    In PHP:
    floor = round down
    ceil = round up

    Edit:


    boccio beat me to it

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much.....

    just another quick one I forgot about. If they have spent less than 200 which is a 5 voucher, they will not be sent any vouchers. So how can I get the script to say "SORRY NO VOUCHERS" if the have spent 199 or less?

    Thank-you so much for your help!

  5. #5
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if($money <= 199)
    {
    echo 
    'SORRY NO VOUCHERS';

    $cnt holds your money? No matter, just replace what ever var you have that holds the total amount of money in the code above for $money.

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $points is the money. so how would I add that to this piece of code if $points is equal to money

    <?php
    $value = 40;


    $sql = "SELECT * FROM $table_name WHERE customer_num = '$customer_num' AND name = '$name'";
    echo "<p>$sql</p>";

    $result = mysql_query($sql, $connection) or die ("couldn't execute query. Error: " . mysql_error() );
    $cnt = mysql_num_rows($result);
    $row = mysql_fetch_array($result);
    $points = $row['points'];
    $vouchers = floor($points / $value);


    if ($cnt !=0){

    print "$points<br>$vouchers";
    }

    else{
    print "TRY AGAIN";

    }

    ?>

  7. #7
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $value 
    40;


    $sql "SELECT * FROM $table_name WHERE customer_num = '$customer_num' AND name = '$name'";
    echo 
    "<p>$sql</p>";

    $result mysql_query($sql$connection) or die ("couldn't execute query. Error: " mysql_error() );
    $cnt mysql_num_rows($result);
    $row mysql_fetch_array($result);
    $points $row['points'];
    $vouchers floor($points $value);
    if(
    $points <= 199)
    {
    echo 
    'SORRY NO VOUCHERS'
    }

    if (
    $cnt !=0){

    print 
    "$points<br>$vouchers";
    }

    else{
    print 
    "TRY AGAIN";

    }

    ?>
    You could also have it die() if you don't want it to display anything else and end the scripts processing.

  8. #8
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could put exit; after the sorry no vouchers part.

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    would that stop it printing out the points and amount of vouchers as well?

    <?php
    $value = 40;


    $sql = "SELECT * FROM $table_name WHERE customer_num = '$customer_num' AND name = '$name'";
    echo "<p>$sql</p>";

    $result = mysql_query($sql, $connection) or die ("couldn't execute query. Error: " . mysql_error() );
    $cnt = mysql_num_rows($result);
    $row = mysql_fetch_array($result);
    $points = $row['points'];
    $vouchers = floor($points / $value);
    if($points <= 199)
    {
    echo 'SORRY NO VOUCHERS';
    }
    exit;

    if ($cnt !=0){

    print "$points<br>$vouchers";
    }

    else{
    print "TRY AGAIN";

    }
    ?>

  10. #10
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $value 
    40;


    $sql "SELECT * FROM $table_name WHERE customer_num = '$customer_num' AND name = '$name'";
    echo 
    "<p>$sql</p>";

    $result mysql_query($sql$connection) or die ("couldn't execute query. Error: " mysql_error() );
    $cnt mysql_num_rows($result);
    $row mysql_fetch_array($result);
    $points $row['points'];
    $vouchers floor($points $value);
    if(
    $points <= 199)
    {
    die(
    'SORRY NO VOUCHERS');
    }

    if (
    $cnt !=0){

    print 
    "$points<br>$vouchers";
    }

    else{
    print 
    "TRY AGAIN";

    }

    ?>

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now if the fields are completed incorrectly it returns "SORRY NO VOUCHERS" rather than "TRY AGAIN". and also when it prints out "SORRY NO VOUCHERS" the HTML stops straight after this, because of the die is it????

  12. #12
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, if the fields are completed incorrectly it returns 'try again', and if money is<200 it goes 'sorry...'

    you're right about the die part...
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  13. #13
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just tried it. If the details are added incorrectly "Sorry no vouchers" is returned.

    If the details are added correctly and the user has more than 200 it returns the points correctly.

    If the details are added correctly but the user does not have enough money it prints out "Sorry no vouchers".

    So the only part not working is the "If details are added incorrectly" this should return "try again" but at the moment it returns "Sorry no vouchers".

    Any ideas?

  14. #14
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hm... try this for debugging purposes:
    PHP Code:
    <?php 
    $value 
    40

    $sql "SELECT * FROM $table_name WHERE customer_num = '$customer_num' AND name = '$name'"

    $result mysql_query($sql$connection) or die ("couldn't execute query. Error: " mysql_error() ); 
    $cnt mysql_num_rows($result); 
    $row mysql_fetch_array($result); 
    $points $row['points']; 
    $vouchers floor($points $value); 
    if(
    $points <= 199
     { 
        echo (
    'SORRY NO VOUCHERS'); } 
    else
     {
          if (
    $cnt !=0){ 
             print 
    "$points<br>$vouchers"; } 
          else{ 
             print 
    "TRY AGAIN"; }
     } 
    ?>
    when you test this make sure that points>200

    stay good
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  15. #15
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got this.....

    Parse error: parse error in /home/altdesign/public_html/limeys_test/show/show.php on line 80

    this is the line...******if ($cnt !=0){

  16. #16
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote this w/o running, but now I tested it and it works fine...

    parse error you have is most probably due to some curly brace... check your braces...
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  17. #17
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is very strange..... still not working although the code looks fine. This is what I have...it now errors on this line....print "$points<br>$vouchers";

    I copied your code excatley...is this what you have?

    PHP Code:
    if($points <= 199){ 

    ****echo (
    'SORRY NO VOUCHERS'); 

    }else{ 

    if (
    $cnt !=0){ 
    *********
    print 
    "$points<br>$vouchers"
     
    }else{ 

    *********print 
    "TRY AGAIN"; } 



  18. #18
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    send me the file on PM...
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  19. #19
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just realised why it's not working...when the users details are inputted incorretcly the script still runs and it thinks that the points are below 199 so thats why it prints out SORRY NO VOUCHERS instead of TRY AGAIN.

    So what I need to do some how is.... first get the script to check if the name and customer number is correct then work out if there are points to be shown.

    any ideas.....???

  20. #20
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just out of curiosity - do you ever sleep?
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  21. #21
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    never this is really doing my head in!!!!!!!!

  22. #22
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm...
    maybe you could just switch if statements...
    like

    if ($cnt!=0)
    {
    if($points > 199) // instead of '<=' go with '>'
    echo....'SORRY NO VOUCHERS'
    else
    echo $points<br>$vouchers
    }
    else
    print "TRY AGAIN
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  23. #23
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any pointers....dosen't seem to work...

    if ($cnt!=0){

    if($points > 199) // instead of '<=' go with '>'

    echo'SORRY NO VOUCHERS';}

    else{

    echo "$points<br>$vouchers";

    }else{

    print "TRY AGAIN";
    }
    ?>

  24. #24
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    done at last......yeah!
    thank you for all your help......

    PHP Code:
    if ($cnt==0){
    //no rows returned, so error in finding records 
    print "TRY AGAIN"

    }else{ 

    //rows returned, so check values 
    //if less than 200 --> show no voucher msg
    if($points <= 199){ 

    echo (
    'SORRY NO VOUCHERS');

    }elseif (
    $points>=200){ 
    //there are vouchers so show 'em
    print "$points<br>$vouchers"



    }
    ?> 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •