Kane250 Posted May 11, 2008 Share Posted May 11, 2008 Hi, I'm wrote a code to take a session number that I provide. In this case, I want it to start at 7. After each run, I want that number to randomly increase or decrease by 1, 2, or 3. And I need the code to not allow this to go lower than 1 or higher than 13. I can't seem to get it quite right. It works okay most of the time, but sometimes it goes to -3 (that's the lowest, it's went). Any ideas what could be wrong here? <?php $_SESSION['kindnessScore'] = 7; //grab the session variable $kindnessScore = $_SESSION['kindnessScore']; // create a random var to figure out if we should add/subtract or do nothing to the score $addOrSub=rand(-1,1); // create a random var to decide how much to move to the score $amountToMove = rand(1,3); // if addOrSub is 0 don't do anything if($addOrSub!=0){ // add either the positive or neg value to the score $kindnessScore+=$amountToMove*$addOrSub; // if the score is within the bounds(13,1) of our bars update the score/session var, otherwise keep the current score if($kindnessScore>13||$kindnessScore<1){ $kindnessScore=$_SESSION['kindnessScore']; } else{ $_SESSION['kindnessScore'] =$kindnessScore; }?> Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/105187-help-with-sessions-and-numbers/ Share on other sites More sharing options...
fred123 Posted May 12, 2008 Share Posted May 12, 2008 Can't see anything wrong, other than the fact that you haven't ended the first IF statement. I don't know if that could be the problem in some php versions? Link to comment https://forums.phpfreaks.com/topic/105187-help-with-sessions-and-numbers/#findComment-538632 Share on other sites More sharing options...
dezkit Posted May 12, 2008 Share Posted May 12, 2008 you forgot to close if($addOrSub!=0){ <?php $_SESSION['kindnessScore'] = 7; //grab the session variable $kindnessScore = $_SESSION['kindnessScore']; // create a random var to figure out if we should add/subtract or do nothing to the score $addOrSub=rand(-1,1); // create a random var to decide how much to move to the score $amountToMove = rand(1,3); // if addOrSub is 0 don't do anything if($addOrSub!=0){ // add either the positive or neg value to the score $kindnessScore+=$amountToMove*$addOrSub; } // if the score is within the bounds(13,1) of our bars update the score/session var, otherwise keep the current score if($kindnessScore>13||$kindnessScore<1){ $kindnessScore=$_SESSION['kindnessScore']; } else{ $_SESSION['kindnessScore'] =$kindnessScore; } ?> Link to comment https://forums.phpfreaks.com/topic/105187-help-with-sessions-and-numbers/#findComment-538636 Share on other sites More sharing options...
Kane250 Posted May 12, 2008 Author Share Posted May 12, 2008 you forgot to close if($addOrSub!=0){ hmm, I made that change and now I get a "Parse error: syntax error, unexpected '}" Link to comment https://forums.phpfreaks.com/topic/105187-help-with-sessions-and-numbers/#findComment-538685 Share on other sites More sharing options...
Kane250 Posted May 12, 2008 Author Share Posted May 12, 2008 Nevermind, looks like it was just the } on the end that was an extra. However, it's still pulling numbers out that it shouldn't. Out of the range I specified....Any idea how? This code repeats over a series of 10 pages, so the number is constantly changing. Looks as if it has pulled the lowest number as being -3 and the highest being 14, even though it's supposed to stay within 1 and 13. Link to comment https://forums.phpfreaks.com/topic/105187-help-with-sessions-and-numbers/#findComment-538693 Share on other sites More sharing options...
thomashw Posted May 12, 2008 Share Posted May 12, 2008 Why not just use this if-statement. Yours is confusing. if( $kindnessScore <= 13 && $kindnessScore >= 1 ) $_SESSION['kindnessScore'] = $kindnessScore; It's much simpler than using an if/else statement. I don't see how it can't work now. Link to comment https://forums.phpfreaks.com/topic/105187-help-with-sessions-and-numbers/#findComment-538706 Share on other sites More sharing options...
Kane250 Posted May 12, 2008 Author Share Posted May 12, 2008 Why not just use this if-statement. Yours is confusing. if( $kindnessScore <= 13 && $kindnessScore >= 1 ) $_SESSION['kindnessScore'] = $kindnessScore; It's much simpler than using an if/else statement. I don't see how it can't work now. Thanks, but I don't know if that changed much. It seems to work well on the first page, but then as the pages go on, it starts going past the limits again... Link to comment https://forums.phpfreaks.com/topic/105187-help-with-sessions-and-numbers/#findComment-538711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.