Home > DeveloperSection > Forums > Stop items with same id being add in a session
Pravesh Singh

Total Post:411

Points:2881
Posted on    October-30-2014 1:04 AM

 PHP Session  Sessionid 
Ratings:


 1 Reply(s)
 747  View(s)
Rate this:
A quick questions about sessions, I have a session with the following code

<?php
session_start();
$_SESSION['event_orders'][] = $_POST['event_id'];
?>
problem is once I add an item to the session, it can still re-add it again, therefore duplicating it.

How would one preform a check, something like "if ID exists in session, show (remove) instead of (add) button"?


ben reitman
ben reitman

Total Post:96

Points:676
Posted on    October-30-2014 1:04 AM

Use in_array function:

<?php
session_start();

if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
    $_SESSION['event_orders'][] = $_POST['event_id'];
}
?>

Don't want to miss updates? Please click the below button!

Follow MindStick