Brent Crouch Posted April 26, 2009 Share Posted April 26, 2009 Here is a piece of php code that I use on a smarty based website. It works fine on that site. I'm trying to modify it to work on a non-smarty site. <?php $_wc_ab = explode(",", $_COOKIE["_wc_ab"]); if ($_COOKIE["_wc_ab"] == "") { $_wc_ab = array(); for($i=1; $i<=20; $i++) { $_wc_ab[] = (rand(0,1) == 0) ? "A" : "B"; } setcookie("_wc_ab",join(",",$_wc_ab),time()+(60*60*24*7),"/"); } for($i=0; $i<count($_wc_ab); $i++) $smarty->assign("wc_cookie_" . ($i+1), $_wc_ab[$i]); ?> Basically, when a visitor visits the site, the script checks to see if they already have a cookie named _wc_ab. If not, it creates the cookie and assigns it a string of 20 randomly generated As and Bs separated by commas. This portion of the script is working fine on my non-smarty based site. I'm having trouble figuring out the syntax of retrieving the cookie array and splitting it into 20 separate variables named wc_cookie_1, wc_cookie_2, wc_cookie_3, etc It obviously isn't for($i=0; $i<count($_wc_ab); $i++) $smarty->assign("wc_cookie_" . ($i+1), $_wc_ab[$i]); since this site doesn't use smarty. Thanks for any help anyone can give. Quote Link to comment https://forums.phpfreaks.com/topic/155753-retrieve-array-from-cookie-and-assign-to-variables/ Share on other sites More sharing options...
sasa Posted April 27, 2009 Share Posted April 27, 2009 <?php $test = array('A','B','C'); for ($i=0;$i<count($test);$i++){ ${"wc_cookie_" . ($i+1)} = $test[$i]; } echo $wc_cookie_2; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155753-retrieve-array-from-cookie-and-assign-to-variables/#findComment-820058 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.