store and call associative array as session. [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

store and call associative array as session. [solved]

Post by lafflin »

Hello,

I am entering a set of variables into a session as an array

Code: Select all

$_SESSION['stud_info']= array("$fn", "$ln", "$dob", "$sex", "$school", "$medical", "$notes") ;
I am able to enter these variables with or without quotes. Using quotes I would expect for it to give me an associative array,
but when I call on the array by key ($fn) I get NULL.
If however I call on the array by index all is good.
I wouldn't expect for the above code to actually give me an associative array, but it does give me an indexed which I can call on by number.
But I would much rather be able to call on it by Key, how can I do this?

Thank for anyone willing to school me up on this.
Last edited by lafflin on Tue Sep 18, 2007 6:46 pm, edited 1 time in total.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

http://us.php.net/key

Fetches the key from an associative array. You are not assigning keys, so the key() function will return null.

Code: Select all

$_SESSION['stud_info'] = array(
    "fn" => "$fn",
    "ln" => "$ln",
    "dob" => "$dob",
    "sex" => "$sex",
    "school" => "$school",
    "medical" => "$medical",
    "notes" => "$notes"
);
while($key = key($_SESSION['stud_info']))
{
    echo $key . " - " . $_SESSION['stud_info'][$key];
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just an aside, there's no need to add quotes around the variables most often.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

Thanks, I appreciate that.
I have another question, I've been learning PHP for about six weeks now, but it seems that I always seem to get confused when I try to enter
variables into my queries. And each time I get it working I feel like I finally understand, but then I end up having trouble again.
This time it's entering session values into my query.

Code: Select all

$query = "INSERT INTO
		                      student_info 
							       ( sid, 
								     first_name,
									 last_name,
									  sex,
									  reg_date,
									  dob, 
									  school, 
									  active, 
									  medical_issues, 
									  notes, 
									  secret_classification, 
									  last_update    ) 
				 VALUES 
				                   ( 'null', 
								     '."$_SESSION['stud_info'][0]".', 
									 '."$_SESSION['stud_info'][1]".', 
									 '."$_SESSION['stud_info'][2]".', 
									  now(), 
									 '."$_SESSION['stud_info'][3]".', 
									 '."$_SESSION['stud_info'][4]".', 
									 'n', 
									 '."$_SESSION['stud_info'][5]".', 
									 '."$_SESSION['stud_info'][6]".', 
									 '0', 
									 'now()'         ) "  ;	
			
		$result = @mysql_query ($query); // Run the query.
I have tried both curly braces and brackets, but it just isn't working, I am getting the unexpected variable error message.
I feel like I shou;d have a handle on this already, but there's obviously something I'm missing. Do I need to assign the session value to a regular variable before inserting into my query? what am I doing wrong?
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

I think I got it, it was the concat dots on the wrong sides of my double quotes. seems weird to have them on that side, but I shall do as the php gods want.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Code: Select all

$query = "INSERT INTO student_info
  (sid,  first_name,  last_name,  sex,  reg_date,  dob,  school,
   active,  medical_issues,  notes,  secret_classification,  last_update)
VALUES
   ( NULL,
     '{$_SESSION['stud_info'][0]}',
     '{$_SESSION['stud_info'][1]}',
     '{$_SESSION['stud_info'][2]}',
     NOW(),
     '{$_SESSION['stud_info'][3]}',
     '{$_SESSION['stud_info'][4]}',
     'n',
     '{$_SESSION['stud_info'][5]}',
     '{$_SESSION['stud_info'][6]}',
     '0',
     NOW())";
or

Code: Select all

$query = <<< EOSQL
INSERT INTO student_info
  (sid,  first_name,  last_name,  sex,  reg_date,  dob,  school,  
   active,  medical_issues,  notes,  secret_classification,  last_update)
VALUES
   ( NULL,
     '{$_SESSION['stud_info'][0]}',
     '{$_SESSION['stud_info'][1]}',
     '{$_SESSION['stud_info'][2]}',
     NOW(),
     '{$_SESSION['stud_info'][3]}',
     '{$_SESSION['stud_info'][4]}',
     'n',
     '{$_SESSION['stud_info'][5]}',
     '{$_SESSION['stud_info'][6]}',
     '0',
     NOW())
EOSQL;
should also work. Link for explanation is Strings. I have changed the string 'null' to the SQL null as I am unsure if that is what you really want.

Something else to bear in mind is do you really want to use a numeric array for the session values or could you change it to be an associative array such as $_SESSION['stud_into']['first_name'] ? When using mysql_fetch_assoc you could then easily build the array automatically whilst maintaining readability. You would know what each index is without trying to figure out what number goes with what. Also if you ever add columns or god forbid change the column order in a table....
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

No i didn't want to use an indexed array, I just didn't know how to make it an associate, I'll be going back to change that.

Thank you all for your help.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

lafflin wrote:I think I got it, it was the concat dots on the wrong sides of my double quotes. seems weird to have them on that side, but I shall do as the php gods want.
What about it seems weird?

Code: Select all

$example = "example";

$str = 'This is an' . $example . 'of concatenation.';
Why would it make sense to be inside of the quotes? *That's* the string.
Post Reply