Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/266540-php-while-loop-doesnt-work/
Share on other sites

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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