Jump to content

help with sessions and numbers..


Kane250

Recommended Posts

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

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;
	}
	?>

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.

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.