Jump to content

PHP Variable Stacking


StormTheGates

Recommended Posts

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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

[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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.