Dynamic Session Variables
Moderator: General Moderators
Dynamic Session Variables
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'];
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'];
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}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;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>"; };
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>"; };
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";- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
foreach($_SESSION as $name => $value)
echo ($_SESSION[$key] = $val) . "\n";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
its at: http://pspackaging.cdi.ie/polypropylene-tapes.php
if you want to have a look
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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>';
}