Site Builder
Editing:
category-uploader.php
writable 0666
<?php $jsonFile = __DIR__ . '/allcategories.json'; // Load existing (or initialize) $categories = []; if (file_exists($jsonFile)) { $categories = json_decode(file_get_contents($jsonFile), true) ?: []; } // Helper: On-the-fly slug function slugify($cat) { return strtolower(trim(preg_replace('/[^a-z0-9]+/i', '-', $cat), '-')); } // Handle form submit $status = ''; $addedList = []; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['categories'])) { $lines = explode("\n", $_POST['categories']); $added = 0; foreach ($lines as $line) { $cat = trim($line); if ($cat === '') continue; $cat = preg_replace('/\s+/', ' ', $cat); $letter = strtoupper($cat[0]); if (!isset($categories[$letter])) $categories[$letter] = []; // Only add if not already present (case-insensitive) $exists = false; foreach ($categories[$letter] as $item) { if (strcasecmp($item['name'], $cat) === 0) { $exists = true; break; } } if (!$exists) { $categories[$letter][] = [ 'name' => $cat ]; $added++; $addedList[] = $cat; } } // Sort and save ksort($categories); foreach ($categories as &$arr) { usort($arr, fn($a, $b) => strcasecmp($a['name'], $b['name'])); } file_put_contents($jsonFile, json_encode($categories, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); $status = "<div style='background:#e9ffd8;border:1.5px solid #96d356;color:#29890b;margin:1em 0;padding:.7em .9em;border-radius:7px;'><b>Success!</b> Added $added new categor" . ($added == 1 ? "y" : "ies") . "."; if ($addedList) $status .= "<div style='margin-top:.6em;font-size:.98em;'><b>Added:</b> " . implode(', ', array_map('htmlspecialchars', $addedList)) . "</div>"; $status .= "</div>"; } $total = 0; foreach($categories as $arr) $total += count($arr); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Upload Business Categories</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { font-family: system-ui, Arial, sans-serif; background:#f6f8fa; margin:0; padding:0; } .box { max-width:540px; margin:2em auto; background:#fff; border-radius:16px; box-shadow:0 2px 10px #d3daef88; padding:2.2em 1.7em; } h2 { color:#384bb6; margin-bottom:.4em; } textarea { width:100%; min-height:180px; border-radius:9px; border:1.5px solid #dbe4f0; font-size:1.09em; padding:.7em; background:#f9fafc; margin-bottom:.8em;} input[type=submit] { font-size:1.11em; font-weight:700; background:#5b36ff; color:#fff; border:none; border-radius:6px; padding:.6em 1.7em; cursor:pointer; } input[type=submit]:hover { background:#2630c0; } .catcount { font-size:1.02em; color:#1d5c33; margin-top:.7em; margin-bottom:.4em;} pre { background:#f8f9ff; border-radius:8px; padding:.7em .9em; font-size:.97em; margin-top:1.7em;} @media(max-width:600px){ .box{padding:1.2em .6em;} textarea{font-size:1em;} } </style> </head> <body> <div class="box"> <h2>Upload or Add Business Categories</h2> <p>Paste new business categories, one per line. <br>All are stored in <b>allcategories.json</b>, grouped by letter. Duplicates are skipped, case-insensitive. Slugs are created on the fly when needed.</p> <?= $status ?> <form method="post"> <textarea name="categories" placeholder="Example Accountants Acupuncture Bakeries"></textarea> <input type="submit" value="Add Categories"> </form> <div class="catcount"><b>Total:</b> <?= number_format($total) ?> categories.</div> <div style="margin-bottom:.8em;"> <?php foreach(range('A','Z') as $ch): $count = isset($categories[$ch]) ? count($categories[$ch]) : 0; if ($count > 0) echo "<b>$ch</b> ($count) "; else echo "$ch "; endforeach; ?> </div> <details><summary style="cursor:pointer;margin-top:.9em;">Show JSON</summary> <pre><?= htmlspecialchars(json_encode($categories, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)) ?></pre> </details> </div> </body> </html>
Save changes
Create folder
writable 0777
Create
Cancel