Wednesday, 7 August 2013

Serialize array into $_POST over and over

Serialize array into $_POST over and over

I'm practicing building a blackjack game and I'm stuck with being able to
hit twice. Here's my code:
if (empty($_POST)) {
for ($i=0; $i<2; $i++) {
$pHand[] = array_shift($deck);
$dHand[] = array_shift($deck);
}
$deck2 = serialize($deck);
$pHand2 = serialize($pHand);
$dHand2 = serialize($dHand);
}
elseif ($_POST['hit'] == "Hit") {
$deck = unserialize($_POST['deck']);
$pHand = unserialize($_POST['pHand']);
$dHand = unserialize($_POST['dHand']);
$pHand[] = array_shift($deck);
$deck2 = serialize($pHand);
$pHand2 = serialize($dHand);
$dHand2 = serialize($deck);
}
<form method="post">
<input type="hidden" name="pHand" value="<?php echo
htmlspecialchars($pHand2, ENT_QUOTES, 'UTF-8'); ?>">
<input type="hidden" name="dHand" value="<?php echo
htmlspecialchars($dHand2, ENT_QUOTES, 'UTF-8'); ?>">
<input type="hidden" name="deck" value="<?php echo
htmlspecialchars($deck2, ENT_QUOTES, 'UTF-8'); ?>">
This all works great but if I press "Hit" again, my $pHand array changes
into something completely different. How do I keep the same value from the
first "Hit" so that it can be added onto the 2nd, 3rd etc. "Hit"?

No comments:

Post a Comment