父母和孩子,而循环PHP

时间:2017-01-19 00:07:27

标签: php forms while-loop

我正在尝试创建一个时间轴来更新状态以及它的评论,但我陷入while for循环评论。它循环所有状态如下:

$_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个链接图片作为输出我没想到,所以我希望输出类似这样:

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

它就像Facebook更新状态

1 个答案:

答案 0 :(得分:0)

说实话,我不清楚你想做什么。我不知道你的数据库表是如何设置的,因此很难提出真正有价值的东西。一般来说,这里有一些我注意到的事情:

在每个循环中运行查询是一种非常昂贵的方法来处理它。也许有些情况下这是不可避免的,但理想情况下,您可能希望先运行查询然后启动循环。由于您有两个不同的表连接数据,因此最终可能需要加入。

此外,您的表格标签不正确。您不应该在循环中启动<table>标记,除非您也在同一循环中关闭它(</table>),因为它会为每个循环创建一个新的<table>而不关闭上一个。