Jump to content

Adding onto the value of a $_SESSION


Logician

Recommended Posts

Is it possible to add onto the value of a $_SESSION, rather replacing it?

 

$_SESSION['order']['cartcontenttext'] += echo 'Small: "'.$content['sizes'].'"';
$_SESSION['order']['cartcontenttext'] += echo 'Medium: "'.$content['sizem'].'"';
$_SESSION['order']['cartcontenttext'] += echo 'Large: "'.$content['sizel'].'"';

 

... so the result would be something like this:

echo $_SESSION['order']['cartcontenttext'];

Small: 1Medium: 3Large: 2
Link to comment
Share on other sites

$_SESSION['order']['cartcontenttext'] = "Some additional text at the beginning " . $_SESSION['order']['cartcontenttext'];
$_SESSION['order']['cartcontenttext'] = $_SESSION['order']['cartcontenttext'] . " Some additional text at the end";

$_SESSION['order']['cartcontenttext'] = 'Small: ' . $content['sizes'] . ' Medium: ' . $content['sizem'] . ' Large: ' . $content['sizel'];

(You could add line breaks ("/n") or HTML break tags ("<br />"), if you didn't want it in a single line)

Link to comment
Share on other sites

Thorpe, I tried your suggestion, but it didn't work and I didn't get an error message in my log:

 

foreach ($_SESSION['cart']['content'] as $content) {
	$query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result);
	if ($content['sizes'] == 0) {goto sizem;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Small: '.$content['sizes'];}
	sizem:
	if ($content['sizem'] == 0) {goto sizel;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Medium: '.$content['sizem'];}
	sizel:
	if ($content['sizel'] == 0) {goto sizexl;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Large: '.$content['sizel'];}
	sizexl:
	if ($content['sizexl'] == 0) {goto endsizes;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Extra large: '.$content['sizexl'];}
	endsizes:
}

 

 

I'll try your suggestion, phpORcaffine.

Link to comment
Share on other sites

Ok, so I tried this. It looks like exactly what I need, but I suspect I have mucked up the braking out?

 

$_SESSION['order']['cartcontenttext'] = '.if ($content['sizes'] == 0) {goto sizem;} else {echo 'Small: '.$content['sizes']; sizem: if ($content['sizem'] == 0) {goto sizel;} else echo 'Medium: '.$content['sizem']; sizel: if ($content['sizel'] == 0) {goto sizexl;} else {echo 'Large: '.$content['sizel']; sizexl: if ($content['sizexl'] == 0) {goto endsizes;} else {echo 'Extra large: '.$content['sizexl']; endsizes:}

 

 

Thorpe, what is wrong with goto's?

Link to comment
Share on other sites

This is the code being used in whole:

 

foreach ($_SESSION['cart']['content'] as $content) {
	$query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result);
	$_SESSION['order']['cartcontenttext'] = if ($content['sizes'] == 0) {goto sizem;} else {echo 'Small: '.$content['sizes'];} sizem: if ($content['sizem'] == 0) {goto sizel;} else {echo 'Medium: '.$content['sizem'];} sizel: if ($content['sizel'] == 0) {goto sizexl;} else {echo 'Large: '.$content['sizel'];} sizexl: if ($content['sizexl'] == 0) {goto endsizes;} else {echo 'Extra large: '.$content['sizexl'];} endsizes:;
	$query = "INSERT INTO orders (id, cartcontent) VALUES (null, '".$_SESSION['order']['cartcontenttext']."'";
	mysql_query ($query);
}

Link to comment
Share on other sites

Yes. Fair enough.

 

I am now going to take 2nd best. I've mind hammered myself so much I am temporarily uncapable of detecting minor erros. Could you please help me with this, its not working can I cannot see why right now:

 

$_SESSION['order']['cartcontenttext'] = 'Small: '.$content['sizes'].'Medium: '.$content.'Large: '.$content['sizel'].'Extra large: '.$content['sizexl']';

Link to comment
Share on other sites

Why do you insist on adding extra quotes that are simply not needed?

 

$_SESSION['order']['cartcontenttext'] = 'Small: '.$content['sizes'].' Medium: '.$content['sizem'].' Large: '.$content['sizel'].' Extra large: '.$content['sizexl'];

 

What do you think the quotes on the end of your last post are actually for? They do nothing.

Link to comment
Share on other sites

Defined by PHP refusing to parse the page.

 

The quote on the end is the closing quote is it not?

 

Closing quote for what?

 

Here is some basic string handling 101:

 

"Any text in these quotes will echo as typed, however the double quotes have the ability to also echo the defined values of variables (i.e $a would echo the value of $a in double quotes)"

 

'Any text in single quotes will echo as typed, and WILL not echo intrinsic value of typed variables, rather the literal value of the variable (i.e $a will echo as $a)'

 

You only need a closing quote and apostrophe when there is a corresponding, opening quote or apostrophe. The PHP interpreter does not require any line terminating quotes or apostrophes. Generally, PHP lines are terminated with a semi-colon ";", except in some cases, where a statement may not complete  on one line (i.e function/method/class declarations ... etc).

 

You should really start by learning the what the difference is between strong and loose typed languages, how PHP is typed (loose as a goose) and then study up on PHP operators and basic control structures.

 

The best way to help some one is to teach them how to find the answer themselves, not do it for them - that doesn't help at all.

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.