Page 1 of 1

Passing vars to next page of result display

Posted: Sun Jan 04, 2004 11:03 am
by eatrom
Project: user selects various criteria for a search, and results display limited to first 20 of howevermany. There is a "Next" and "Previous" link to step through the remaining results. I am trying to maintain the search criteria, so that the results maintain consistancy.

What I was trying was to place the $_POST vars into a temp array and saving that in $_SESSION. IT seems to work for the most part, but losses the "$key" name, and just uses a numeric key instead. I'm still new at associative arrays and thought someone could guide me through this.

Sample code follows:

Code: Select all

if($_POST['doCommand']) {
		// form was submitted
		$varArray=array();							// set up temp array
		foreach($_POST as $key=>$value) {			// read in each posted var
			$tempArray = array("$key"=>"$value");
			$varArray = array_merge ($varArray, $tempArray);
		}
		$_SESSION['hold_vars']=$varArray;			// save to session
		while(list($key,$value) = each ($varArray)) {
			echo("$key:$value<br>");				// this displays correctly
		
		}
	} else {
		// form was not submitted
		$varArray=$_SESSION['hold_vars'];			// recover temp vars
		while(list($key,$value) = each ($varArray)) {
			echo("$key:$value<br>");				// this displays number keys
		
		}
	}

Code: Select all

===
1st time displays:
citySearch:Livermore
min_price:275
max_price:325
beds:3
baths:2
doCommand:submit
===
2nd time displays:
0:Livermore
1:275
2:325
3:
4:
5:
How do I keep the key name while saving into session??

:?:

[Edit: Added PHP & CODE tags for eyecandy. --JAM]

Posted: Sun Jan 04, 2004 11:16 am
by Nay
How about doing this istead?

Code: Select all

foreach($_POST $key => $value) {
   $_SESSION['temp'][$key] = $value;
}

// and on the other page:

$array = $_SESSION['temp'];
echo $array['stuff'];

// or just plain
echo $_SESSION['temp']['stuff'];
hope it helps,

-Nay

[Edit: Nice signature ;) --JAM]

Posted: Sun Jan 04, 2004 11:20 am
by JAM
Also check up on the [php_man]serialize[/php_man]() function, as that might help you in using arrays with sessions.

Posted: Sun Jan 04, 2004 12:59 pm
by eatrom
JAM wrote:Also check up on the [php_man]serialize[/php_man]() function, as that might help you in using arrays with sessions.
Interesting, but the serialize alternates the key name and the value. I've changed the code to:

Code: Select all

if($_POST&#1111;'doCommand']) &#123;
		// form was submitted
		$varArray=array();
		foreach($_POST as $key=&gt;$value) &#123;
			$tempArray = array("$key"=&gt;"$value");
			$varArray = array_merge ($varArray, $tempArray);
		&#125;
		
		
		$data = addslashes(serialize($varArray));
die($data);
	&#125; else &#123;
which results in:

Code: Select all

a:17:&#123;s:16:"simpleSearchFlag";s:10:"cityStreet";s:10:"citySearch";s:9:"Livermore";s:9:"zipSearch";s:0:"";s:10:"mls_search";s:0:"";s:12:"streetSearch";s:0:"";s:10:"areaSearch";s:8:"ex.20106";s:8:"Submit_x";s:2:"45";s:8:"Submit_y";s:2:"16";s:6:"cities";s:5:"Array";s:9:"min_price";s:3:"275";s:9:"max_price";s:3:"325";s:4:"beds";s:1:"3";s:5:"baths";s:1:"2";s:4:"acre";s:1:" ";s:4:"SqFt";s:0:"";s:9:"doCommand";s:6:"submit";s:10:"resultCode";s:3:"res";&#125;
so then unserialize would still need to pair the key/val back together.

:cry:

Posted: Sun Jan 04, 2004 2:23 pm
by JAM
Helpful?

Code: Select all

<?php
    session_start();
    echo '<pre>';
    unset ($_SESSION);
    print_r($_POST);
    foreach($_POST as $key => $value) {
        $_SESSION[$key] = $value;
    }
    // print_r($_SESSION);
    $data = $_SESSION;
    $data = base64_encode(serialize($data));
    echo $data;
    $_SESSION['mydata'] = $data;
    $newdata = $_SESSION['mydata'];
    echo '<br>';
    echo $newdata;
    echo '<br>';
    $newdata = unserialize(base64_decode($newdata));
    print_r($newdata);
?>

<form method="post">
<input type="text" name="one" value="ONE" />
<input type="text" name="two" value="TWO" />
<input type="text" name="three" value="THREE" />
<input type="text" name="four" value="FOUR" />
<input type="submit" />
</form>
Result:

Code: Select all

Array
(
    &#1111;one] =&gt; ONE
    &#1111;two] =&gt; TWO
    &#1111;three] =&gt; THREE
    &#1111;four] =&gt; FOUR
)
YTo0OntzOjM6Im9uZSI7czozOiJPTkUiO3M6MzoidHdvIjtzOjM6IlRXTyI7czo1OiJ0aHJlZSI7czo1OiJUSFJFRSI7czo0OiJmb3VyIjtzOjQ6IkZPVVIiO30=
YTo0OntzOjM6Im9uZSI7czozOiJPTkUiO3M6MzoidHdvIjtzOjM6IlRXTyI7czo1OiJ0aHJlZSI7czo1OiJUSFJFRSI7czo0OiJmb3VyIjtzOjQ6IkZPVVIiO30=
Array
(
    &#1111;one] =&gt; ONE
    &#1111;two] =&gt; TWO
    &#1111;three] =&gt; THREE
    &#1111;four] =&gt; FOUR
)
Sorry, got carried away while testing some other mumbojumbo...