2015-10-17 62 views
0

我試圖做一個帖子和評論部分,用戶可以發佈和comment.For爲此我有2個表:帖子,評論如何獲得後的電流id,同時評論給它

在我帖子表中有兩列:id和帖子;它是這樣的:

id | post 
---------------------- 

    1 | lo;l;l 
----------------------- 

    2 | i am feeling well 
在我的評語表

有三列:ID,評論和POST_ID(這也正是用戶評論的帖子的ID,這樣我可以動態地檢索評論每次發帖

我管理的帖子部分,但無法弄清楚如何在評論時獲取特定帖子的ID。 我不想要任何直接的解決方案,而是PDO的工作大綱如何做到這一點。在這方面的任何幫助將不勝感激

這是我迄今爲止管理的(它可能有點長,但它的理解非常簡單。我不提供我的數據庫查詢頁面,以保持帖子小。所有我需要知道如何獲得特定的post_id,而我正在評論它)

我還沒有寫在ajax調用內的insertComment.php頁面,但我想我的評論插入和檢索功能去那裏

<?php 
     session_start(); 
     require_once 'myDB.php'; 
     if(isset($_POST['post']) && !empty($_POST['post'])){ 
      $post=$_POST['post']; 
      $_SESSION['post']=$_POST['post']; 

      try{ 
       $newComment=DB::getInstance(); 
       $newComment->insert('post',array('posts'=>$post)); 
       }catch(Exception $e){ 
       echo $e->getMessage(); 
       } 
      header('HTTP/1.1 303 see other'); 
      header('Location:'.$_SERVER['PHP_SELF']); 
     }  
?> 
<html> 
<head> 
<style> 
    #formdiv{width:347px;height:120px;background:#dfe3ee;position:relative;border:1px dashed black;top:0px;left:300px;padding:5px; margin-bottom:3px; 

    } 
    #cmntbox{ 
     width:347px;background:#dfe3ee;position:relative;border:1px solid black;left:300px;padding:5px; 
    } 
    .repText{ 
     width:100%;background:#f7f7f7;position:relative;border:1px solid black;padding:3px;resize:none;} 
    } 
</style> 
</head> 
</body> 
<div id='formdiv'> 
    <form action='' method='POST'> 
     <textarea name='post' placeholder="what's on your mind !" cols='40' rows='3'></textarea> 
     <input type='submit' name='submit' value='post comment' style='float:right;width:100px;height:30px;background:#3b5998;color:white;'> 
    </form> 
</div> 
<?php 

     $newComment=DB::getInstance(); 
     $results=$newComment->getComment('SELECT','post','posts')->result(); 

     foreach($results as $result=>$val){ 
?> 
    <div id='cmntbox'><?php 
     echo $val->posts; 
     echo '</br><hr>';?> 
     <form> 
      <textarea name='myrep' id='myreply' class='repText'></textarea> 
      <input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' > 
     </form> 
    </div> 
<?php 
     } 
?> 
<script> 
    var reply=document.getElementsByClassName('reply'); 
    var repText=document.getElementsByClassName('repText'); 
    for(i=0;i<reply.length;i++){ 
     (function(i){ 
      reply[i].addEventListener('click',function(e){ 
        var xmlHttp=new XMLHttpRequest(); 
        xmlHttp.onreadystatechange=function(){ 
         if(xmlHttp.readyState==4 && xmlHttp.status==200){ 
          //do nothing 
         }else{ 
          alert('there was a problem '); 
         } 

        } 
        var parameters='myrep='+document.getElementById('myreq').value 
        xmlHttp.open("POST", "insertcomment.php", true); 
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
        xmlHttp.send(parameters); 
        } 
      }); 
     })(i); 

    } 
</script> 
</body> 
</html> 

enter image description here

+0

沿着這些線必須有一千個教程 – Strawberry

+0

任何人都可以給我一些暗示..我完全在黑暗中? –

+0

那麼,通常您在選擇帖子時選擇post_id,並將其保存在表單中的隱藏輸入內。 – Strawberry

回答

2

當你的頁面被加載的時候,你應該有這個帖子ID,所以當在html中繪製評論框時,你可以將帖子ID設置爲某個html元素的屬性或隱藏字段

因此,在你的情況更新後的腳本

<div id='cmntbox'><?php 
    echo $val->posts; 
    echo '</br><hr>';?> 
    <form> 
     <textarea name='myrep' id='myreply' class='repText'></textarea> 
     <input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' > 
    </form> 
</div> 

<div id='cmntbox'><?php 
    echo $val->posts; 
    echo '</br><hr>';?> 
    <form id="<?php echo $val->postId ?>"> 
     <textarea name='myrep' id='myreply' class='repText'></textarea> 
     <input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' > 
    </form> 
</div> 

OR

<div id='cmntbox'><?php 
    echo $val->posts; 
    echo '</br><hr>';?> 
    <form> 
     <input type="hidden" name="postId" value="<?php echo $val->postId ?>" /> 
     <textarea name='myrep' id='myreply' class='repText'></textarea> 
     <input type='button' class='reply' value='reply' style='width:50px;height:30px;background:#3b5998;color:white;' > 
    </form> 
</div>