Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Posts
    157
    Thanks
    6
    Thanked 4 Times in 4 Posts

    How to compare results and echo largest value

    I have this little script that takes the posted values from a form (yes or no radio buttons) and assigns each answer to a variable. Every time the answer is yes, that variable has a value of +1 added to it. So after the final results are calculated some variables may have a value of 4 and some with a value of 9. Here's a snippet of it:

    PHP Code:
    $one 0;
    if (
    $_POST['q2'] == 'yes')
    $one++;
    if (
    $_POST['q4'] == 'yes')
    $one++;

    $two 0;
    if (
    $_POST['q12'] == 'yes')
    $two++;
    if (
    $_POST['q13'] == 'yes')
    $two++;

    echo 
    'One: ' $one '<br />';
    echo 
    'Two: ' $two '<br />'
    What I'm trying to figure out though is how to check which variable has the highest value, after the points are added up, and echo that one first. Does anyone have an idea of how to do that?

    Thank you so much!

  • #2
    Regular Coder
    Join Date
    Dec 2006
    Location
    In the wilderness
    Posts
    106
    Thanks
    9
    Thanked 5 Times in 5 Posts
    PHP Code:
    echo max($one$two); 
    "The advantage of computers is that they do exactly what you tell them to do. The disadvantage of computers, on the other hand, is that they do exactly what you tell them to do."

    Excellent resource for learning PHP here

  • #3
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    $one 
    0;
    if (
    $_POST['q2'] == 'yes')
    $one++;
    if (
    $_POST['q4'] == 'yes')
    $one++;

    $two 0;
    if (
    $_POST['q12'] == 'yes')
    $two++;
    if (
    $_POST['q13'] == 'yes')
    $two++;

    if(
    $one $two)
    {
        echo 
    "One has the greater value with ".$one."<br />";
    }
    elseif(
    $two $one)
    {
        echo 
    "Two has the greater value with ".$two."<br />";    
    }
    else
    {
        echo 
    "Values One and Two have the same value of ".$one."<br />";    
    }

    echo 
    'One: ' $one '<br />';
    echo 
    'Two: ' $two '<br />';  

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #4
    Regular Coder
    Join Date
    Mar 2007
    Posts
    157
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Wow that's so simple and works great! Thank you!

    What do I do if I have more than two variables? Would I write it out kind of like this?

    PHP Code:
    if($one $two$three$four

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    No. should be like
    PHP Code:
    if($one $two && $one $three && $one $four
    See http://php.net/manual/en/language.operators.logical.php
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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