Jump to content

[SOLVED] Holding Array in Session Array


scottybwoy

Recommended Posts

Hi All, I'm trying to hold bits of info as they become accessible to the script, but when the script finishes it seems to overwrite, or lose it.  How would I make it keep the data sent before without overwriting it.  Here's what I have to start with :

<?php
function saleLoad()
{
	global $data_array, $product, $prodId, $template;

	if (!isset($_SESSION['SALE']))
	{
		$_SESSION['SALE'] = $data_array = array();
	}

	if (isset($_POST['custId']))
	{
		global $data_array;
		$custId = $_POST['custId'];
		$company = $this->selectRecord('company', 'customers', 'custId', $custId);
		$push_array = array('custId' => $custId, 'company' => $company);
		$this->array_push_associative($data_array, $push_array);
	}

	if (isset($_POST['clientId']))
	{
		global $data_array;
		$clientId = $_POST['clientId'];
		$name = $this->selectRecord('name', 'client', 'clientId', $clientId);
		$push_array = array('clientId' => $clientId, 'name' => $name);
		$this->array_push_associative($data_array, $push_array);
	}

	print_r($_SESSION['SALE']);
	include_once(MK_SALE_PAGE);

}
?>

 

As you will probably work out, selectRecord, just selects the record and array_push_associative, adds the second array to the first.

 

I'm trying to put an array into the session data, is that possible?  And how do I do it without it overwriting itself each time.  Thanks in advance

Link to comment
Share on other sites

i'm not sure, i'd need more information about your script, and probably more code to go off of to say for sure, but i believe your problem is here:

<?php
if (!isset($_SESSION['SALE']))
{
	$_SESSION['SALE'] = $data_array = array();
}
?>

 

try this instead:

<?php
if (empty($_SESSION['SALE']))
{
                $data_array = array();
	$_SESSION['SALE'] = $data_array;
}
?>

 

see what happens. also, post the output of $_SESSION['SALE'], before and after the revision.

Link to comment
Share on other sites

Hi, No that didn't do anything.

 

I was in a bit of a rush yesterday so didn't write it clearly.  What I am doing is passing data from the user via selection boxes.  Each time its sent this script is called but is sent different bits of data.  But I want it to hold the data that was sent b4.  Until it has done what it needs to do with it.

 

I have amended the script as above and below is my html :

<?php
if (isset($data_array['clientId']) && isset($data_array['company'])) {
?>
<form name="clientAdmin" action='home.php' method='POST'>
<input type='hidden' name='content' value='sale' />
<div class='l'>
<label for='company' class='labelL'>Company :</label>
<input type='text' id='company' value='<?php echo $data_array['company'] ?>' name='company' size='25%' class='required labelL' readonly />
</div>
<div class='r'>
<label for='clientId' class='labelL'>Client :</label>
<input type='text' id='clientId' value='<?php echo $data_array['clientId'] ?>' name='clientId' size='25%' class='required labelL' readonly />
</div>
<?php
} elseif (isset($data_array['company']) && isset($data_array['custId']) && $_REQUEST['select'] == 'Select Company') {
?>
<form name="clientSelect" action='home.php' method='POST'>
<input type='hidden' name='content' value='sale' />
<div class='l'>
<label for='company' class='labelL'>Company :</label>
<input type='text' id='company' value='<?php echo $data_array['company'] ?>' name='company' width='22%' class='required labelL' readonly />
</div>
<div class='r'>
<label for='clientId'>Client ID :</label>
<?php Template::selectWhereMenu('clientId', 'name', 'client', 'custId', $data_array['custId'], '1'); ?>
<input type='submit' name='select' id='select' value='Select Client' class='btn' onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" />
</div>
<?php
} else {
?>
<form name="custSelect" action='home.php' method='POST'>
<input type='hidden' name='content' value='sale' />
<div>
<label for='custId' class='labelL'>Company :</label>
<?php Template::selectSpecificMenu('custId', 'company', 'customers', '1'); ?>
<input type='submit' name='select' id='select' value='Select Company' class='btn' onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" />
</div>
<?php
}
?>

 

The rest is the rest of my form.  Any ideas, how I can do this?

Link to comment
Share on other sites

This is my array_push_assoc() if anyone want's it or think it may help unravelling my problem, thanks.

<?php
function array_push_associative(&$arr)
{
   		$args = func_get_args();
   		foreach ($args as $arg)
   		{
       		if (is_array($arg))
       		{
           		foreach ($arg as $key => $value)
           		{
               		$arr[$key] = $value;
               		//$ret++;
           		}
       		} else {
           		$arr[$arg] = "";
       		}
   		}

   		//return $ret;
}
?>

Link to comment
Share on other sites

thought $GLOBALS might hold the answer, but that pushed me to work out how I could keep all the info I wanted, script looks a bit like this now :

<?php
	global $data_array, $product, $prodId, $template;

	$data_array = $_POST;

	if (isset($data_array['custId']) && empty($data_array['company']))
	{
		$data_array['company'] = $this->selectRecord('company', 'customers', 'custId', $data_array['custId']);
	}

	if (isset($data_array['clientId']) && empty($data_array['name']))
	{
		$data_array['name'] = $this->selectRecord('name', 'client', 'clientId', $data_array['clientId']);
	}

	include_once(MK_SALE_PAGE);
?>

And my template still looks the same.  Thanks anyway ;)

Link to comment
Share on other sites

Back to this problem, sorry.  I will need to use $_SESSION array as I want to hold some other data too, and this isn't working for some reason.  This script is run three times;

 

1st - Without any data sent - the form is just called.

2nd - With 'custId' sent - this data is then returned to the form.

3rd -With 'company' and 'clientId' sent - then back to the rest of the form.

 

What I want is all of that data stored as it comes available.  Seems simple enough, until I try it.  So what I have created is a $_SESSION var for each piece of data to make it simple;

 

$_SESSION['scustId'] = $custId,

$_SESSION['scompany'] = $company,

$_SESSION['sclientId'] = $clientId,

$_SESSION['sclient'] = $client.

 

Then further down I produce a printout of the $_SESSION data:

<?php
foreach($_SESSION as $key => $value){
	echo "<strong>".$key." : </strong>".$value."<br>";
}
?>

This holds all my $_SESSION vars from other pages in my script, but when it comes to this particular function, it works in a peculiar way.

 

1st execution - Prints out all previous $_SESSION data.

2nd execution - Prints out previous and 'scustId' + 'scompany', so far so good.

3rd execution - Prints out previous and 'sclientId' + 'sclient, 'scustId' + 'scompany' are gone!

 

What could be unsetting this data?  Here's the complete code :

<?php
function saleLoad() {
global $data_array;

$data_array = $_POST;

if (isset($data_array['custId']) && empty($data_array['company'])) {
	$_SESSION['scustId'] = $data_array['custId'];
	$_SESSION['scompany'] = $this->selectRecord('company', 'customers', 'custId', $data_array['custId']);
}

if (isset($data_array['clientId']) && empty($data_array['name'])) {
	$_SESSION['sclientId'] = $data_array['clientId'];
	$_SESSION['sclient'] = $this->selectRecord('name', 'client', 'clientId', $data_array['clientId']);
}

foreach($_SESSION as $key => $value){
	echo "<strong>".$key." : </strong>".$value."<br>";
}

if (!empty($_SESSION['scustId']) && !empty($_SESSION['sclientId'])) {
	//$this->salesPrep($_SESSION['s*']);
}

include_once(MK_SALE_PAGE);

}
?>

 

Thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.