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!

Simple array question

Status
Not open for further replies.

gokeeffe

Programmer
Jan 11, 2005
170
IE
Hi,

Is it possible to do the following for example could I change the array below from method one to method two.


Method One

$address_book = array
(
array(first_name => "John", last_name => "Davis", phone_number => "0876006529"),
array(first_name => "Graham", last_name => "Okeeffe", phone_number => "0875656565"),
array(first_name => "James", last_name => "Doyle", phone_number => "0876008962"),
array(first_name => "Mary", last_name => "O'Brien", phone_number => "0856006529"),
);


Method Two

$address_book = array
(
array(first_name => $_SESSION[first_name], last_name => $_SESSION[last_name], phone_number => $_SESSION[phone_number],),
);

What I want to do is pass variable from a web form to another page as SESSIONS
And then incorporate in the same array as I want to store different details before I submit to a database.
 
sure, be careful to save the final array itself to a session...

Known is handfull, Unknown is worldfull
 
You're close. It'd be more like:

$address_book = array
(
array([red]'[/red]first_name[red]'[/red] => $_SESSION[[red]'[/red]first_name[red]'[/red]], [red]'[/red]last_name[red]'[/red] => $_SESSION[[red]'[/red]last_name[red]'[/red]], [red]'[/red]phone_number[red]'[/red] => $_SESSION[[red]'[/red]phone_number[red]'[/red]])
);

However, assigning a variable a value from $_SESSION ($a = $_SESS['foo'];) doesn't propogate the value. Assigning $_SESSION a value from a variable ($_SESSION['foo'] = $a) does.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
If I use

$address_book = array
(
array('first_name' => $_SESSION['first_name'], 'last_name' => $_SESSION['last_name'], 'phone_number' => $_SESSION['phone_number'])
);

Will I be able to take the SESSION values out of the array and INSERT them into a MySQL table or is their a better
way to store this information


Regards
 
gokeefe:
You're not thinking that if you use some values from $_SESSION to populate an array, those values will be in that array across scripts, are you?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
TANSTAAFL

I was hoping to store these values in an array until I need to insert the values into my database,

I am totally incorrect to think this.

If I am how do I store these groups of values
until I populate the database with the values.

Is my concept incorrect

Regards
 
<aside>
"TANSTAAFL" is my signature, not my member handle.
</aside>


I don't know if your concept is incorrect, mainly because I don't understand what it is you're trying to do. For how long are you going to store the values in the array $address_book?

If you intent to keep $address_book alive, it needs to be inside $_SESSION. $_SESSION and only $_SESSION can survive between scripts.

Maybe it's time to post a little code. Try this script on for size:

Code:
<?php
session_start();
if (!isset($_SESSION['address_book']))
{
	$_SESSION['address_book'] = array();
}

if (isset ($_POST['name']))
{
	$_SESSION['address_book'][] = array ('name' => $_POST['name'], 'phone' => $_POST['phone']);
}

print '<html><body>
<table border="1"><tr><td>Name</td><td>Phone</td></tr>';

foreach ($_SESSION['address_book'] as $entry)
{
	print '<tr><td>' . $entry['name'] . '</td><td>'. $entry['phone'] . '</td></tr>';
}

print '</table><hr><form method="POST" action="' . $_SERVER['PHP_SELF'] . '">
<input type="text" name="name"><br><input type="text" name="phone"><br><input type="submit">
</form>';

print '</body></html>';
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214

What I am trying to do is the following.
I have a 3 step registration phase when you complete
Step 3 you are given the choose to proceed to check-out or
register another Interest (Max 5). So what I want to do is to be able to store multiple Interests in an array until payment has been verified and then submit the Interests which into my database. This is why I need an array in order to store all of this data before I submit to database.

I can understand your code and will try to incorporate it into my code. I will let you know how I get on.

Regards

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top