2017-01-19 117 views
-1

我想創建一個時間線來更新狀態以及它的評論,但我卡在while for循環評論。它的所有循環的狀態是這樣的:父母和孩子while循環php

$_session['name1'] update: 
lol 
test comment //comment 
test2 comment //comment 

$_session['name2'] update: 
lol2 
test comment //comment 
test2 comment //comment 

我需要循環在剛剛父狀態子註釋,如何將這項代碼看看?還是使用類或函數?

輸出應該是這樣的:

$_session['name1'] update: 
lol 
test comment //comment 
test2 comment //comment 

$_session['name2'] update: 
lol2 
another comment //comment 

到目前爲止我的代碼:

<?php 
session_start(); 
require "koneksi.php"; 

echo 'selamat datang, '.$_SESSION['nama']." <a href='logout.php'>logout</a><br>"; 
?> 

<form action='' method='POST'> 
<h5>Update Status</h5> 
<textarea name='textarea' cols='30' rows='3' placeholder="what's on your mind?"> </textarea><br> 
<input type='submit'> 
</form> 

<?php 
if(isset($_POST['textarea'])){ 
    $text = $_POST['textarea']; 
    $pemilik = $_SESSION['nama']; 
    $sql1 = "INSERT INTO `home` VALUES(0,'$text','$pemilik')"; 
    mysqli_query($conn,$sql1); 
} 

if(isset($_POST['comment'])){ 
    $comment = $_POST['comment']; 
    $pemiliks = $_SESSION['nama']; 
    $sqlcom = "INSERT INTO `comment` VALUES(0,'$comment','$pemiliks')"; 
    mysqli_query($conn,$sqlcom); 
} 

if(isset($_GET['del_stat'])){ 
    $del = $_GET['del_stat']; 
    $delsSql = "DELETE FROM `home` WHERE `id`='$del'"; 
    mysqli_query($conn,$delsSql); 
} 

if($query2 = mysqli_query($conn,$sql2 = "SELECT * FROM `home`")){ 
    while($row = mysqli_fetch_assoc($query2)){ 
     $stat = $row['id']; 
     echo '<table>'; 
     echo '<tr>'; 
     echo '<td>'; 
     echo $row['pemilik'].' <font color=blue>post</font> '; 
     if($_SESSION['nama']==$row['pemilik']){echo "<a href=home.php?del_stat=$stat>delete</a>";} 
     echo '</td>'; 
     echo '</tr>'; 
     echo '<td>'; 
     echo $row['timelines']; 
     if($results = mysqli_query($conn,$sql = "SELECT * FROM `comment`")){ 
      while($theRows = mysqli_fetch_array($results)){ 
       $roww = $theRows['comment']; 
       echo '<table>'; 
       echo '<tr>'; 
       echo '<td>'; 
       echo $roww; 
       echo '</td>'; 
       echo '</tr>'; 
       echo '</table>'; 
      } 
     } 
     echo '</td>'; 
     echo '</tr>'; 
     echo '<tr>'; 
     echo '<td>'; 
    ?> 
    <form action='' method='POST'> 
    <textarea name='comment' rows='3' cols='20' placeholder='comment...'></textarea><br> 
    <input type='submit'> 
    </form> 
    <?php 
     echo '</td>'; 
     echo '</tr>'; 
     echo '</table>'; 
    } 
} 

my database home for loop the updated status :

my database comment for loop the updated comment

我不能張貼2個鏈接的圖像爲輸出我不預期,所以我想輸出si milar這樣的:

Faddi Susanto post delete 
hahahahahaha //comment belongs to Faddi Susanto 
hello //comment belongs to Faddi Susanto 
hello again //comment belongs to Faddi Susanto 
s //comment belongs to Faddi Susanto 

adi post delete 
hohohohohohoho //comment belongs to adi 
tes adi //comment belongs to adi 
tes adi //comment belongs to adi 

它像更新狀態

回答

0

說實話一個網站,我不是你正在嘗試做清楚。我不知道你的數據庫表是如何設置的,所以很難建議任何真正有價值的東西。一般來說,這裏有一些我注意到的事情:

在每個循環中運行一個查詢是一個非常昂貴的方法來處理這個問題。也許有些情況下,這是不可避免的,但理想情況下,您需要先運行查詢,然後啓動循環。由於你有兩個不同的連接數據表,你可能最終需要JOIN。

此外,您的表標記是不正確的。您不應在循環中啓動<table>標記,除非您還在同一循環內關閉它(</table>),因爲它將爲每個循環創建一個新的<table>而不關閉前一個循環。

+0

嗨,我編輯了我的帖子,包括數據庫。我只是問像父母和孩子一樣的循環,想知道它是否更清晰? – faddi

+0

這並沒有多大幫助。我沒有看到任何外鍵連接兩個表之間的任何數據。表格和列名不適用於任何特別的東西。我無法幫助到目前爲止提供的內容。 – Ben