#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    23
    Rep Power
    0

    Array function not working


    Hey, I am really confused why this will not work.

    PHP Code:
    $firstarray     =$array[0]['COUNT(`prediction`)']; //currently =7
    $secondarray     =$array[1]['COUNT(`prediction`)']; //currently =6
    $thirdarray     =$array[2]['COUNT(`prediction`)']; //currently =6
    $fourtharray     =$array[3]['COUNT(`prediction`)']; //currently =6


    function first_two_tie($firstarray$secondarray){
    if(
    $firstarray==$secondarray){
    return 
    true;
    }
    }

    function 
    second_two_tie ($secondarray$thirdarray){
    if(
    first_two_tie($firstarray$secondarray)==false){
    if(
    $secondarray==$thirdarray){
    return 
    true;
    }
    }
    }

    function 
    second_three_tie ($thirdarray$fourtharray){
    if(
    second_two_tie($secondarray$thirdarray)==true){
    if(
    $thirdarray==$fourtharray){
    return 
    true;
    }
    }
    }

    if (
    second_two_tie ($secondarray$thirdarray)==true){
    echo 
    'works 1';
    //currently works

    if (second_three_tie ($thirdarray$fourtharray)==true){
    echo 
    'works 2';
    }
    //currently doesnt work 
    It does not make sense to me. If I change with in the second_three_tie function second_two_tie($secondarray, $thirdarray)==false) then both work, which does not make any sense.

    Any help would be greatly appreciated
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,615
    Rep Power
    1945
    My comments about you logic. I hope it will be helpful, although I don't know how much it will help with your actual issue/question:

    First, I think you should change the logic of the functions, so it does not use a mix of global and parameter-based value.
    e.g. in second_two_tie you are using $firstarray.

    Secondly, you should also change the names of the function parameter. The function doesn't need to know that the values to be compared is coming from array.

    The code would then be something like this: (untested)
    PHP Code:
    $firstarray   =$array[0]['COUNT(`prediction`)']; //currently =7
    $secondarray  =$array[1]['COUNT(`prediction`)']; //currently =6
    $thirdarray   =$array[2]['COUNT(`prediction`)']; //currently =6
    $fourtharray  =$array[3]['COUNT(`prediction`)']; //currently =6 

    function first_two_tie($firstvalue$secondvalue){
        if(
    $firstvalue==$secondvalue){
            return 
    true;
        }
    }

    function 
    second_two_tie (firstvalue$secondvalue$thirdvalue){
        if(
    first_two_tie($firstvalue$secondvalue)==true){
            if(
    $secondvalue==$thirdvalue){
                return 
    true;
            }
        }
    }

    function 
    second_three_tie ($firstvalue$secondvalue$thirdvalue$fourthvalue){
        if(
    second_two_tie(firstvalue$secondvalue$thirdvalue)==true){
            if(
    $thirdvalue==$fourthvalue){
                return 
    true;
            }
        }
    }

    if (
    second_two_tie ($firstarray$secondarray$thirdarray)==true){
        echo 
    'works 1';
    //currently works

    if (second_three_tie ($firstarray$secondarray$thirdarray$fourtharray)==true){
        echo 
    'works 2';
    }
    //currently doesnt work 
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    23
    Rep Power
    0
    Originally Posted by MrFujin
    My comments about you logic. I hope it will be helpful, although I don't know how much it will help with your actual issue/question:

    First, I think you should change the logic of the functions, so it does not use a mix of global and parameter-based value.
    e.g. in second_two_tie you are using $firstarray.

    Secondly, you should also change the names of the function parameter. The function doesn't need to know that the values to be compared is coming from array.

    The code would then be something like this: (untested)
    PHP Code:
    $firstarray   =$array[0]['COUNT(`prediction`)']; //currently =7
    $secondarray  =$array[1]['COUNT(`prediction`)']; //currently =6
    $thirdarray   =$array[2]['COUNT(`prediction`)']; //currently =6
    $fourtharray  =$array[3]['COUNT(`prediction`)']; //currently =6 

    function first_two_tie($firstvalue$secondvalue){
        if(
    $firstvalue==$secondvalue){
            return 
    true;
        }
    }

    function 
    second_two_tie (firstvalue$secondvalue$thirdvalue){
        if(
    first_two_tie($firstvalue$secondvalue)==true){
            if(
    $secondvalue==$thirdvalue){
                return 
    true;
            }
        }
    }

    function 
    second_three_tie ($firstvalue$secondvalue$thirdvalue$fourthvalue){
        if(
    second_two_tie(firstvalue$secondvalue$thirdvalue)==true){
            if(
    $thirdvalue==$fourthvalue){
                return 
    true;
            }
        }
    }

    if (
    second_two_tie ($firstarray$secondarray$thirdarray)==true){
        echo 
    'works 1';
    //currently works

    if (second_three_tie ($firstarray$secondarray$thirdarray$fourtharray)==true){
        echo 
    'works 2';
    }
    //currently doesnt work 
    Thanks for the response, maybe I should explain what I am trying to do further. I am checking to see if the values are the same or a tie. If there is a tie I will pay out points(different sheet not really important). Why I need to have if(first_two_tie($firstarray, $secondarray)==false), is because for there to be a tie for second it means that there was only one person who came in first. Hope this helps clear things up
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    there are some misunderstandings regarding booleans and some bad practices.

    When you compare two values using "==", the result already is a boolean. It's either true or false. There's no point in putting the expression into an if statement, because that doesn't make it "truer" or "falser". Things like "if ($exp == true)" are also pointless. What's the "== true" supposed to do? $exp itself already has a truth value: "if ($exp)".

    It's also kind of sloppy to have the function return a value in some cases and in some cases not. Yes, you can do that in PHP, but it's confusing and error-prone (your function won't work with strict comparison "===", for example).

    You should also make a general, "intelligent" function rather than repeating the same logic again and again for each case.

    PHP Code:
    <?php

    $values 
    = array(7666);

    function 
    tie($values$first_n) {
        if (!
    is_int($first_n) || !is_array($values) || $first_n count($values))
            return 
    null;
        elseif (
    count($values) < 2)
            return 
    false;
        elseif (
    $first_n == 2)
            return 
    $values[0] == $values[1];
        else {
            
    $tie false;
            if (!
    tie($values2)) {
                
    $tie true;
                
    $value $values[1];
                for (
    $i 2$i $first_n$i++)
                    if (
    $values[$i] != $value) {
                        
    $tie false;
                        break;
                    }
            }
            return 
    $tie;    
        }
    }


    var_dumptie($values3) );
    var_dumptie($values4) );
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    23
    Rep Power
    0
    can you explain why it is wrong to set up a function where if another function is true to do some other code?
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    I didn't say that. Where do you take that from?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    23
    Rep Power
    0
    I guess I read it wrong. I removed all of the ==true yet it still does not work. It works up until this function
    PHP Code:
    function second_three_tie ($secondarray$thirdarray$fourtharray){
    if(
    second_two_tie($secondarray$thirdarray)){
    if(
    $thirdarray==$fourtharray){
    echo 
    'works';

    }
    }

    I have checked by echoing out both $thirdarray and $forutharray they both = 6. When I change it to if($thirdarray==6) it works. But when I change it to if($fourtharray==6) it does not work any ideas?

IMN logo majestic logo threadwatch logo seochat tools logo