1

Update

With this code I can retrieve the difference, but I can see only the first athlete of the query. Why? Shouldn't the while loop for x times, where x is the number of results?

function difference() {

global $db;

$result = $db->query("
        SELECT *
        FROM maxithlon
        WHERE owner = '". $_SESSION[teamid] ."'
        AND season = '". $this->season ."'
        AND week = '". $this->currentWeek ."'
        ORDER BY RAND();
");

while ($row = $result->fetch_assoc()) {

$result2 = $db->query("
        SELECT *
        FROM maxithlon
        WHERE owner = '". $_SESSION[teamid] ."'
        AND season = '". $this->season ."'
        AND week = '". $this->currentWeek ."-1'
        AND athleteId = '". $row[athleteId] ."'
");

while ($row2 = $result2->fetch_assoc()) {


    $difference[$row2[athleteId]][form] = $row[form] - $row2[form];
    $difference[$row2[athleteId]][maxid] = $row[maxid] - $row2[maxid];
    $difference[experience] = $row[experience] - $row2[experience];
    $difference[mood] = $row[mood] - $row2[mood];
    $difference[strenght] = $row[strenght] - $row2[strenght];
    $difference[stamina] = $row[stamina] - $row2[stamina];
    $difference[speed] = $row[speed] - $row2[speed];    
    $difference[agility] = $row[agility] - $row2[agility];  
    $difference[jump] = $row[jump] - $row2[jump];
    $difference[throws] = $row[throws] - $row2[throws];
    $difference[specialty1] = $row[specialty1] - $row2[specialty1];
    $difference[specialty2] = $row[specialty2] - $row2[specialty2];
    $difference[height] = $row[height] - $row2[height];
    $difference[weight] = $row[weight] - $row2[weight];
    $difference[fans] = $row[fans] - $row2[fans];
    $difference[wage] = $row[wage] - $row2[wage];

return($difference);
    }
    }

}

I've searched for an answer that could satisfy my needings, but I couldn't find it.

I have a database, with the "maxithlon" table that contains the athletes' data. The two functions are defined to retrieve the athletes' data and to put them in a array. The first retrieves the data of the last week, the second the data of the current one.

I need to compare the two arrays to obtain the difference between the values of the two weeks. Do you see a possible solution?

6
  • Do you mean mathematical difference between the actual values or the number of values that are different? You need to be more specific about "compare the two arrays to obtain the difference between the values of the two weeks". Commented Dec 11, 2012 at 14:39
  • The mathematical difference! I'm editing the question to let you see the actual structure of the array. Commented Dec 11, 2012 at 14:41
  • what is your table schema and what output do you want from your comparison? i.e. if col1 > col2 but col3 == col4 and col5 != col6, what's the output? Commented Dec 11, 2012 at 14:42
  • Can you provide the output of var_dump($current); please? Commented Dec 11, 2012 at 14:45
  • predator17.com/downloads/var_dump($current).txt Commented Dec 11, 2012 at 14:55

3 Answers 3

2

What you can do is make an array of all of the keys you want to find the difference between the values.

$criteria = array('stamina', 'speed', 'agility', /*...*/);
$differences = array();
foreach($criteria as $key)
{
    $differences[$key] = $current[$key] - $last[$key];
}

That is assuming $current and $last are arrays containing the data about a given athlete's current and previous week's results (so the second level of the array you posted in the comments).

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, let me try this out, thanks! I'll report the results asap.
predator17.com/downloads/var_dump.txt I've modified the code in the first post!
$current_key should be changed to $key in the foreach($criteria as $key) loop
No, I'm far away from the result i wanted. I tried to see the problem by another point of view, you can see the result in the first post.
Anyway, you deserve a ^ :)
0

You can use array_diff(). Here's the manual

4 Comments

"Returns an array containing all the entries from array1 that are not present in any of the other arrays." I already checked it, and it didn't help. What I need is a comparison of the actual values of the array, not of the existence of elements.
Isn't that the definition of difference. If you need to compare, you can use array_cmp
I meant the mathematical difference, sorry for the misunderstanding.
ooh, then you can just subtract the values. Use array_values() to get just the values of both arrays, and then subtract each value from its corresponding (other) array value, inside a foreach loop.
0

just loop through the array and perform comparission operations if that satisfy your need since no direct function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.