« Back to History
post_add_edit.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================= FILE: post_add_edit.php PURPOSE: - Create + Edit Shayari Posts - Each shayari saved as <p> tag - Supports bulk paste, keyboard navigation, drag reorder FEATURES: - Add + Edit mode (?id=POST_ID) - Auto slug from title - Shayari boxes auto split on paste - Enter → next box | Ctrl+Enter → new box - Ctrl+B → bold - Drag & drop reorder - Per-box style (heart / gradient / dark) - Google-sheet style box add - Output remains backward compatible DB: - Uses existing `posts` table - body_html contains <p data-style="...">shayari</p> ============================================================================= */ error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/config.php'; $pdo = db_connect(); function h($s){ return htmlspecialchars((string)$s,ENT_QUOTES,'UTF-8'); } $msg = ''; $post = null; $shayari_boxes = []; /* ---------- EDIT MODE ---------- */ $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if ($id) { $st = $pdo->prepare("SELECT * FROM posts WHERE id=?"); $st->execute([$id]); $post = $st->fetch(PDO::FETCH_ASSOC); if ($post) { preg_match_all('#<p(?: data-style="([^"]*)")?>(.*?)</p>#s', $post['body_html'], $m, PREG_SET_ORDER); foreach ($m as $p) { $shayari_boxes[] = [ 'text' => html_entity_decode(strip_tags($p[2])), 'style' => $p[1] ?? '' ]; } } } /* ---------- SAVE ---------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = trim($_POST['title'] ?? ''); $slug = trim($_POST['slug'] ?? ''); $shayari = $_POST['shayari'] ?? []; $styles = $_POST['style'] ?? []; if ($title==='' || $slug==='' || empty($shayari)) { $msg = 'Title, slug aur shayari required'; } else { $body=[]; foreach ($shayari as $i=>$s) { $t = trim($s); if ($t==='') continue; $stl = $styles[$i] ?? ''; $attr = $stl ? ' data-style="'.h($stl).'"' : ''; $body[] = "<p$attr>".h($t)."</p>"; } if ($id) { $pdo->prepare(" UPDATE posts SET title=?,slug=?,body_html=? WHERE id=? ")->execute([$title,$slug,implode("\n",$body),$id]); $msg='✅ Post updated'; } else { $pdo->prepare(" INSERT INTO posts(title,slug,body_html,status) VALUES(?,?,?,'published') ")->execute([$title,$slug,implode("\n",$body)]); $msg='✅ Post created'; } } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Post Editor</title> <style> .wrap{max-width:1100px;margin:20px auto;font-family:system-ui} .box{border:1px solid #ccc;padding:20px;border-radius:6px} .shayari-box{border:1px dashed #bbb;padding:8px;margin-bottom:8px;cursor:move} textarea{width:100%;min-height:60px} .tools{display:flex;gap:6px;margin-bottom:4px} .small{width:120px} </style> </head> <body> <div class="wrap"> <div class="box"> <h2>Post Add / Edit</h2> <?php if($msg): ?><p><?=h($msg)?></p><?php endif; ?> <form method="post"> <label>Title</label> <input name="title" id="title" value="<?=h($post['title']??'')?>"> <label>Slug</label> <input name="slug" id="slug" value="<?=h($post['slug']??'')?>"> <hr> <div id="area"> <?php if(!$shayari_boxes) $shayari_boxes[]=['text'=>'','style'=>'']; foreach($shayari_boxes as $i=>$b): ?> <div class="shayari-box" draggable="true"> <div class="tools"> <select name="style[]"> <option value="">Default</option> <option value="heart" <?=($b['style']=='heart'?'selected':'')?>>Heart</option> <option value="gradient" <?=($b['style']=='gradient'?'selected':'')?>>Gradient</option> <option value="dark" <?=($b['style']=='dark'?'selected':'')?>>Dark</option> </select> </div> <textarea name="shayari[]"><?=h($b['text'])?></textarea> </div> <?php endforeach; ?> </div> <input type="number" id="cnt" class="small"> <button type="button" onclick="add()">Add</button> <br><br> <button>Save</button> </form> </div> </div> <script> /* slug */ title.oninput=()=>{ if(!slug.dataset.touched) slug.value=title.value.toLowerCase().replace(/[^a-z0-9]+/g,'-'); } slug.oninput=()=>slug.dataset.touched=1; /* add box */ function add(n=cnt.value||1){ for(let i=0;i<n;i++){ const d=document.createElement('div'); d.className='shayari-box'; d.draggable=true; d.innerHTML=`<div class="tools"> <select name="style[]"> <option value="">Default</option> <option value="heart">Heart</option> <option value="gradient">Gradient</option> <option value="dark">Dark</option> </select></div> <textarea name="shayari[]"></textarea>`; area.appendChild(d); } } /* keyboard */ area.addEventListener('keydown',e=>{ if(e.ctrlKey && e.key==='Enter'){ add(); e.preventDefault(); } if(e.ctrlKey && e.key==='b'){ document.execCommand('bold'); } }); /* drag reorder */ let drag; area.addEventListener('dragstart',e=>drag=e.target); area.addEventListener('dragover',e=>{ e.preventDefault(); const t=e.target.closest('.shayari-box'); if(t && t!==drag) area.insertBefore(drag,t); }); </script> </body> </html>