SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Can anyone?

  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Can anyone?

    Can anyone see the problem with this PHP code?

    PHP Code:
    if(($_POST['frac1'] == "") && ($_POST['frac2'] == "") && ($_POST['dec'] == "")) {
    $message "<font color=\"red\">Please fill in both fraction fields!</font>";

        } elseif((
    $decRes <= 1) && ($decRes 0)) {
        
    $fracRes1 "";
        
    $message "<font color=\"red\">Please enter a decimal bigger than 1!</font>";

        } elseif((
    $decRes-1) == (int)($decRes-1)) {
        
    $fracRes1 $decRes-1;
        
    $fracRes2 = ($decRes-1)/($decRes-1);

        } elseif((((
    $decRes-1)*100)/50) == (int)((($decRes-1)*100)/50)) {
        
    $fracRes1 = (($decRes-1)*100)/50;
        
    $fracRes2 = ((($decRes-1)*100)/50)/($decRes-1);

        } elseif((((
    $decRes-1)*100)/25) == (int)((($decRes-1)*100)/25)) {
        
    $fracRes1 = (($decRes-1)*100)/25;
        
    $fracRes2 = ((($decRes-1)*100)/25)/($decRes-1);

        } elseif((((
    $decRes-1)*100)/20) == (int)((($decRes-1)*100)/20)) {
        
    $fracRes1 = (($decRes-1)*100)/20;
        
    $fracRes2 = ((($decRes-1)*100)/20)/($decRes-1);

        } elseif((((
    $decRes-1)*100)/10) == (int)((($decRes-1)*100)/10)) {
        
    $fracRes1 = (($decRes-1)*100)/10;
        
    $fracRes2 = ((($decRes-1)*100)/10)/($decRes-1);

        } elseif((((
    $decRes-1)*100)/5) == (int)((($decRes-1)*100)/5)) {
        
    $fracRes1 = (($decRes-1)*100)/5;
        
    $fracRes2 = ((($decRes-1)*100)/5)/($decRes-1);

        } elseif((((
    $decRes-1)*100)/4) == (int)((($decRes-1)*100)/4)) {
        
    $fracRes1 = (($decRes-1)*100)/4;
        
    $fracRes2 = ((($decRes-1)*100)/4)/($decRes-1);

        } elseif((((
    $decRes-1)*100)/2) == (int)((($decRes-1)*100)/2)) {
        
    $fracRes1 = (($decRes-1)*100)/2;
        
    $fracRes2 = ((($decRes-1)*100)/2)/($decRes-1);

        } else {
        
    $fracRes1 = (($decRes-1)*100)/1;
        
    $fracRes2 = ((($decRes-1)*100)/1)/($decRes-1);
        } 

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Its messy for one...one moment.
    I don't see the point, the third if statement is going to be true always.
    } elseif(($decRes-1) == (int)($decRes-1)) {
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might be more helpful if you could give some information as to the problem your having with the code?
    This would save people having to look through it all trying to spot possible errors and (hopefully) result in a faster response to help you correct it.
    Last edited by t3stin; Feb 8, 2009 at 11:37. Reason: typo.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $DecRes is a variable that is entered by the user, it can be to 2 decimal points. The code works with all of the elseif statements apart from it works irregularly (doesn't always work) with the following elseif statements:

    PHP Code:
    elseif(((($decRes-1)*100)/20) == (int)((($decRes-1)*100)/20)) {
    $fracRes1 = (($decRes-1)*100)/20;
    $fracRes2 = ((($decRes-1)*100)/20)/($decRes-1);

    } elseif((((
    $decRes-1)*100)/10) == (int)((($decRes-1)*100)/10)) {
    $fracRes1 = (($decRes-1)*100)/10;
    $fracRes2 = ((($decRes-1)*100)/10)/($decRes-1);

    } elseif((((
    $decRes-1)*100)/5) == (int)((($decRes-1)*100)/5)) {
    $fracRes1 = (($decRes-1)*100)/5;
    $fracRes2 = ((($decRes-1)*100)/5)/($decRes-1);

    } elseif((((
    $decRes-1)*100)/4) == (int)((($decRes-1)*100)/4)) {
    $fracRes1 = (($decRes-1)*100)/4;
    $fracRes2 = ((($decRes-1)*100)/4)/($decRes-1);

    } elseif((((
    $decRes-1)*100)/2) == (int)((($decRes-1)*100)/2)) {
    $fracRes1 = (($decRes-1)*100)/2;
    $fracRes2 = ((($decRes-1)*100)/2)/($decRes-1);

    Is there any reason that anyone can see why these conditions are not water tight? It's really baffling me. _thanks for anything

  5. #5
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All of them should all end up true, since if you replace $decRes with the number 5, you'll get this:

    PHP Code:
    elseif(20 == 20) {

    $fracRes1 20;
    $fracRes2 5;

    } elseif(
    40 == 40) {

    $fracRes1 40;
    $fracRes2 10;

    } elseif(
    80 == 80) {

    $fracRes1 80;
    $fracRes2 20;

    } elseif(
    100 == 100) {

    $fracRes1 100;
    $fracRes2 25;

    } elseif(
    200 == 200) {

    $fracRes1 200;
    $fracRes2 50;


    Although I don't really know what you're trying to achieve

  6. #6
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. That's correct they should all end true (as will any integer). But they are ordered sequentially so the top condition that returns true is the only relevant one - in the original post you will see that your example of $decRes = 5 will mean that $fracRes1 = 4 and $fracRes2 = 1, as they should they are returned like this. If $decRes = 5.5, the next condition down is applicable, so $fracRes1 = 9 and $fracRes2 = 2. The first three conditions work as they should do but the conditions after are temperamental (work sometimes)...

    For example; if $decRes = 5.2, then applying the first relevant condition:

    PHP Code:
    elseif(((($decRes-1)*100)/20) == (int)((($decRes-1)*100)/20)) {
    $fracRes1 = (($decRes-1)*100)/20;
    $fracRes2 = ((($decRes-1)*100)/20)/($decRes-1);

    $fracRes1 = 21 and $fracRes2 = 5, as they should. But using the same condition with $decRes = 3.2, $fracRes1 = 220 and $fracRes2 = 100. So it has completely ignored those conditions where it is applicable and skipped to the else statement. This as well as other examples I cannot understand and hope someone can help me to. Hopefully the problem is a little clearer now. Your input is greatly appreciated.
    _Thanks_

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    I just can't make sense of what you're trying to achieve. If I may be so blunt, what is the purpose of the code; given some inputs, what are the expected outputs?
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    converting decimal odds into fractional odds

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Location
    Pakistan
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    what is your problem with this code?

  10. #10
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    some parts of it don't always work as mentioned in my penultimate post and I have no reason why.

  11. #11
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can I take it that no-one can conquer this one?

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After having read both this thread and your other one, I still don't really know what you're trying to do.

    I know sometimes it can be difficult to explain yourself, but based on the replys in your threads(this and http://www.sitepoint.com/forums/showthread.php?t=597234), you need to somehow do a better job of it.

    Spends some time on it, provide specific examples. Be detailed and clear.

  13. #13
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let's be serious, a PHP problem can be solved if you ask a clear question. just copy pasting some code won't help you too much.


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
  •