Solved

Help avoiding duplicates when adding to PHP Session

Posted on 2014-11-24
1
123 Views
Last Modified: 2014-11-24
When trying to add one or more arrays to my Session array, I run into a problem where the added array is duplicated on a page reload. I've tried a few different things to avoid this, but can not seem to find the solution.

Is it possible to somehow count how many times the page has been refreshed so that $discountedItems is only added once to the Session, regardless of how many times the page get's refreshed?

$discountedItems = array(
                    'name' => 'discount', 
                    'quantity' => '1', 
                    'price' => '-35.00',
                    );

array_push($_SESSION['items'],$discountedItems);

Open in new window

0
Comment
Question by:jej07
1 Comment
 
LVL 9

Accepted Solution

by:
teebon earned 500 total points
ID: 40463674
Try the following :

session_start();

if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}


$discountedItems = array(
                    'name' => 'discount', 
                    'quantity' => '1', 
                    'price' => '-35.00',
                    );

if($_SESSION['count'] == 0)
{
  array_push($_SESSION['items'],$discountedItems);
}

Open in new window

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Send email using HTML and PHP in separate file 5 41
uploading Theme in Wordpressn - geting error 4 28
Get data from two MySQL tables 6 30
PHP substring 3 18
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question