USIN FORMS Passing into an array then to a session variable

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
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

USIN FORMS Passing into an array then to a session variable

Post by parkin »

okie.. :) i'm doin the sessions.. But somehow I get the same thing.. It only displays my latest form entry.. the array didn't save the entry I did before.

I want to get the name from a form.. Check if it exists in the database.. If it does.. push it into an array and display.. Then when i get back to the form... I do everything all over again.. push it into the array.. And when it displays.. i'd like both the first name and the second name entered to be pushed and displayed from the array..

Currently.. It only displays the latest one..

Can any one correct me? I can't seem to find out what's wrong..

Code: Select all

<?php
// retrieve the array from the session n display..
session_start();
$arr_unserial = unserialize($_session[searcharr]);
foreach($arr_unserial as $sess)
{
		print "session arr:$sess";
	}


$prods = getProd("product"); // retrieve the products from the database
$prod=array();

global $search_arr;
$search_arr=array(); // to store the prod_names that need to be searched

// Gets product names and pushes into array
foreach($prods as $prod_row)
{
	if($prod_row[prod_family]!="special")
		{
			$prod_row[prod_name]=makealias($prod_row[prod_name]);
			array_push($prod,$prod_row);
		}
}

print "<html><head>
		<title></title>
		</head><body>
		<blockquote>";
	

// Text field to search the prod name
print "<table border=0>";
print "<tr><td>";
print "<form action= $PHP_SELF name=selectprod>";
html_hidden("actionflag","selection");
print "<b>Search: </b>";
//text box
html_text("form[prod_name]");
print " ";
html_submit("","Ok");
print "</form>";
print "</td></tr>";
print "</table> <br>";

print "<hr>";


// Textfield search Action
if (isset($actionflag) && $actionflag =="selection") // check action flag
{
	if($form[prod_name]!=null) // check if textfield not null
	{
		print "text search entered: $form[prod_name]<br><br>";
				
		// Check if the product name entered is correct
		foreach($prods as $prod_row) 
		{
			if($form[prod_name] == $prod_row[prod_name]) // Product name entered matches	
			{
				$matched = $form[prod_name];
				print "$prod_row[prod_name] [matches] $matched<br>";
				break;		
			}
			else // Product name entered does not match 
			{	
				$matched = "not exist";
			}
			
		}

	}
	else
	{
		print "";
	}
}


// Push into array
if($matched!=null && $matched!="not exist")
{
	array_push($search_arr,$matched); 
	foreach($search_arr as $searched)
	{
		print "search_arr: $searched<br>";
	}
}

// sessions
if($PHPSESSID && $matched!=null && $matched!="not exist")
{	
	$arr_serial = serialize($search_arr);
	print "$sessarr<br>";
	print "Session started<br>";	
	
	session_start(); // Start the session
	$_session[searcharr] = $arr_serial;
	print "$_session[searcharr]<BR>";
}


print "</blockquote>
		</body>	</html>";

?>
Thanks.. alot! :wink:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The superglobal is called $_SESSION.
although function names are not case sensitive variable names are.

There's no need to serialize the array. You can make each member of $_SESSION an array if you like to, e.g.

Code: Select all

<?php
ini_set('display_errors', TRUE);
session_start();
if (isset($_POST['userinput']))
	@$_SESSION['userinput'][] = $_POST['userinput']; // too lazy for testing right now, suppressing warning
?>
<html>
	<head>
		<title>Session Test</title>
	</head>
	<body>
		<fieldset>
			<legend>current values in $_SESSION['userinput']</legend>
				<pre><?php @print_r($_SESSION['userinput']); ?></pre>
		</fieldset>
		<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			<input type="text" name="userinput" />
			<input type="submit" value="add" />
		</form>
	</body>
</html>
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Post by parkin »

sorry.. I don't quite understand..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the basic structure of your script is

Code: Select all

<?php
session_start();
$arr_unserial = unserialize($_session[searcharr]); 

/**
modifying values
making $search_arr the new array to store
*/

$arr_serial = serialize($search_arr); 
$_session[searcharr] = $arr_serial; 
?>
first, to solve your current problem, you have to change the name of the variable.

Code: Select all

<?php
session_start();
$arr_unserial = unserialize($_SESSION[searcharr]); 

/**
modifying values
making $search_arr the new array to store
*/

$arr_serial = serialize($search_arr); 
$_SESSION[searcharr] = $arr_serial; 
?>
after that you should read http://www.php.net/manual/en/language.types.array.php#language.types.array.donts
leading to

Code: Select all

<?php
session_start();
$arr_unserial = unserialize($_SESSION['searcharr']); 

/**
modifying values
making $search_arr the new array to store
*/

$arr_serial = serialize($search_arr); 
$_SESSION['searcharr'] = $arr_serial; 
?>
For the serialize thing - don't bother too much about it...

serialize($search_arr) creates a string representation of the array $search_arr
To see what I mean try

Code: Select all

<?php
$search_arr = array('first', 'second', 'third');
$serialzed = serialize($search_arr);
?>
<html>
	<body>
		<fieldset>
			<legend>type of $search_arr</legend>
			<?php echo gettype($search_arr); ?>
		</fieldset>
			
		<fieldset>
			<legend>echoing $search_arr</legend>
			<?php echo $search_arr; ?>
		</fieldset>
			
		<fieldset>
			<legend>print_r of $search_arr</legend>
			<?php print_r($search_arr); ?>
		</fieldset>

		<hr />

		<fieldset>
			<legend>type of $serialzed</legend>
			<?php echo gettype($serialzed); ?>
		</fieldset>
			
		<fieldset>
			<legend>echoing $serialzed</legend>
			<?php echo $serialzed; ?>
		</fieldset>
			
		<fieldset>
			<legend>print_r of $serialzed</legend>
			<?php print_r($serialzed); ?>
		</fieldset>
	</body>
</html>
a short tour on arrays:

Code: Select all

<?php $arr = array(1=>'first', 2=>'second', 3=>'third'); ?>
<html>
	<body>
		<pre><?php print_r($arr); ?></pre>
		type of the second element is: <?php echo gettype($arr[2]); ?>
	</body>
</html>
$arr is an array having three elements. Each of them is a string.
But you can also assign arrays as elements.

Code: Select all

<?php 
$elem = array(1=>'a', 2=>'b', 3=>'c');
$arr = array(1=>'first', 2=>$elem, 3=>'third'); ?>
<html>
	<body>
		<pre><?php print_r($arr); ?></pre>
		type of the second element is: <?php echo gettype($arr[2]); ?>
	</body>
</html>
now you have a multidimensional array. The second element is an array as well.

Code: Select all

<?php 
$elem = array(1=>'a', 2=>'b', 3=>'c');
$arr = array(1=>'first', 2=>$elem, 3=>'third'); ?>
<html>
	<body>
		<pre><?php print_r($arr); ?></pre>
		type of the second element of $arr[2] is: <?php echo gettype($arr[2][2]); ?>
	</body>
</html>
http://www.php.net/manual/en/language.types.array.php will tell you more about it.


$_SESSION can take arrays as elements as well.

Code: Select all

<?php
session_start();
if (isset($_SESSION['an_array']))
	array_push($_SESSION['an_array'], rand(0, 15));
else
	$_SESSION['an_array'] = array();
?>
<html>
	<body>
		currently <?php echo count($_SESSION['an_array']); ?> entries in the session-array
		<br />
<?php
		$sum = 0;
		foreach($_SESSION['an_array'] as $entry)
		{
			echo $entry, '<br />';
			$sum += (int)$entry;
		}
?>		
		<hr />
		<?php echo $sum; ?>
		<br />
		<a href="<?php echo $_SERVER['PHP_SELF']; ?>">reload the page</a>
	</body>
</html>
as you can see I treat $_SESSION as a normal array. In fact it is.
The only special thing about this array is, it's filled with values when session_start() is invoked and is serialized and stored when the session ends (ofetn at the end of the script)
parkin
Forum Commoner
Posts: 25
Joined: Wed Jul 23, 2003 3:18 am

Post by parkin »

Thanks a whole lot for the explaination volka! :lol:
I understand it now.. :) And it manges to work.. :wink:
Thanks! :mrgreen:
Post Reply