Page 1 of 1

store and call associative array as session. [solved]

Posted: Tue Sep 18, 2007 4:15 pm
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.

Posted: Tue Sep 18, 2007 4:31 pm
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];
}

Posted: Tue Sep 18, 2007 4:43 pm
by feyd
Just an aside, there's no need to add quotes around the variables most often.

Posted: Tue Sep 18, 2007 4:50 pm
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?

Posted: Tue Sep 18, 2007 4:54 pm
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.

Posted: Tue Sep 18, 2007 5:26 pm
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....

Posted: Tue Sep 18, 2007 6:45 pm
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.

Posted: Tue Sep 18, 2007 6:58 pm
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.