STOP ITEMS WITH SAME ID BEING ADD IN A SESSION

Pravesh Singh

Total Post:412

Points:2888
Posted by  Pravesh Singh
PHP
 1192  View(s)
Ratings:
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"?
  1. ben reitman

    Post:96

    Points:676
    Re: Stop items with same id being add in a session

    Use in_array function:

    <?php
    session_start();

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!