3

I have a "what character are you" quiz website that is built in PHP for an assignment. I also need to store how many people get each of the 5 possible characters.

I have a text file with just the text - 0 0 0 0 0 Each 0 is a counter for the amount of times someone picks a specific character.

I explode it into an array

$txt = "results2.txt";
$scores = file_get_contents($txt);
$editablescores = explode(",", $scores);

Then depending on the score someone receives I want to add +1 to the appropriate 0 in the array.

This is an example of what I am using but it doesn't work. The following error comes up. Notice: Undefined offset: 4 in /Users/sinclaa3/Sites/PHPStyles/hi.php on line 53

0 0 0 0 0 is displayed but then 1 is added on to that as such. 0 0 0 0 0 1

if ($score < 6 ) {

$editablescores[0]++;
    //0 denotes the position in the array that I want to add one to

};



$storablescores = implode(",", $editablescores);
file_put_contents($txt, $storablescores);
8
  • "website that is built in PHP for an assignment" So you're asking us to do your homework for you? Commented Jan 14, 2014 at 21:02
  • 1
    Are you able to use a database instead of a text file? That should simplify things greatly. Commented Jan 14, 2014 at 21:03
  • 4
    What does "doesn't work" mean? "Doesn't work" is an inadequate description for us to understand the problem. What happened when you tried it? Did you get an error message? Did you get incorrect results? Did you get no results? If the results were incorrect, what made them incorrect? What were you expecting instead? Did you get any correct results? If so, what were they? Don't make us guess. Commented Jan 14, 2014 at 21:03
  • @PatrickQ: There is nothing wrong with users asking for help with homework. Commented Jan 14, 2014 at 21:03
  • 1
    Well, it would work for 1 user at a time, but without locking, it's terribly susceptible to race conditions. Either add locking (and suffer the waits that will cause), or use other storage options: databases (sqlite seems an option), memcached and the like, and possibly others which fix that locking & atomic operations for you. Commented Jan 14, 2014 at 21:04

1 Answer 1

4

Your explode is wrong; you say you have 0 0 0 0 0, then you try to explode on ,. Fixed:

$editablescores = explode(" ", $scores);

Note that your implode is wrong for the same reason; it should be:

$storablescores = implode(" ", $editablescores);
Sign up to request clarification or add additional context in comments.

5 Comments

Actually he also implodes on "," so most likely the 0 0 0 0 0 is wrong.
Yes this appears that the input text of 0 0 0 0 0 is wrong and should be 0,0,0,0,0.
Unless the original file was created by hand with spaces instead of commas. In which case, this answer is right and it would also indicate why it has never worked.
The original file was created by hand
@user3195675 Whether it was created by hand doesn't matter; what separates the numbers matters. If it is a space, then my answer addresses your problem. If it's tabs, then use "\t" instead of " ". If it really is comma-separated, then something else is going on.

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.