简体   繁体   English

删除数据库中的上一行,通过单击按钮显示数据库中的下一行

[英]Delete previous row in database show the next row in database by clicking button

I need Help on this, base on what I already did: In the output every time I click the button it shows the row (comments) in database. 基于已经完成的工作,我需要帮助:每次单击按钮时,输出中都会显示数据库中的行(注释)。 But I want that If I click the next button it will show the row (comments) in the database and when I click it again It will delete the previous row (comments) in the database and show the next row (comments). 但是我想要,如果我单击下一步按钮,它将显示数据库中的行(注释),当我再次单击它时,它将删除数据库中的上一行(注释)并显示下一行(注释)。

Here is the code: 这是代码:

<?php
    include 'dbh.php';

    $commentNewCount = $_POST['commentNewCount'];

    $sql = "SELECT * FROM comments LIMIT $commentNewCount";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo $row['id'];
        }
    } else {
        echo "There are number!";
    }
?>

This is for the button: 这是用于按钮的:

<script>
//jQuery code here!
$(document).ready(function() {
    var commentCount = 0;
    $("button").click(function() {
        commentCount = commentCount + 1;
        $("#comments").load("load-comments.php", {
            commentNewCount: commentCount
        });
    });
});
</script>

You have AJAX tagged in your question, so I am assuming that you are somewhat familiar with the term. 您在问题中标记了AJAX,所以我假设您对这个术语有点熟悉。 This can indeed be done by AJAX, but I don't see any AJAX attempts in your code? AJAX确实可以做到这一点,但是我在您的代码中看不到任何AJAX尝试吗?

Also, when you say delete , do you mean specifically delete by the literal sense, or simply that your comment display works as sort of a sliding function, removing the previous comment from display, showing the next comment in queue? 另外,当您说delete时 ,您的意思是按字面意思专门删除 ,还是只是将注释显示用作一种滑动功能,从而从显示中删除前一个注释,从而在队列中显示下一个注释? I am going by the literal sense in my example, since that's what I have to assume really. 在我的示例中,我将按字面意义进行操作,因为这是我必须真正假设的。

What you could do, is to have a file that handles all the comments, and displays them however you like. 您可以做的是拥有一个处理所有注释的文件,并根据需要显示它们。 For instance, 例如,

<?php
$sql="SELECT * FROM comments ORDER BY id DESC";
$result_set=mysqli_query($conn, $sql);
while ($row=mysqli_fetch_assoc($result)) {
   /*
   html construction of displaying comments,
   echo'ing the row value that displays the comments.
   echo $row['comments']; as a guess
   */
}
?>

Let's call that PHP file comments.php for the sake of referencing later on. 为了稍后引用,我们将其称为PHP文件comment.php Also note, that I chose to make an ORDER BY in a descending order. 另请注意,我选择按降序排列ORDER BY Assuming your comment id is an auto increment, then this will always display the newest comments first, since the highest id's will be the latest entries. 假设您的评论ID是自动递增的,则始终会先显示最新评论,因为ID最高将是最新条目。

Then, in another file, display-comments.php as an example, you could make a document.ready function, that loads your comments into an element, <div> element for example. 然后,在另一个文件display-comments.php作为示例,您可以创建一个document.ready函数,该函数将您的注释加载到元素中,例如<div>元素。

The AJAX function could look like this: AJAX函数如下所示:

$( document ).ready(function() {
    function loadCommentsAjax()
    {   
        $.ajax({
            type : "POST",
            url : "comments.php",
            data : { },
            success: function (html) {
              $("#commentContainer").html(html);                   
            }
        })
    }
});

What I do here, is that I encapsulate an AJAX function in a document.ready function. 我在这里所做的是将AJAX函数封装在document.ready函数中。 What this does, is that when the document is ready, it fires the AJAX function. 这样做是为了在文档准备就绪时触发AJAX函数。

How the AJAX function works, is that we declare a data type. AJAX函数的工作方式是声明一个数据类型。 The most common are probably POST and GET . 最常见的可能是POSTGET You can look up the different data types, how to handle them, and what they mean specifically. 您可以查找不同的数据类型,如何处理它们以及它们的具体含义。 The main difference between POST and GET for instance, is that GET displays its values as parameters in the URL. 例如, POSTGET之间的主要区别是GET其值显示为URL中的参数。 Since we aren't parsing any data, we could use a GET just fine, since it will have no influence. 由于我们不解析任何数据,因此可以使用GET ,因为它不会产生影响。 However, should you ever need to parse sensitive data, where you don't want the user to mingle with the data, then you should use POST . 但是,如果您需要解析敏感数据,而又不想让用户与数据混在一起,则应该使用POST

We then tell the AJAX function, which page/file it should work with. 然后,我们告诉AJAX函数应该使用哪个页面/文件。 In our example, it will be the comments.php that we created earlier. 在我们的例子,这将是我们创建较早的comments.php。 It will then in the success function, paste the html content into a container that we defined. 然后它将在成功函数中,将html内容粘贴到我们定义的容器中。 In this case, commentContainer . 在这种情况下, commentContainer Note that it's an id specific targeting, which means our container element needs to have that specific id . 请注意,这是一个特定于ID的定位,这意味着我们的容器元素需要具有该特定ID Note, that the container is in our main file, the display-comments.php file. 请注意,该容器位于我们的主文件display-comments.php文件中。

An example of the container could be the following: 容器的示例如下:

<div id="commentContainer"></div>

This div element will then contain the comments and html logic that we made in our comments.php file. 然后,该div元素将包含我们在comments.php文件中所做的注释和html逻辑。

In your button, you can then have another AJAX function, handling the comment deletion, and call our loadCommentsAjax() function on success, to reload our comments in the appropriate fashion. 然后,在您的按钮中,您可以拥有另一个AJAX函数,以处理注释删除,并在成功时调用我们的loadCommentsAjax()函数,以适当的方式重新加载我们的注释。

The AJAX function handling the deletion of comments, would then again have a PHP file that will perform the delete. 处理注释删除的AJAX函数将再次具有一个将执行删除操作的PHP文件。 We'll call this delete-comments.php in our example. 在示例中,我们将其称为delete-comments.php

example of our delete AJAX function: 删除AJAX函数的示例:

function deleteNewestComment() {
    $.ajax({
        type : "POST",
        url : "delete-comments.php",
        data : { },
        success: function (html) {
          loadCommentsAjax();                   
        }
    })
}

Our delete-comments.php will look something like this: 我们的delete-comments.php将如下所示:

<?php
/*
since you are deleting the latest entry each time, what we could do,
is make an SQL query that deletes the max id from the comments table
*/

$sql="DELETE FROM comments WHERE id=(SELECT max(id) FROM comments)";
$result=mysqli_query($conn, $sql);
?>

We will then have our button call the delete function like so: 然后,我们将让按钮调用delete函数,如下所示:

<button onclick="deleteNewestComment();">Delete latest comment</button>

Let me know if this is what you were looking for at all, or whether you really just wanted a sliding kind of logic, where you simply iterate through your comments, displaying one at a time. 让我知道这到底是您要查找的内容,还是真的只想使用一种滑动的逻辑,您可以在其中简单地遍历注释,一次显示一个。

But in case you did mean literally delete , then wouldn't it be better to have delete buttons linked to each comment, so that you can delete them seperately, and independantly? 但是,如果确实是按字面意义删除 ,那么将删除按钮链接到每个注释是否更好,以便您可以分别和独立地删除它们?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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