Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$_SESSION[ variable] syntax 1

Status
Not open for further replies.

overyde

Programmer
May 27, 2003
226
ZA
Hi,

I need to know a really simple thing but can't figure it out.
Say I have three $_SESSION[] variables eg:
$_SESSION[number1];
$_SESSION[number2];
$_SESSION[number3];

How would I get the digit to increment. I thought almot along these lines:
$i = 1;
$_SESSION[number$i];
$i++;

Reality is built on a foundation of dreams.
 
I thought that too...it is in a loop but it is the actual $_SESSION['number$i'] syntax I'm having problems with.

If I insert a ' then this is the error I get:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\Apache Group\Apache2\htdocs\creative\site\php\admin\adminad_month.php on line 272

Reality is built on a foundation of dreams.
 
Minor change there:
Code:
for($i=0; $i < 10; $i++){
  $_SESSION[[COLOR=red]"[/color]number$i[COLOR=red]"[/color]];
}
By using " instead of ' you force the contents to be parsed and variables resolved. Someone will hopefull be able to give a better description that that *lol*

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
by [number1] do you mean "1" or do you mean "sometext1" "sometext2" etc?

if the first then just use the syntax
Code:
$_SESSION[] = "first entry";
$_SESSION[] = "second entry";
print_r($_SESSION);

if you mean the second then you can make this work:

Code:
for ($i=0; $i<10; $i++):
 $_SESSION["sometext{$i}"] = "text entry ". $i;
endfor;
echo "<pre>";
print_r($_SESSION);
echo "</pre>";

remember if you use single quotes then variables are not expanded. you do not need to use the curlybraces to make this work but I find it more readable to do so if combining text and variables like above. you can, of course, also use the notation ['sometext'.$i] if you really want to make use of single quotes.



 
Problem isthat whenever I put in either ' or " I get this error:

Code:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Program Files\Apache Group\Apache2\htdocs\creative\site\php\admin\adminad_month.php on line 272.

jpadie...
it is $_SESSION[sometext$i]

Reality is built on a foundation of dreams.
 
have you tried copy and pasting the second code snip i left above? do you still get that error?

if so could you post the code you use within
Code:
 tags?
 
could work, but this is more in the context that I need it:

Code:
							   // show the file input field based on($num_files).
for ($i = 1; $i <= $num_files; $i++) {
echo "<tr><td class=\"text_content1_bold\">Video $i:</td>
<td><input type=\"file\" name=\"admonthvid". $i ."\" class=\"form_textblock2\" value=\"$_SESSION["admonthvid{$i}"]\"></td></tr>";
					           }

Reality is built on a foundation of dreams.
 
the problem is in your $_SESSION use. you're using double quotes inside double quotes which doesn't work. i'd duck out of the quoted string and concatenate the value instead.

i've fixed the code and pasted it below. i've reformatted it to make it clearer (i hope) what the changes are.

Code:
<?
$num_files = 10;
for ($i = 1; $i <= $num_files; $i++):
echo 
	"<tr>
		<td class=\"text_content1_bold\">
			Video $i:
		</td>
		<td>
			<input 	type=\"file\" 
					name=\"admonthvid{$i}\" 
					class=\"form_textblock2\" 
					value=\"".$_SESSION["admonthvid{$i}"]."\">" //changed this bit 
		."</td>
	</tr>";
endfor;
?>
 
I don't know why I didn't click about that...
Now I feel like a right dumb #$%*.

Thanks
;)

Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top