简体   繁体   中英

While loop on php doesn't work why?

enter image description here i am working on project CMS with php and one of my while loops doesn't work and i cant find why.

i am echo on (td) inside of loop and nothing showed on the page but when i echo outside of while loop it work very well. i have commentet the loop to see you. Can you give me some help where i have the problem?

<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Author</th>
            <th>Comment</th>
            <th>Email</th>
            <th>Status</th>
            <th>In Response to</th>
            <th>Date</th>
            <th>Approve</th>
            <th>Unapprove</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>

<?php     
        $query = "SELECT * FROM comments ";
        $select_comments = mysqli_query($connection, $query);

        while($row = mysqli_fetch_assoc($select_comments)) {
            $comment_id = $row['comment_id'];
            $comment_post_id = $row['comment_post_id'];
            $comment_author = $row['comment_author'];
            $comment_content = $row['comment_content'];
            $comment_email = $row['comment_email']; 
            $comment_status = $row['comment_status']; 
            $comment_date = $row['comment_date']; 


            echo "<tr>";

            echo "<td>$comment_id</td>";
            echo "<td>$comment_author</td>";
            echo "<td>$comment_content</td>";
/
            echo "<td>$comment_email</td>";
            echo "<td>$comment_status</td>";

            //THIS IS THE LOOP DOESN'T WORK
            $query = "SELECT * FROM posts WHERE post_id = $comment_post_id";

            $select_post_id_query = mysqli_query($connection, $query);

            while($row = mysqli_fetch_assoc($select_post_id_query)) {
                $post_id = $row['post_id'];
                $post_title = $row['post_title'];

            echo "<td><a href='../post.php?p_id=$post_id'>$post_title</a></td>";
            }


            echo "<td>$comment_date</td>";
            echo "<td><a href='comment.php?approve=$comment_id'>Approve</a></td>";
            echo "<td><a href='comment.php?unapprove=$comment_id'>Unapprove</a></td>";
            echo "<td><a href='comment.php?delete=$comment_id'>Delete</a></td>";

            echo "</tr>";

        }
?>

    </tbody>      
</table>






<?php

//approve posts
if(isset($_GET['approve'])){

$the_comment_id = $_GET['approve'];

    $query = "UPDATE comments SET comment_status = 'approved' WHERE comment_id = $the_comment_id ";
    $approve_comment_query = mysqli_query($connection, $query);
    header("Location: comment.php");
}


//unapprove posts
if(isset($_GET['unapprove'])){

$the_comment_id = $_GET['unapprove'];

    $query = "UPDATE comments SET comment_status = 'unapproved' WHERE comment_id =  $the_comment_id ";
    $unapprove_comment_query = mysqli_query($connection, $query);
    header("Location: comment.php");
}

//delete posts
if(isset($_GET['delete'])){

$the_comment_id = $_GET['delete'];

    $query = "DELETE FROM comments WHERE comment_id = {$the_comment_id} ";
    $delete_query = mysqli_query($connection, $query);
    header("Location: comment.php");
}



?>

You are overwriting variable $row in your second loop. You should change it eg. to $subRow to avoid such situation.

i have find the missing part on of my db with the connection.

THNX all you friends :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM