Page 1 of 1

Dynamic Session Variables

Posted: Thu Jun 10, 2004 10:12 am
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'];

Posted: Thu Jun 10, 2004 10:15 am
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..

Posted: Thu Jun 10, 2004 10:16 am
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;

Posted: Thu Jun 10, 2004 10:17 am
by denhamd2
that sounds perfect. does this do exactly what i had in mind above or will i need to customise the variable names?

Posted: Thu Jun 10, 2004 10:20 am
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 ;)

Posted: Thu Jun 10, 2004 10:24 am
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>"; };

Posted: Thu Jun 10, 2004 10:32 am
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";

Posted: Thu Jun 10, 2004 10:38 am
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";

Posted: Thu Jun 10, 2004 10:40 am
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.. ;)

Posted: Thu Jun 10, 2004 10:54 am
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

Posted: Thu Jun 10, 2004 11:00 am
by denhamd2
seems to be working better now, is there any possible way i can exclude empty variables from the results?

Posted: Thu Jun 10, 2004 11:11 am
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???

Posted: Thu Jun 10, 2004 11:19 am
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>'; 
}

Posted: Thu Jun 10, 2004 11:28 am
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?

Posted: Thu Jun 10, 2004 11:41 am
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?