0) $ids[] = $v; } $ids = array_unique($ids); sort($ids); if (empty($ids)) { header('Location: index.php?error=no_selection'); exit; } // Validate IDs exist in DB $placeholders = implode(',', array_fill(0, count($ids), '?')); $stmt = $pdo->prepare("SELECT activityID FROM activities WHERE activityID IN ($placeholders)"); $stmt->execute($ids); $validIds = $stmt->fetchAll(PDO::FETCH_COLUMN); if (empty($validIds)) { header('Location: index.php?error=invalid_selection'); exit; } sort($validIds); $idsStr = implode(',', $validIds); // Generate deterministic 5-character hash from the sorted activity IDs $hash = substr(hash('sha256', $idsStr), 0, 5); // Check if this exact selection already has a hash stored $stmt = $pdo->prepare("SELECT hash FROM selection_hashes WHERE activity_ids = ?"); $stmt->execute([$idsStr]); $existing = $stmt->fetchColumn(); if ($existing) { if ($existing !== $hash) { // Overwrite old (long) hash with the new short hash $stmt = $pdo->prepare("UPDATE selection_hashes SET hash = ? WHERE activity_ids = ?"); $stmt->execute([$hash, $idsStr]); } } else { // Store new hash $stmt = $pdo->prepare("INSERT INTO selection_hashes (hash, activity_ids) VALUES (?, ?)"); $stmt->execute([$hash, $idsStr]); } // Redirect to the shareable packing list URL header("Location: packing.php?h=" . urlencode($hash)); exit;