php - Serialize array into $_POST over and over -
i'm practicing building blackjack game , i'm stuck being able hit twice. here's code:
if (empty($_post)) { ($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 works great if press "hit" again, $phand array changes different. how keep same value first "hit" can added onto 2nd, 3rd etc. "hit"?
consider using $_session store data server side between submissions instead.
something along lines of:
<?php session_start(); if (empty($_post)) { ($i=0; $i<2; $i++) { $phand[] = array_shift($deck); $dhand[] = array_shift($deck); } $_session['deck'] = $deck; $_session['phand'] = $phand; $_session['dhand'] = $dhand; } elseif ($_post['hit'] == "hit") { $deck = $_session['deck']; $phand = $_session['phand']; $dhand = $_session['dhand']; $phand[] = array_shift($deck); $_session['deck'] = $deck; $_session['phand'] = $phand; $_session['dhand'] = $dhand; }
Comments
Post a Comment