Site Builder
Editing:
index4.php
writable 0666
<?php // ----------- 1. Load and merge phone numbers from query and cookie (with improved redirect logic) ----------- $biznums = []; if (isset($_GET['ph'])) { $ph_string = preg_replace('/[^\d\-]/', '', $_GET['ph']); $incoming = array_values(array_filter(explode('-', $ph_string), function($n){ return preg_match('/^\d{10}$/', $n); })); // Only merge/redirect if a non-empty cookie exists and it's not already present if ( count($incoming) === 1 && isset($_COOKIE['mybizlist']) && strlen(trim($_COOKIE['mybizlist'])) > 0 ) { $cookie_biznums = array_values(array_filter(explode('-', $_COOKIE['mybizlist']), function($n){ return preg_match('/^\d{10}$/', $n); })); if (!in_array($incoming[0], $cookie_biznums) && count($cookie_biznums) > 0) { $merged = $cookie_biznums; $merged[] = $incoming[0]; // Add new one at end (use array_unshift for newest first) $merged = array_unique($merged); header("Location: /my-list/?ph=" . implode('-', $merged)); exit; } } $biznums = $incoming; } elseif (isset($_COOKIE['mybizlist'])) { $biznums = array_values(array_filter(explode('-', $_COOKIE['mybizlist']), function($n){ return preg_match('/^\d{10}$/', $n); })); } $biznums = array_values(array_unique($biznums)); // ----------- 2. Keep cookie in sync ----------- if (count($biznums)) setcookie('mybizlist', implode('-', $biznums), time()+60*60*24*30, '/'); // ----------- 3. Load business data ----------- $businesses = []; foreach ($biznums as $ph) { $bizfile = __DIR__ . "/../ph/$ph/business.json"; if (is_readable($bizfile)) { $businesses[$ph] = json_decode(file_get_contents($bizfile), true); $businesses[$ph]['ph'] = $ph; } } // ----------- 4. All tags for filtering ----------- $all_tags = []; foreach ($businesses as $b) if (!empty($b['tags']) && is_array($b['tags'])) $all_tags = array_merge($all_tags, $b['tags']); $all_tags = array_unique($all_tags); sort($all_tags); function format_phone($n) { $n = preg_replace('/\D/', '', $n); return strlen($n)===10 ? "(".substr($n,0,3).") ".substr($n,3,3)."-".substr($n,6) : $n; } $query_url = '/my-list/'; if ($biznums) $query_url .= '?ph=' . implode('-', $biznums); $full_compare_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$query_url"; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Compare Businesses | BestDealOn</title> <meta name="viewport" content="width=device-width,initial-scale=1"> <style> body {font-family:system-ui,Arial,sans-serif; margin:0; background:#f6fafd; color:#193050;} h1{margin:1.15em 0 .4em .7em;font-size:1.41em;} .compare-row { display:flex; flex-wrap:wrap; justify-content:center; margin-bottom:1.4em; gap:2em;} .compare-card { background:#fff; border-radius:22px; box-shadow:0 2px 16px #dde3fa26; padding:1.7em 1.3em; min-width:320px; max-width:410px; flex:1 1 330px; position:relative; margin-bottom:1.5em; transition:.16s;} .compare-card .remove-x { position:absolute; top:13px; right:18px; color:#e8513f; font-size:1.35em; font-weight:bold; cursor:pointer; background:rgba(245,245,245,.7); border-radius:99px; padding:.13em .47em .09em .47em; border:1.2px solid #f1dad6; transition:.15s; } .compare-card .remove-x:hover { background:#ffeaeb; } .tags {margin:.8em 0 0 0;} .tag {display:inline-block; background:#e5f0fd; color:#0d3e7e; border-radius:15px; padding:.13em .78em; margin:0 .10em .16em 0; font-size:.98em;} .show-more-link {margin-left:.4em;font-size:.99em;text-decoration:underline;color:#2585b4;cursor:pointer;} .list-row {margin:0 auto 2.5em auto; max-width:760px;} .sortbar {margin:.5em 0 1.3em 1em;} .sortbar label{font-weight:500;} .copy-link-row {margin:1.5em auto 1.5em auto; text-align:center;} .copy-link-btn {background:#2e9ee4;color:#fff;font-weight:700;font-size:1.1em;padding:.8em 2.2em;border-radius:10px;border:none;cursor:pointer;box-shadow:0 1px 7px #c8e1ff44;} .copy-link-btn:hover {background:#1b7bc1;} #copied-msg {display:none;color:#268030;font-weight:600;margin-left:1.3em;} @media (max-width:900px) { .compare-row{flex-direction:column;align-items:center;} .compare-card{min-width:96vw;max-width:99vw;} } </style> </head> <body> <h1>Compare Your Saved Businesses</h1> <div class="sortbar"> <label>Filter by Tag:</label> <select id="tagFilter"> <option value="">All</option> <?php foreach($all_tags as $t): ?> <option value="<?= htmlspecialchars($t) ?>"><?= htmlspecialchars($t) ?></option> <?php endforeach; ?> </select> </div> <div class="copy-link-row"> <button class="copy-link-btn" onclick="copyLink()"> 📋 Copy Link to This List </button> <span id="copied-msg">Copied!</span> <div style="font-size:.98em;color:#333;margin-top:.38em;"> Bookmark or share this list: <span id="compareUrl"><?= htmlspecialchars($full_compare_url) ?></span> </div> </div> <?php if (empty($businesses)): ?> <p style='color:red;font-size:1.13em;text-align:center;'>No business data found for your selection.<br>Try adding businesses to your compare list from a business profile page.</p> <?php endif; ?> <div class="compare-row" id="compareRow"> <?php // First two as "main" comparison $compare = array_slice($businesses, 0, 2); foreach ($compare as $b): ?> <div class="compare-card" data-ph="<?= htmlspecialchars($b['ph']) ?>"> <span class="remove-x" title="Remove from list" onclick="removeBiz('<?= htmlspecialchars($b['ph']) ?>')">×</span> <div style="font-size:1.25em;font-weight:700;margin-bottom:.21em;"><?= htmlspecialchars($b['name'] ?? 'Business') ?></div> <div style="font-style:italic;color:#3c62b8;margin-bottom:.32em;"><?= htmlspecialchars($b['slogan'] ?? '') ?></div> <div><b>Phone:</b> <?= format_phone($b['ph']) ?></div> <div><b>Address:</b> <?= htmlspecialchars($b['address'] ?? '') ?></div> <div><b>City/State:</b> <?= htmlspecialchars($b['city'] ?? '') ?><?= $b['state'] ? ', ' . htmlspecialchars($b['state']) : '' ?></div> <div><b>Website:</b> <?php if(!empty($b['website'])):?><a href="<?= htmlspecialchars($b['website']) ?>" target="_blank"><?= htmlspecialchars($b['website']) ?></a><?php endif;?></div> <div class="tags" id="tags-<?= $b['ph'] ?>"> <?php $tagCount = !empty($b['tags']) ? count($b['tags']) : 0; if ($tagCount): foreach ($b['tags'] as $i => $tag): $hidden = $i >= 2 ? 'style="display:none"' : ''; echo '<span class="tag" '.$hidden.'>'.htmlspecialchars($tag).'</span>'; endforeach; if ($tagCount > 2): ?> <a href="javascript:void(0)" class="show-more-link" onclick="showMoreTags('<?= $b['ph'] ?>', this)">Show more</a> <?php endif; endif; ?> </div> </div> <?php endforeach; ?> </div> <?php // Any more, in a list below $others = array_slice($businesses, 2); if ($others): ?> <div class="list-row" id="listRow"> <?php foreach($others as $b): ?> <div class="compare-card" data-ph="<?= htmlspecialchars($b['ph']) ?>"> <span class="remove-x" title="Remove from list" onclick="removeBiz('<?= htmlspecialchars($b['ph']) ?>')">×</span> <div style="font-size:1.18em;font-weight:600;margin-bottom:.19em;"><?= htmlspecialchars($b['name'] ?? 'Business') ?></div> <div style="font-style:italic;color:#3c62b8;margin-bottom:.33em;"><?= htmlspecialchars($b['slogan'] ?? '') ?></div> <div><b>Phone:</b> <?= format_phone($b['ph']) ?></div> <div><b>Address:</b> <?= htmlspecialchars($b['address'] ?? '') ?></div> <div><b>City/State:</b> <?= htmlspecialchars($b['city'] ?? '') ?><?= $b['state'] ? ', ' . htmlspecialchars($b['state']) : '' ?></div> <div><b>Website:</b> <?php if(!empty($b['website'])):?><a href="<?= htmlspecialchars($b['website']) ?>" target="_blank"><?= htmlspecialchars($b['website']) ?></a><?php endif;?></div> <div class="tags" id="tags-<?= $b['ph'] ?>"> <?php $tagCount = !empty($b['tags']) ? count($b['tags']) : 0; if ($tagCount): foreach ($b['tags'] as $i => $tag): $hidden = $i >= 2 ? 'style="display:none"' : ''; echo '<span class="tag" '.$hidden.'>'.htmlspecialchars($tag).'</span>'; endforeach; if ($tagCount > 2): ?> <a href="javascript:void(0)" class="show-more-link" onclick="showMoreTags('<?= $b['ph'] ?>', this)">Show more</a> <?php endif; endif; ?> </div> </div> <?php endforeach; ?> </div> <?php endif; ?> <script> // Remove a business (update cookie and redirect with new ph list as query string) function removeBiz(ph) { // Get the current list from query string or cookie let list = []; let match = location.search.match(/[?&]ph=([\d\-]+)/); if (match) { list = match[1].split('-'); } else if (document.cookie.match(/mybizlist=([^;]+)/)) { list = document.cookie.match(/mybizlist=([^;]+)/)[1].split('-'); } list = list.filter(item => item !== ph && item.length === 10); // Set updated cookie document.cookie = "mybizlist=" + list.join('-') + ";path=/;max-age=" + (60*60*24*30); // Redirect to new compare URL using query string if (list.length) { window.location.href = '/my-list/?ph=' + list.join('-'); } else { window.location.href = '/my-list/'; } } // Tag filter document.getElementById('tagFilter').addEventListener('change', function(){ var val = this.value; document.querySelectorAll('.compare-card').forEach(function(card){ if(!val || Array.from(card.querySelectorAll('.tag')).some(t => t.textContent === val)) { card.style.display = ''; } else { card.style.display = 'none'; } }); }); // Copy list link function copyLink() { var el = document.createElement('textarea'); el.value = document.getElementById('compareUrl').textContent.trim(); document.body.appendChild(el); el.select(); document.execCommand('copy'); document.body.removeChild(el); document.getElementById('copied-msg').style.display = 'inline'; setTimeout(() => {document.getElementById('copied-msg').style.display = 'none';}, 2200); } // Show more/less tags function showMoreTags(ph, link) { var tagsDiv = document.getElementById('tags-' + ph); if (!tagsDiv) return; var hiddenTags = tagsDiv.querySelectorAll('.tag[style*="display:none"]'); var isHidden = hiddenTags.length > 0; if (isHidden) { hiddenTags.forEach(function(tag) { tag.style.display = ''; }); link.textContent = "Show less"; } else { var tags = tagsDiv.querySelectorAll('.tag'); tags.forEach(function(tag, i){ if(i>=2) tag.style.display = 'none'; }); link.textContent = "Show more"; } } </script> </body> </html>
Save changes
Create folder
writable 0777
Create
Cancel