PHP while循环在Bootstrap模式中不起作用

时间:2013-04-26 22:29:31

标签: php mysql bootstrap-modal

下面的代码显示了用户最近发布的数据库,以及用户查看帖子时的删除按钮

<div class="span12">
                    <?php
                        // get current user ID
                        $userid = $row['0'];

                        // get posts
                        $sql_posts = "SELECT * FROM posts WHERE ownerid='$userid' ORDER BY id DESC LIMIT 0,5";
                        $result_posts = mysql_query($sql_posts);

                        // for each post, show le post.
                        while($row_posts = mysql_fetch_assoc($result_posts)) {
                    ?>
                    <div class="well">
                        <span class="label"><?php echo date('F j Y',strtotime($row_posts['time']));?></span>
                        at
                        <span class="label"><?php echo date('g:i a',strtotime($row_posts['time']));?></span>
                        <?php
                            if($player==$_SESSION['username']) {
                        ?>
                            <a href="#deletepost" data-toggle="modal">
                                <span class="label label-important">Delete post</span>
                            </a>
                            <!-- delete post modal -->
                            <div id="deletepost" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-header">
                                    <button type="button" class="close delete" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h3 id="myModalLabel">Delete post</h3>
                                </div>
                                <div class="modal-body">
                                    Are you want to delete post #<?php echo $row_posts['id'];?>?
                                    <p class="muted">
                                        "<i><?php echo $row_posts['contents'];?></i>"
                                    </p>
                                </div>
                                <div class="modal-footer">
                                    <form class="pull-right form-inline" method="post" action="post_delete.php">
                                        <input type="hidden" value="<?php echo $row_posts['id'];?>" name="postid">
                                        <input type="hidden" value="<?php $filepath = $_SERVER["SCRIPT_NAME"]; echo basename($filepath);?>" name="currentpage">
                                        <button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Keep the post</button>
                                        <button type="submit" class="btn btn-danger">I am sure. Delete the post!</button>
                                    </form>
                                </div>
                            </div>
                            <!-- end modal -->
                        <?php
                            } // end delete post button
                        ?>
                        <hr width="250px">
                        <img src="profilepic.php?player=<?php echo $player;?>&size=32" />
                        <?php echo $row_posts['contents'];?>
                    </div>
                    <?php
                        } // end post foreach
                    ?>
                </div>

出于某种原因,一旦用户点击模态,它每次都会显示相同的帖子。例如,如果用户在第一篇帖子上点击删除,帖子内容为hello,则会在模态中显示hello。但是,对于循环中的所有其他帖子,如果您点击“删除”,它将显示每个模态中的第一个帖子。

2 个答案:

答案 0 :(得分:0)

您的删除链接的href为#deletepost,您的模态ID始终相同。

更改它以使其不使用ID,或使每个ID不同。

试试这个,

<div class="span12">
                    <?php
                        // get current user ID
                        $userid = $row['0'];

                        // get posts
                        $sql_posts = "SELECT * FROM posts WHERE ownerid='$userid' ORDER BY id DESC LIMIT 0,5";
                        $result_posts = mysql_query($sql_posts);

                        // for each post, show le post.
                        while($row_posts = mysql_fetch_assoc($result_posts)) {
                    ?>
                    <div class="well">
                        <span class="label"><?php echo date('F j Y',strtotime($row_posts['time']));?></span>
                        at
                        <span class="label"><?php echo date('g:i a',strtotime($row_posts['time']));?></span>
                        <?php
                            if($player==$_SESSION['username']) {
                        ?>
                            <a href="#deletepost-<?php echo $row_posts['id'];?>" data-toggle="modal">
                                <span class="label label-important">Delete post</span>
                            </a>
                            <!-- delete post modal -->
                            <div id="deletepost-<?php echo $row_posts['id'];?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-header">
                                    <button type="button" class="close delete" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h3 id="myModalLabel">Delete post</h3>
                                </div>
                                <div class="modal-body">
                                    Are you want to delete post #<?php echo $row_posts['id'];?>?
                                    <p class="muted">
                                        "<i><?php echo $row_posts['contents'];?></i>"
                                    </p>
                                </div>
                                <div class="modal-footer">
                                    <form class="pull-right form-inline" method="post" action="post_delete.php">
                                        <input type="hidden" value="<?php echo $row_posts['id'];?>" name="postid">
                                        <input type="hidden" value="<?php $filepath = $_SERVER["SCRIPT_NAME"]; echo basename($filepath);?>" name="currentpage">
                                        <button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Keep the post</button>
                                        <button type="submit" class="btn btn-danger">I am sure. Delete the post!</button>
                                    </form>
                                </div>
                            </div>
                            <!-- end modal -->
                        <?php
                            } // end delete post button
                        ?>
                        <hr width="250px">
                        <img src="profilepic.php?player=<?php echo $player;?>&size=32" />
                        <?php echo $row_posts['contents'];?>
                    </div>
                    <?php
                        } // end post foreach
                    ?>
                </div>

答案 1 :(得分:0)

每次调用模型时都使用不同的型号ID

<div class="modal fade" id="<?php echo $id; ?>" role="dialog">
                    <div class="modal-dialog modal-sm">
                        <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal">&times;</button>
                          <h4 class="modal-title">Delete Product</h4>
                        </div>
                        <div class="modal-body">
                          <p>Are you sure, want to delete this?</p>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <a href="delete.php?pro=<?php echo $id; ?>"><span  class="btn btn-danger" >Delete</span></a>
                        </div>
                        </div>
                    </div>
                </div>