0

I'm struggling with some "simple" algorithm for some time now and after searching whole internet for the answer I gave up..... sad, I know. On the beginning I wanted to apologize for my English grammar etc.

Based on this query:


    SELECT a.*, b.*
    FROM mecze a
    JOIN bets b
    ON a.mecz_id = b.mecz_id AND a.wynik != 'NULL'

I have an array "punkty" with such values below:

    [0] => Array
        (
            [mecz_id] => 1           //match_id
            [druzyna_1_id] => 1      //team_1_id
            [druzyna_2_id] => 2      //team_2_id
            [wynik] => 1:2           //score - of the match
            [wynik_buk] => 2         //bookie score - of the match
            [bet_id] => 1            //users bet id
            [user_id] => 1           //user id :)
            [bet_wynik] => 3:2       //users bet score
            [bet_wynik_buk] => 1     //users bookie bet score
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0            //points - here will be added when calculated
        )

    [1] => Array
        (
            [mecz_id] => 2
            [druzyna_1_id] => 3
            [druzyna_2_id] => 4
            [wynik] => 3:2
            [wynik_buk] => 1
            [bet_id] => 2
            [user_id] => 1
            [bet_wynik] => 3:2
            [bet_wynik_buk] => 1
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

    [2] => Array
        (
            [mecz_id] => 3
            [druzyna_1_id] => 2
            [druzyna_2_id] => 4
            [wynik] => 1:1
            [wynik_buk] => 0
            [bet_id] => 3
            [user_id] => 1
            [bet_wynik] => 1:1
            [bet_wynik_buk] => 0
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

    [3] => Array
        (
            [mecz_id] => 1
            [druzyna_1_id] => 1
            [druzyna_2_id] => 2
            [wynik] => 1:2
            [wynik_buk] => 2
            [bet_id] => 6
            [user_id] => 4
            [bet_wynik] => 3:1
            [bet_wynik_buk] => 1
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

    [4] => Array
        (
            [mecz_id] => 2
            [druzyna_1_id] => 3
            [druzyna_2_id] => 4
            [wynik] => 3:2
            [wynik_buk] => 1
            [bet_id] => 7
            [user_id] => 4
            [bet_wynik] => 2:3
            [bet_wynik_buk] => 2
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

    [5] => Array
        (
            [mecz_id] => 3
            [druzyna_1_id] => 2
            [druzyna_2_id] => 4
            [wynik] => 1:1
            [wynik_buk] => 0
            [bet_id] => 8
            [user_id] => 4
            [bet_wynik] => 0:0
            [bet_wynik_buk] => 0
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

    [6] => Array
        (
            [mecz_id] => 1
            [druzyna_1_id] => 1
            [druzyna_2_id] => 2
            [wynik] => 1:2
            [wynik_buk] => 2
            [bet_id] => 9
            [user_id] => 5
            [bet_wynik] => 1:2
            [bet_wynik_buk] => 2
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

    [7] => Array
        (
            [mecz_id] => 2
            [druzyna_1_id] => 3
            [druzyna_2_id] => 4
            [wynik] => 3:2
            [wynik_buk] => 1
            [bet_id] => 10
            [user_id] => 5
            [bet_wynik] => 3:3
            [bet_wynik_buk] => 0
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

    [8] => Array
        (
            [mecz_id] => 3
            [druzyna_1_id] => 2
            [druzyna_2_id] => 4
            [wynik] => 1:1
            [wynik_buk] => 0
            [bet_id] => 11
            [user_id] => 5
            [bet_wynik] => 1:1
            [bet_wynik_buk] => 0
            [krol] => Mario Gomez (GER)
            [laczny] => 5 pkt � Hiszpania ( 2 pkt )
            [punkty] => 0
        )

Based on the array above I'm assigning points for getting a good score and exact result:
1 point when a player have correct score (i.e. 1 or X or 2)
4 points when a player have correct exact score (i.e. 1:1 or 3:1 etc.)


    foreach ( $punkty as $k => $v ){
        $wbuk = $v['wynik_buk'];         //match bookie score
        $bwbuk = $v['bet_wynik_buk'];    //users bookie bet score
        $w = $v['wynik'];                //match score
        $bw = $v['bet_wynik'];           //users bet score

        if( $wbuk == $bwbuk ) {
            if ( $w == $bw ) {
                $pkt = 4;
            }else{ 
                $pkt = 1; 
            }
        } else {
            $pkt = 0;
        }
    }

The above code is assigning the points correctly but I want to add another thing to it.

I want to add some bonus points for hitting the right score or right exact result if only one user has this score/result:
2 points if user is the only one with right score (i.e. 1 or X or 2)
1 point if user is the only one with right exact result (i.e. 3:1 or 1:1 etc.)

Please help !!! I know that there must be thousand of ways to do it but my mind is in StackOverflow state and crashed :)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gonti
  • 11
  • 1
  • I don't speak Polish so I don't understand what your variables mean. – Simon Forsberg May 15 '12 at 12:42
  • "wynik" (result) is the match result i.e. 3:1, 1:1 etc. "wynik_buk" (bookie result for the match<>) "bet_wynik" (bet result) is the users result ---- does it help at all? – Gonti May 15 '12 at 12:55
  • Sorry for that. I'll try to edit the original post and explain the variables – Gonti May 15 '12 at 13:00
  • A round about way would be to do a sql query with count on amount of users that has exact score and if count equals 1 add bonus points. – Nightwolf May 15 '12 at 13:08
  • Could you give me an example of such query based on above dataset and variables? I have 2 tables (mecze and bets) and 2 variables (mecz_id and user_id) which I have to use. I can't make such query... – Gonti May 16 '12 at 11:40

1 Answers1

0

Its tricky to give an answer as I can't understand the variables but I think I understand what you want to do.

To tell whether or not the user is the only one with the exact score or the exact result you have a couple of options (well a few but keeping it as simple as possible), run a query on each loop to see if the user is the only one with the correct value or write a function that uses the in_array function:

in_array() and multidimensional array

Edit:

Or before you get into this loop, run a query and create a 'score' array and manipulate that.

Community
  • 1
  • 1
Richard Askew
  • 1,217
  • 1
  • 10
  • 16