Can somebody help me with this (simple program, messing up s

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KyZu
Forum Newbie
Posts: 17
Joined: Sun Apr 24, 2011 9:13 pm

Can somebody help me with this (simple program, messing up s

Post by KyZu »

Sorry for my noob question, I've learned about sessions a bit and tried to make a simple "game", basically a user has to enter one of the colors of the rainbow and after they name all 7, they win. Sounds simple enough, but I'm clearly making an obvious mistake and I've googled and read and experimented, but obviously something is alluding me.

Here is the pastebin:
http://pastebin.com/XmN4YLp8


I suppose what I want to know is...how do I store the values from the form (that is, $_POST['color']) and save that information to something like an array, because whatever I'm doing now is resetting the counter ($_SESSION['left']) and if a user guesses "red" for instance, it temporarily goes from 7 to 6, but then bumps up to 7?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can somebody help me with this (simple program, messing

Post by Christopher »

You reset it to 7 every time. You should only set it to 7 if (!isset($_SESSION['left'])) {
(#10850)
KyZu
Forum Newbie
Posts: 17
Joined: Sun Apr 24, 2011 9:13 pm

Re: Can somebody help me with this (simple program, messing

Post by KyZu »

That worked:
http://pastebin.com/Xi1hq9G6

Just wondering why? I think I understand it subtracting once, but why wouldn't it keep at 6 for example? Is it because the $_SESSION['left'] exists now nowhere else except at $_SESSION['left']-- ?

Also, there's just 2 small problems

- I have to make two correct guesses in order for the "You have " . $_SESSION['left'] . " colors left to guess" to start subtracting, why does it do this?
- Also, how do I prevent it from registering the same answer twice, like red/red/red/red/red/red/blue ?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Can somebody help me with this (simple program, messing

Post by McInfo »

KyZu wrote:Just wondering why?
$_SESSION['left'] is like any other variable in that it doesn't matter what value it held before. If you assign 7 to it, it will hold 7.
KyZu wrote:- I have to make two correct guesses in order for the "You have " . $_SESSION['left'] . " colors left to guess" to start subtracting, why does it do this?
Because the value is echoed before it is decremented. It should be echoed after it is decremented.
KyZu wrote:- Also, how do I prevent it from registering the same answer twice, like red/red/red/red/red/red/blue ?
a. You could store an array of remaining answers in the session instead of the number. When the user answers correctly, remove that answer from the array. Count the number of items in the array to find how many answers remain.

b. You could store an array of boolean values in the session related to whether each answer has been guessed. If you store the number 0 for false and 1 for true, the array sum will tell you how many answers remain. Be careful to store only 0 or 1.

c. You could store a 7-bit number in the session. Each bit would represent the status of an answer (0: not answered, 1: answered). Do some bit flipping to change the statuses. If the number equals 127 (1111111 in binary), all answers have been guessed.
KyZu
Forum Newbie
Posts: 17
Joined: Sun Apr 24, 2011 9:13 pm

Re: Can somebody help me with this (simple program, messing

Post by KyZu »

Thank you so much McInfo

I just had one last question and wont bother you guys about this anymore, just something I've been trying to figure out. Suppose you had a 'game' where someone had to guess a number and submit the answer from a form (like the one here), is there any way you could store their values in an array and echo them each time they're wrong?

Say the number was 50, and they typed '10', hit submit, '5' hit submit, '13' hit submit, can you save those values anywhere and perhaps echo them out after each incorrect guess? How would you write that?

sorry, no more stupid questions :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Can somebody help me with this (simple program, messing

Post by McInfo »

It wouldn't be much different from what you have already written. Instead of storing a number by itself in $_SESSION, you would store the number in a array in $_SESSION. Remember to initialize the array if it is not set. To convert the array to an output string, either echo each item in a loop or implode the array. It will probably be helpful to you to read what the manual has to say about arrays.
Post Reply