Re: Can I do this? If so why wont it work

From: Date: Fri, 16 Jan 2004 22:28:43 +0000
Subject: Re: Can I do this? If so why wont it work
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-174894@lists.php.net to get a copy of this message
Alex Hogan wrote:
I am wanting to read in several session values at once. This is what I have so far; $_SESSION['obj[1]'] = $_REQUEST['txtObj1']; $_SESSION['obj[2]'] = $_REQUEST['txtObj2']; $_SESSION['obj[3]'] = $_REQUEST['txtObj3']; $_SESSION['obj[4]'] = $_REQUEST['txtObj4']; $_SESSION['obj[5]'] = $_REQUEST['txtObj5']; $i = 1; while ($i <= 5) {
            $obj_ar[$i] = $_SESSION['obj[$i]']$i++;
} I keep getting this error; Parse error: parse error, unexpected T_VARIABLE in mypage.php on line 24
Well, first your error is because you have $i++ on that last line. It should be separated like someone else mentioned. Second, using "['obj[$i]']" as the key is not going to work because $i is not evaluated between single quotes. Third, you really need to change how you do all of this and understand arrays a little better. First thing to do is instead of naming things "txtObj1" and "txtObj2", etc, name them "txtObj[]". Name every one of them that. Now, when the form is submitted, you'll have a $_REQUEST['txtObj'] array containing all of your data. $_REQUEST['txtObj'][0] will be the first value, $_REQUEST['txtObj'][1] will be the second, etc. To save that in the session, you can simply do: $_SESSION['txtObj'] = $_REQUEST['txtObj']; Now, $_SESSION['txtObj'][0] is the first one, $_SESSION['txtObj'][1] is the second, etc. Any time you're naming things _1, _2, _3, etc, you're doing it the hard way instead of using an array. Hope this helps. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com

« previous php.general (#174894) next »