Dynamic Session Variables

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
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Dynamic Session Variables

Post by denhamd2 »

Hi again,

thanks for the advice for the t_variable prob.

just another quickie:

I'm trying to create dynamic session variables which have unform names up to order150_q. instead of declaring each one individually like below is there a way of doing this through a loop?

$_SESSION["order7_q"] = $_POST['order7_q'];
$_SESSION["order8_q"] = $_POST['order8_q'];
$_SESSION["order9_q"] = $_POST['order9_q'];
$_SESSION["order10_q"] = $_POST['order10_q'];
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
note: this blindly does it.. you may want to add an ablility to filter out things you don't want..
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

foreach($_POST as $name => $value)
  if(
     preg_match("#order(\d+)_q#", $name, $matches)  &&
     $matches[1]>0 && $matches[1]<150
   ) $_SESSION[$name] = $value;
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

that sounds perfect. does this do exactly what i had in mind above or will i need to customise the variable names?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

It's the answer to the question you've posted. Whether that question was what you had in mind or not is a completely different story ;)
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

yep that works fine, thanks.

lastly something along the same lines:

im trying to say if the session variable exists echo it in a new paragraph, else don't do anything. instead of having to type this out 150 times do you know of how I could set up a loop do this automatically? it would save my life big time...

if ($_SESSION["order1"] == "") {} else { echo "<p>Details: " . $_SESSION["order1"] . "| Quantity:" . $_SESSION["order1_q"] . "</p>"; };
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

foreach($_SESSION as $name => $value)
  if(
     preg_match("#order(\d+)_q#", $name, $matches)  &&
     $matches[1]>0 && $matches[1]<150
   ) echo "Details: ". $_SESSION["order" . $matches[1]] . " | Quantity:" . $value . "\n";
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

hi weirdan

thanks for the help, however i used feyd's code above to declare the variables. just wondering having done that, would the code to use be?:

foreach($_SESSION as $name => $value)
echo $_SESSION[$key] = $val . "\n";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

foreach($_SESSION as $name => $value) 
echo ($_SESSION[$key] = $val) . "\n";
slightly more like that.. you don't want to add \n to the end of the session var now.. ;)
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

for some reason it seems to spit out submit instead of the entered values, any ideas?

its at: http://pspackaging.cdi.ie/polypropylene-tapes.php
if you want to have a look
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

seems to be working better now, is there any possible way i can exclude empty variables from the results?
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

im praying feyd will know the answer for this 1...


is there a way to put "Item:" before each item variable and "Quantity:" before each quantity variable

... something to do with an odd/even number loop???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if(!empty($_SESSION)) 
{ 
echo '<p>Detail:<br />'; 
foreach($_SESSION as $key => $val) 
{ 
  if(!empty($val))
  {
  if(preg_match('|_q$|',$val)) echo 'Quantity: '.$val.'<br />';
  elseif(preg_match('|_i$|',$val)) echo 'Item: '.$val.'<br />';
  }
} 
echo '</p>'; 
}
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

thanks again. my god im getting heart failure, everything seemed to work until you go back and order again at which point the previously ordered quantities and and items seem to get a little muddled,,, is there any possible way to group the item with its quantity?
denhamd2
Forum Newbie
Posts: 12
Joined: Thu Jun 10, 2004 9:59 am

Post by denhamd2 »

sorry, the actual problem is that when the user buys something else and views their cart again, the items they have already selected are already there but no quantities? the ones they have just bought do show the quantities however. any ideas?
Post Reply