2017-08-07 134 views
0

我存儲在一個字段名tagpost我複選框值和它們保存留做tag1tag2tag3tag4,現在我已經成功地獲取它們:存儲複選框值獲取和複選框選擇

 $tg = $row['tagpost ']; 
     $tg1 = explode(',', $tg); 
     $tg2 = implode(" ",$tg1); 

但我現在面臨的問題是如何保持選定的值?

這裏是我的PHP部分從形式:

enter image description here

<?php //include config 
require_once('../includes/config.php'); 

//if not logged in redirect to login page 
if(!$user->is_logged_in()){ header('Location: login.php'); } 
?> 
<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Admin - Edit Post</title> 
    <link rel="stylesheet" href="../style/normalize.css"> 
    <link rel="stylesheet" href="../style/main.css"> 
    <script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script> 
    <script> 
      tinymce.init({ 
       selector: "textarea", 
       plugins: [ 
        "advlist autolink lists link image charmap print preview anchor", 
        "searchreplace visualblocks code fullscreen", 
        "insertdatetime media table contextmenu paste" 
       ], 
       toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image" 
      }); 
    </script> 
    <script type="text/javascript"> 
    function delete_id_img(id) 
    { 
     if(confirm('Do you really want to delete this image?')) 
     { 
      window.location.href='edit-post.php?act=deleteimg&id='+id; 
     } 
    } 
    </script> 
</head> 
<body> 

<div id="wrapper"> 

    <?php include('menu.php');?> 
    <p><a href="./">Blog Admin Index</a></p> 

    <h2>Edit Post</h2> 


    <?php 

    //if form has been submitted process it 
    if(isset($_POST['submit'])){ 



     $_POST = array_map('stripslashes', $_POST); 

     //collect form data 
     extract($_POST); 

     //very basic validation 
     if($postID ==''){ 
      $error[] = 'This post is missing a valid id!.'; 
     } 

     if($postTitle ==''){ 
      $error[] = 'Please enter the title.'; 
     } 

     if($postCat ==''){ 
      $error[] = 'Please select category.'; 
     } 

     if($postDesc ==''){ 
      $error[] = 'Please enter the description.'; 
     } 

     if($postCont ==''){ 
      $error[] = 'Please enter the content.'; 
     } 

     if(!isset($error)){ 

     $st = "SELECT * FROM blog_posts WHERE postID = :postID"; 
     $st = $db->prepare($st); 
     $st->bindParam(':postID', $postID, PDO::PARAM_STR); 
     $st->execute(); 
     $rm = $st->fetch(); 
     $count = $st->rowCount(); 

     if($_FILES['file']['name']!='') 
     { 
       $file='../uploads/images/'.$rm['postImg']; 
       @unlink($file);    
       $tmp_name = $_FILES["file"]["tmp_name"]; 
       $namefile = $_FILES["file"]["name"]; 
       $ext = end(explode(".", $namefile)); 
       $fileUpload = move_uploaded_file($tmp_name,"../uploads/images/".$image_name); 
       $image_name=time().".".$ext;     
       watermark_image($tmp_name,"../uploads/images/".$image_name); 
       $img = ''.$image_name.''; 
      }else{ 
       $img = $rm['postImg']; 
      } 

      try { 

       $postSlug = slug($postTitle); 

       //insert into database 
       $stmt = $db->prepare('UPDATE blog_posts SET postTitle = :postTitle, postImg = :postImg, postYoutube = :postYoutube, postSlug = :postSlug, postDesc = :postDesc, postCont = :postCont, postCat = :postCat WHERE postID = :postID') ; 
       $stmt->execute(array(
        ':postTitle' => $postTitle, 
        ':postImg' => $img, 
        ':postYoutube' => $postYoutube, 
        ':postSlug' => $postSlug, 
        ':postDesc' => $postDesc, 
        ':postCont' => $postCont, 
        ':postCat' => $postCat, 
        ':postID' => $postID 
       )); 

        $seturl = "".$postID."/".$postSlug.""; 
        $qry2 = "UPDATE blog_posts SET postSlug = :postSlug WHERE postID = :postID"; 
        $statement = $db->prepare($qry2);    
        $statement->bindParam(':postSlug', $seturl);  
        $statement->bindParam(':postID', $postID);    
        $statement->execute(); 

       //redirect to index page 
       header('Location: edit-post.php?id='.$postID.''); 
       exit; 

      } catch(PDOException $e) { 
       echo $e->getMessage(); 
      } 

     } 

    } 

    ?> 



    <?php 
    //check for any errors 
    if(isset($error)){ 
     foreach($error as $error){ 
      echo $error.'<br />'; 
     } 
    } 

     try { 

      $stmt = $db->prepare('SELECT postID, postTitle, postImg, postYoutube, postDesc, postCont, postYoutube, postTag, postCat FROM blog_posts WHERE postID = :postID') ; 
      $stmt->execute(array(':postID' => $_GET['id'])); 
      $row = $stmt->fetch(); 

      $tg = $row['postTag']; 
      $tg1 = explode(',', $tg); 
      $tg2 = implode(',', $tg1); 


     } catch(PDOException $e) { 
      echo $e->getMessage(); 
     } 

    ?> 

    <?php 
$act = $_REQUEST['act']; 
    $id = $_REQUEST['id']; 
    if($act == "deleteimg") 
    { 
     //$qry = "SELECT * FROM article where id IN(".$id.")"; 
     //$result = mysql_query($qry); 
     $qry = "SELECT * FROM blog_posts WHERE postID IN(:postID)"; 
     $stmt = $db->prepare($qry); 
     $stmt->bindParam(':postID', $id); 
     $stmt->execute();  
     while($row = $stmt->fetch()) 
     { 
      $file='../uploads/images/'.$row['postImg']; 
      @unlink($file); 
     } 

     //$sqlAdd ="update article set ne_image = '' where id IN(".$id.")"; 
     //mysql_query($sqlAdd); 
     $qr = "UPDATE blog_posts SET postImg = '' WHERE postID IN(:postID)"; 
     $statement = $db->prepare($qr); 
     $statement->bindParam(':postID', $id); 
     $statement->execute(); 
     //header("Location:post-article.php?id=".$id.""); 
     header('Location: edit-post.php?id='.$id.'');  
    } 
     ?> 

     <?php 
    //show message 
    if(isset($_GET['action'])){ 
     echo '<h3>Post '.$_GET['action'].'.</h3>'; 
    } 
    ?> 

    <?php if(!empty($row['postImg'])){ echo '<img src="../uploads/images/'.$row['postImg'].'"><br/><a href="javascript:delete_id_img('.$row['postID'].')">Delete</a><br/>'; }else{ echo'<br/>'; } 
?> 

<?php if(!empty($row['postYoutube'])){ echo '<iframe width="720" height="450" src="http://www.youtube.com/embed/'.$row['postYoutube'].'"></iframe><br/><br/>'; }else{ echo'<br/>'; } 
?> 

    <form action='' method='post' enctype="multipart/form-data"> 
     <input type='hidden' name='postID' value='<?php echo $row['postID'];?>'> 

     <p><label>Title</label><br /> 
     <input type='text' name='postTitle' value='<?php echo $row['postTitle'];?>'></p> 

     <p><label>Category</label><br /> 
     <select name="postCat"> 
       <option value="" selected="selected" disabled="disabled">Select Category</option> 
        <?php     
        $stmt = $db->prepare('select * from blog_categories'); 
        $stmt->execute(); 
        $rc = $stmt->fetchAll(); 
        foreach($rc as $rows){ 
        ?> 
        <option value="<?php echo $rows['catUrl']; ?>" <?php if($row['postCat'] == ''.$rows['catUrl'].'') echo 'selected="selected"'; ?>><?php echo $rows['catName']; ?></option> 

        <?php 
        } 
        ?> 
       </select> 
       </p> 

     <?php if(!empty($row['postYoutube'])){ 
     ?> 

     <?php 
     }else{ 
     ?>  
     <p><label>Image</label><br /> 
     <input type="file" name="file" id="file"/> </p> 
     <?php 
     } 
     ?>  
     <?php if(!empty($row['postImg'])){ 
     ?> 

     <?php 
     }else{ 
     ?>   

     <p><label>Youtube Video ID</label><br /> 
     <input type="text" name="postYoutube" id="postYoutube" placeholder="Video ID e.g. lQem15Ow6hw" value="<?php echo $row['postYoutube'];?>" /> </p> 
     <?php 
     } 
     ?>   

     <p><label>Description</label><br /> 
     <textarea name='postDesc' cols='60' rows='10'><?php echo $row['postDesc'];?></textarea></p> 

     <p><label>Content</label><br /> 
     <textarea name='postCont' cols='60' rows='10'><?php echo $row['postCont'];?></textarea></p> 

     <br/> 
     <p><label>Tags <small></small></label><br /><br /> 
     <?php     
        $stmt = $db->prepare('select * from blog_tag'); 
        $stmt->execute(); 
        $rt = $stmt->fetchAll(); 
        foreach($rt as $rtg){ 
        ?> 
        (<input type="checkbox" name="postTag[]" value="<?php echo $tg2; ?>" <?php if(in_array($rtg['tagurl'],$tg1)){ echo 'checked="checked"'; } ?>> <?php echo $rtg['tagName']; ?>) , 

        <?php         
        } 
        ?> 
     <br/> 

     <p><input type='submit' name='submit' value='Update'></p> 

    </form> 

</div> 

</body> 
</html> 

上面的代碼獲取來自blog_tags所有標籤名稱,並嘗試將其與tagpost內的職位比較。

+3

有一個空間在' 'tagpost''?!?! – Strawberry

回答

0

簡單使用IN_ARRAY<?= (in_array($rtg['tagName'],$tg1))? 'checked="checked"' : "" ?>

$tg = $row['tagpost']; 
$tg1 = explode(',', $tg); 

.......... 

foreach($rt as $rtg){ 
?> 
<input type="checkbox" name="postTag[]" value="<?php echo $rtg['tagurl']; ?>" <?php if(in_array($rtg['tagurl'],$tg1)){ echo 'checked="checked"'; } ?> > <?php echo $rtg['tagName']; ?> 

<?php     
} 
+0

評論不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/151256/discussion-on-answer-by-jyothi-stored-checkbox-values-fetch-and-keep-checkbox-se) 。 –