0

There is probably something dumb I am doing wrong here.

public function get_scores($id)
{
    $results = array();
    $sql = "SELECT * FROM scores WHERE comp_id = $id";
    $rows = $this->db->query($sql)->result();
    foreach($rows as $row) {
        if($row->confirmed_id) {
            $results[$row->uid] += $row->score;
        }
    }
    sort($results);
    return $results;
}

So basically what I am trying to do is add all of the users scores in the database and return them in order of rank. confirmed->id is just a check to make sure the score has been confirmed (and thus is addable to their total score). I am basically trying to just make an associative array where the key is the users ID, and the score of each question they have in the database is added on. The query works fine, and $row-uid and $row->score both return the correct thing for every row, but $results[] never has anything added to it. If I even change it just to something silly like $results[3] = 0 at top, and then $results[3]++ or += 1 in the for loop, it doesn't add anything to $results[3].

EDIT: Problem solved. Indeed was something dumb -- confirmed_id was set to null by my partner when he reran our database after I had previously set it all to 1. Thanks guys :)

4
  • Can you just check without '+' ie. $results[$row->uid] = $row->score; Commented Mar 22, 2012 at 4:31
  • 1
    What is the value of $row->confirmed_id for each row? Is it ever truthy? Commented Mar 22, 2012 at 4:32
  • 2
    You have something against curly braces or using aggregate SQL functions? Your code could benefit from both :) Commented Mar 22, 2012 at 4:33
  • Sigh. Embarrassing. My coding partner set them all to NULL in our database for some reason. After I had all set them to 1. What a dumb problem. Commented Mar 22, 2012 at 4:44

2 Answers 2

2

You are adding to $results[something] before it exists. You need to create it in the first case and then only increment it once it exists.

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

8 Comments

Well it is at least a problem.
This isn't -- I have added in $results[3] = 0 at the top, and it doesn't fix it previously. [3] is my test case that all current uids are.
I think you misunderstand. Initially $results[anyid] is undefined. It doesn't exist. Thus when you do $results[anyid] += 3, you are doing $results[anyid] = undefined + 3. Which won't do what you want.
Right -- which is why I was saying at the top of my code I was setting it $result[3] to 0, since I know all of my current id's are 3 anyway. So it would be doing $result[3] = 0 + x. Thanks though =]
The fact remains that the code you posted has a blatant defect. So since you keep insisting it isn't wrong I'm downvoting the question until you fix it.
|
0

You need to remove the "+=" operation from the code. Check with this.

public function get_scores($id)
{
    $results = array();
    $sql = "SELECT * FROM scores WHERE comp_id = $id";
    $rows = $this->db->query($sql)->result();
    foreach($rows as $row)
        if($row->confirmed_id)
            $results[$row->uid] = $row->score;
    sort($results);
    return $results;
}

Your previous operation is similar to

$results[$row->uid] = $results[$row->uid] + $row->score; 

So it will not add values to your row.

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.