madmadmax Posted August 1, 2012 Share Posted August 1, 2012 if ($playercards>21 || $choice==1) { while($dealercards<17) { $key2 = array_rand($_SESSION['sesscards']); $pick9 = $_SESSION['sesscards'][$key2]; unset($_SESSION['sesscards'][$key2]); array_push($_SESSION['dealercards'], $pick9); $dealercards=EvaluateHand($_SESSION['dealercards']); $strd.="$pick9|"; } .... } This code seems to add the same card duplicated over and over again even though I use the same code outside of a while loop and it seems to work. Quote Link to comment https://forums.phpfreaks.com/topic/266540-php-while-loop-doesnt-work/ Share on other sites More sharing options...
peipst9lker Posted August 1, 2012 Share Posted August 1, 2012 Edit: PLease provide the code from EvaluateHands! Please put code in [php ] tags! (You can find a php-button in the editor) Quote Link to comment https://forums.phpfreaks.com/topic/266540-php-while-loop-doesnt-work/#findComment-1365950 Share on other sites More sharing options...
cyberRobot Posted August 1, 2012 Share Posted August 1, 2012 What does EvaluateHand() look like? For what it's worth, the following code works: <?php $sesscards = array(1,2,3,4,5,6,7,8,9,10,11,12); $dealercards = array(); //if ($playercards>21 || $choice==1) { while(array_sum($dealercards)<17) { $key2 = array_rand($sesscards); $pick9 = $sesscards[$key2]; unset($sesscards[$key2]); array_push($dealercards, $pick9); // $dealercards=EvaluateHand($dealercards); $strd.="$pick9|"; var_dump($dealercards); print '<br />'; } //} ?> Quote Link to comment https://forums.phpfreaks.com/topic/266540-php-while-loop-doesnt-work/#findComment-1365951 Share on other sites More sharing options...
jazzman1 Posted August 1, 2012 Share Posted August 1, 2012 First of all, you have to check the statement. This statement must be true: $dealercards<17 While Loops execute a block of code as long as a specified condition is true. Quote Link to comment https://forums.phpfreaks.com/topic/266540-php-while-loop-doesnt-work/#findComment-1365981 Share on other sites More sharing options...
madmadmax Posted August 2, 2012 Author Share Posted August 2, 2012 What does EvaluateHand() look like? For what it's worth, the following code works: <?php $sesscards = array(1,2,3,4,5,6,7,8,9,10,11,12); $dealercards = array(); //if ($playercards>21 || $choice==1) { while(array_sum($dealercards)<17) { $key2 = array_rand($sesscards); $pick9 = $sesscards[$key2]; unset($sesscards[$key2]); array_push($dealercards, $pick9); // $dealercards=EvaluateHand($dealercards); $strd.="$pick9|"; var_dump($dealercards); print '<br />'; } //} ?> Evaluate hand is irrelevant because it works great, the code also works great, the only problem is it adds the same card multiple times to $strd I get output like "5D|5D|5D|". Thanks everyone for being so helpful but especially you cyberRobot for actually testing this. Quote Link to comment https://forums.phpfreaks.com/topic/266540-php-while-loop-doesnt-work/#findComment-1366482 Share on other sites More sharing options...
cyberRobot Posted August 8, 2012 Share Posted August 8, 2012 Just in case you're still looking for help, have you tried displaying the session variable to make sure it contains what's expected as the loop executes? <?php //... $key2 = array_rand($_SESSION['sesscards']); $pick9 = $_SESSION['sesscards'][$key2]; unset($_SESSION['sesscards'][$key2]); var_dump($_SESSION['sesscards']); //see what the session variable looks like after unset() //... ?> Displaying the session variable should let you know if the value is being removed as expected. Quote Link to comment https://forums.phpfreaks.com/topic/266540-php-while-loop-doesnt-work/#findComment-1367778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.