Question

The code below shows the user's recent posts from the database, and a delete button if the user viewing the post

<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>

For some reason, once the user hits the modal it shows the same post every time. For example, if the user hit delete on the first post and the post contents was hello, it would show hello in the modal. However, for all the rest of the posts in the loop if you hit Delete it will show the first post in every single modal.

Was it helpful?

Solution

Your href for your delete link is, #deletepost, and your modal ID is always the same.

Change it so it doesn't use ID's, or make each ID different.

Try this,

<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>

OTHER TIPS

Use different model ID 's in each time when you call model,

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top