0

I'm trying to create a custom medals/awards system for a bulletin board script.

The part I'm having issues with is the task that runs every 10 minutes to check for users who are eligible for medals.

Here's my current code:

// select all medals from database
$query = $db->simple_select("medals", "*", "");
$medals = $db->fetch_array($query);

// select users
$query_users = $db->simple_select("users", "*", "");
$results = $db->fetch_array($query_users);

foreach($medals as $medal)
{
    foreach($results as $result)
    {
        if($result['postnum'] >= $medal['postnum'])
        {
            $insert = array( 
                "mid" => $medals['id'],
                "muid" => $result['uid'],
                "mtime" => TIME_NOW,
            ); 

            $db->insert_query("users_medals", $insert);                 
        }
    }
}

The intention for the above code is to query the medals table which holds all the medals which eligible to be "won" by users. The users table is pretty self explanatory - it holds the users of the forum board. When users have meet the requirements for a medal, the medal is inserted into the user_medals table with the medal id, user id, and the time the medal was awarded.

However, each time the task is ran, it inserts over 30 rows (I'm using a test board with only two users, so it should only be inserting 1 row, as only 1 user has met the requirements for the medal for users with at least 2 posts).

Could someone help me with this?

Thanks.

4
  • can you post what you get from var_dump($medals) and var_dump($results) Commented Aug 11, 2011 at 22:59
  • It seems to me that you are comparing every postnum value in the medals db with every postnum value in the results db. Wouldn't you want to compare only one against one? Like, compare 1st row/column of results with the first row/column of medals, and so on? I feel like if you're comparing everything with everything, you're bound to get multiple TRUE responses to your IF statement. Commented Aug 11, 2011 at 23:11
  • I fixed the issue. I was using the wrong variable... Commented Aug 11, 2011 at 23:17
  • @Spencer: If you feel your solution will benefit others, can you post it as an answer and mark it as accepted? Otherwise, you can close this question. Commented Aug 12, 2011 at 14:50

1 Answer 1

1

The problem is here:

if($result['postnum'] >= $medal['postnum'])

Output the value of $result['postnum'] and $medal['postnum'] with each loop to examine their values. You will find that the first value is always greater than or equal to the second value, so the loop always executes.

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

3 Comments

Is this because he's using $result and $medal instead of $results and $medals? I notice that these are the values and not the arrays. Is there a better way to write this? I've never seen a foreach loop within a foreach loop except for multidimensional arrays. Is the double foreach loop valid?
@bozdoz: The OP is referencing the variables correctly. The lines foreach($medals as $medal) and foreach($results as $result) create variables named $medal and $result, respectively. The format of the loop depends on his data structure, but looks like a workable (if not optimized) solution in this case.
But this will check every result against every medal, correct? Foreach($medals as $medal) will run through every medal, and for every medal it will call foreach($results as $result) which will run through every result. I feel like it would be better to go through $results only once: perhaps a switch statement would work better: e.g. switch($result['postnum'])...case ($result['postnum'] >= 200):...bronze...case ($result['postnum'] >= 500):...silver...etc.

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.