« Back to History
category_tags_add.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php // File: /public_html/shayari/category_tags_add.php require_once __DIR__ . '/config.php'; $pdo = db_connect(); $errors = []; $success = null; // Categories for dropdown $categories = []; try { $stmt = $pdo->query("SELECT id, name FROM shayari_categories ORDER BY sort_order ASC, name ASC"); $categories = $stmt->fetchAll(); } catch (Exception $e) { $errors[] = "Category load error: " . htmlspecialchars($e->getMessage()); } // Handle form POST $selected_category_id = isset($_POST['category_id']) ? (int)$_POST['category_id'] : 0; $tags_raw = $_POST['tags'] ?? ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($selected_category_id <= 0) { $errors[] = "Please select a category."; } $tags_raw = trim($tags_raw); if ($tags_raw === '') { $errors[] = "Tags enter karo (comma ya newline se)."; } if (empty($errors)) { // Split tags by comma or newline $parts = preg_split('/[\r\n,]+/', $tags_raw); $tags = array_filter(array_map('trim', $parts), function ($t) { return $t !== ''; }); if (!$tags) { $errors[] = "Valid tags nahi mile."; } else { try { $pdo->beginTransaction(); $tagSelect = $pdo->prepare("SELECT id FROM tags WHERE name = :name"); $tagInsert = $pdo->prepare("INSERT INTO tags (name, created_at) VALUES (:name, NOW())"); $mapInsert = $pdo->prepare(" INSERT IGNORE INTO category_tags (category_id, tag_id) VALUES (:category_id, :tag_id) "); $added = 0; foreach ($tags as $tagName) { // 1) tag exist? $tagSelect->execute([':name' => $tagName]); $row = $tagSelect->fetch(); if ($row) { $tagId = (int)$row['id']; } else { // naya tag banao $tagInsert->execute([':name' => $tagName]); $tagId = (int)$pdo->lastInsertId(); } // 2) category-tag mapping $mapInsert->execute([ ':category_id' => $selected_category_id, ':tag_id' => $tagId, ]); if ($mapInsert->rowCount() > 0) { $added++; } } $pdo->commit(); $success = "Tags saved! (New mappings added: {$added})"; // Tags text reset, category same rahe $tags_raw = ''; } catch (Exception $e) { $pdo->rollBack(); $errors[] = "Save error: " . htmlspecialchars($e->getMessage()); } } } } // existing tags for selected category (optional, nice UX) $existing_tags = []; if ($selected_category_id > 0) { $sql = " SELECT t.name FROM category_tags ct JOIN tags t ON t.id = ct.tag_id WHERE ct.category_id = :cid ORDER BY t.name ASC "; $st2 = $pdo->prepare($sql); $st2->execute([':cid' => $selected_category_id]); $existing_tags = $st2->fetchAll(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Category Tags Add</title> <style> body { font-family: system-ui, sans-serif; background:#f5f5f5; margin:0; padding:20px; } .wrap { max-width: 800px; margin:0 auto; background:#fff; padding:20px; border-radius:8px; box-shadow:0 0 8px rgba(0,0,0,0.06); } h1 { margin-top:0; } .field { margin-bottom:16px; } .field label { display:block; font-weight:600; margin-bottom:6px; } .field select, .field textarea { width:100%; padding:8px; } .msg-error { background:#ffe2e2; border:1px solid #f5aaaa; padding:10px; margin-bottom:12px; border-radius:6px; } .msg-success { background:#e2ffe7; border:1px solid #aaf5bc; padding:10px; margin-bottom:12px; border-radius:6px; } ul { margin:0; padding-left:18px; } button { padding:10px 18px; border:none; border-radius:4px; cursor:pointer; font-weight:600; background:#34A853; color:#fff; } .tag-list { margin-top:10px; padding:8px; border-radius:6px; background:#f9f9f9; border:1px solid #eee; } .tag-list span { display:inline-block; margin:4px; padding:4px 8px; border-radius:12px; background:#e3f2fd; font-size:13px; } </style> </head> <body> <div class="wrap"> <h1>Category ke liye Tags Add karo</h1> <?php if ($success): ?> <div class="msg-success"><?php echo htmlspecialchars($success); ?></div> <?php endif; ?> <?php if ($errors): ?> <div class="msg-error"> <strong>Errors:</strong> <ul> <?php foreach ($errors as $e): ?> <li><?php echo htmlspecialchars($e); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="post"> <div class="field"> <label for="category_id">Category select karo</label> <select name="category_id" id="category_id" onchange="this.form.submit()"> <option value="0">-- Select Category --</option> <?php foreach ($categories as $cat): ?> <option value="<?php echo (int)$cat['id']; ?>" <?php echo ($selected_category_id === (int)$cat['id']) ? 'selected' : ''; ?>> <?php echo htmlspecialchars($cat['name']); ?> </option> <?php endforeach; ?> </select> <small>Category change karte hi existing tags niche update ho jayenge.</small> </div> <div class="field"> <label for="tags">New Tags (comma ya newline se separated)</label> <textarea name="tags" id="tags" rows="4" placeholder="love, true love, dosti"><?php echo htmlspecialchars($tags_raw); ?></textarea> </div> <button type="submit">Save Tags for Category</button> </form> <?php if ($selected_category_id > 0): ?> <h2>Is Category ke Tags:</h2> <div class="tag-list"> <?php if ($existing_tags): ?> <?php foreach ($existing_tags as $t): ?> <span><?php echo htmlspecialchars($t['name']); ?></span> <?php endforeach; ?> <?php else: ?> <em>Abhi koi tag assign nahi hai.</em> <?php endif; ?> </div> <?php endif; ?> </div> </body> </html>