StormTheGates Posted May 12, 2007 Share Posted May 12, 2007 Any help is appreciated. for($num=0; $num<=9; $num+=1){ $exsettings='$settings['.$num.']'; eval("\$exsettings = \"$exsettings\";"); if($exsettings=='1'){ $check$num='checked'; }else{ $check$num=''; } } Basically I need the variables to stack, for instance $check$num would wind up to be increasing such as $check1 $check2 $check3. How do I make it do this? Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/ Share on other sites More sharing options...
hitman6003 Posted May 12, 2007 Share Posted May 12, 2007 I think what you want is... ${$check$num} = 'value'; Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251065 Share on other sites More sharing options...
StormTheGates Posted May 12, 2007 Author Share Posted May 12, 2007 Nope, dosnt work. Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251067 Share on other sites More sharing options...
Wo0tHigh Posted May 12, 2007 Share Posted May 12, 2007 I am also having very similar problems, no idea what to do, any more thoughts anyone? Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251069 Share on other sites More sharing options...
hitman6003 Posted May 12, 2007 Share Posted May 12, 2007 oops, forgot the concatenation operator... ${$check . $num} = 'value'; EDIT: An example: $check = 'value'; for ($i = 1; $i < 10; $i++) { ${$check . $i} = $i + 10; } echo $value1 . '<br />'; echo $value2 . '<br />'; echo $value3 . '<br />'; echo $value4 . '<br />'; echo $value5 . '<br />'; echo $value6 . '<br />'; echo $value7 . '<br />'; echo $value9 . '<br />'; echo $value9; Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251072 Share on other sites More sharing options...
Wo0tHigh Posted May 12, 2007 Share Posted May 12, 2007 This is what I tried, <?php $userinfoget=mysql_query("SELECT `sms_credits`,`sms_phone`,`sms_settings` FROM `users` WHERE `username` = '$username'") or die(mysql_error()); $userinfo=mysql_fetch_assoc($userinfoget); $settings=explode(",",$userinfo[sms_settings]); $check='value'; for($num=0; $num<=9; $num++){ $exsettings='$settings['.$num.']'; eval("\$exsettings = \"$exsettings\";"); if($exsettings=='1'){ ${$check.$num}='checked'; }elseif($exsettings=='0'){ ${$check.$num}=''; } } echo"0 = $check0<br>1 = $check1<br>1 = $check2<br>3 = $check3<br>4 = $check4<br>5 = $check5<br>6 = $check6<br>7 = $check7<br>8 = $check8<br>9 = $check9<br>"; ?> It returned this... 0 = 1 = 1 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = Still doesnt work Im sure theres a way of doing this though! Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251079 Share on other sites More sharing options...
neel_basu Posted May 12, 2007 Share Posted May 12, 2007 You need to use $GLOBALS Array if($exsettings=='1'){ $GLOBALS[$check.$num]='checked'; }elseif($exsettings=='0'){ $GLOBALS[$check.$num]=''; } Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251136 Share on other sites More sharing options...
trq Posted May 12, 2007 Share Posted May 12, 2007 Im sure theres a way of doing this though! Did you even bother trying hitman's method? It works! Its called variable variables and this is exactly its purpose. You need to use $GLOBALS Array What does it have to do with the $GLOBALS array? Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251143 Share on other sites More sharing options...
neel_basu Posted May 12, 2007 Share Posted May 12, 2007 What does it have to do with the $GLOBALS array? He was talking that he need to make variables like $check1, $check2, $check3, $check4, $check5, ................... Thats why $GLOBALS Array will work here if($exsettings=='1'){ $GLOBALS['check'.$num]='checked'; }elseif($exsettings=='0'){ $GLOBALS['check'.$num]=''; } OOPS The previous one had a mistake. Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251146 Share on other sites More sharing options...
trq Posted May 12, 2007 Share Posted May 12, 2007 [code]The problem is this piece of code.... [code] $exsettings='$settings['.$num.']'; eval("\$exsettings = \"$exsettings\";"); and the fact that $exsettings never equals 0 or one, hence the if() never runs. Try... <?php $settings = explode(",",$userinfo['sms_settings']); $check='value'; for ($num=0; $num<=9; $num++) { if ($settings[$num] == 1) { ${$check.$num} = 'checked'; } elseif ($settings[$num] == 0){ ${$check.$num} = ''; } } echo"0 = $check0<br>1 = $check1<br>1 = $check2<br>3 = $check3<br>4 = $check4<br>5 = $check5<br>6 = $check6<br>7 = $check7<br>8 = $check8<br>9 = $check9<br>"; ?> Thats why $GLOBALS Array will work here Sorry, but I still just don't see the point.[/code][/code] Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251149 Share on other sites More sharing options...
neel_basu Posted May 12, 2007 Share Posted May 12, 2007 There are thousand ways to do 1 thing you are doing it without using $GLOBALS array but it can be done using $GLOBALS Array also. Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251151 Share on other sites More sharing options...
trq Posted May 12, 2007 Share Posted May 12, 2007 There are thousand ways to do 1 thing you are doing it without using $GLOBALS array but it can be done using $GLOBALS Array also. We could put all our variables in the $GLOBALS array if we wanted, but there wouldn' t be much point would there. My point is there is no point to your method. Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251154 Share on other sites More sharing options...
Wo0tHigh Posted May 12, 2007 Share Posted May 12, 2007 Hey, Thanks very much for your help, the $GLOBALS method worked perfectly! Did you even bother trying hitman's method? It works! Its called variable variables and this is exactly its purpose. [/quote Yep, tried it, unless I was doing something wrong it didnt work in the way i needed it to, $GLOBALS did the trick Also, it was stored in the database as "0,0,0,0,0,0,0,0,0,0", and was either set to 1 or 0 Thanks for your help once again Quote Link to comment https://forums.phpfreaks.com/topic/51020-php-variable-stacking/#findComment-251205 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.