0

私の問題は、jQuery を使用して div を自動更新していますが、次のスクリプトを使用してレコードを追跡しているため、ユーザーが [さらに読み込む] をクリックすると投稿が読み込まれますが、jQuery が div を更新するとすぐにデフォルトにリセットされます、ロードされたレコードの数を保持するにはどうすればよいですか

これは私が試したものです

if(isset($_POST['load_more'])) {
    $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
    $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
} else {
    $counter = 10;
    $_SESSION['total_counter'] = $counter;
    $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
}

何が起こるかは、更新時に値を使用し、$counterさらにロードするセッション値を忘れることです

詳細コード

<?php
    require_once 'connection.php';
    require_once 'functions.php';
    if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false) {
        header('Location: login.php');
        exit;
    }
    date_default_timezone_set("Asia/Calcutta");
?>
<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery(".toggle_container").show();
                jQuery("div.question_trigger").click(function(){
                jQuery(this).toggleClass("active").next().toggle("slow");
                });
            });
        </script>
        <script type="text/javascript">
            var auto_refresh = setInterval(function() {
                  $('.messages').load('chat.php .messages', function(){
                       $(this).fadeIn("fast")
                  });
            }, 2000);
        </script>
    </head>
    <body>
        <?php
            require_once 'header.php';
            if($_SESSION['user_id'] == 1) {
                $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_first = 'yes' WHERE checked_first = ''");
            } elseif ($_SESSION['user_id'] == 2) {
                $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_second = 'yes' WHERE checked_second = ''");
            } elseif ($_SESSION['user_id'] == 3) {
                $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET  checked_third = 'yes' WHERE checked_third = ''");
            }   
        ?>
        <div class="container">
            <form method="POST">
                <textarea name="messaging"></textarea>
                <input type="submit" value="Post" name="message_input" class="gen_button" />
            </form>
            <img src="smileys/smile.gif" /><a target="_blank" href="smileys_info.php"> Smileys Info</a>
            <br /><br />
            <?php
            if(isset($_POST['message_input'])) {
                $throw_error = array();
                if(isset($_POST['messaging']) && $_POST['messaging'] == '') {
                    $throw_error['msg_blank'] = "Message Cannot Be Blank";
                }

                if(empty($throw_error)) {
                    $message = nl2br(htmlspecialchars(mysqli_real_escape_string($connection, $_POST['messaging'])));
                    $date_time = date("Y-m-d H:i:s");
                    if(!mysqli_query($connection, "INSERT INTO tbl_chat (msg_from, message, record_date) VALUES('".$_SESSION['user_id']."', '$message', '$date_time')")) {
                        echo mysqli_error($connection);
                    }

                    if($_SESSION['user_id'] == 1) {
                        if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_first) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                            echo mysqli_error($connection);
                        } 
                    } elseif ($_SESSION['user_id'] == 2) {
                        if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_second) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                            echo mysqli_error($connection);
                        }
                    } elseif ($_SESSION['user_id'] == 3) {
                        if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_third) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                            echo mysqli_error($connection);
                        }
                    }
                    else {
                        header('Location: chat.php');
                        exit;
                    }
                }

                if(!empty($throw_error)) {
                    if(isset($throw_error['msg_blank'])) {
                        echo "<span class=\"red\">".$throw_error['msg_blank']."</span>";
                    }
                }
            }

            if(isset($_POST['load_more'])) {
                $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
                $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);

            } else {
                $counter = 10;
                $_SESSION['total_counter'] = $counter;
                $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
            }
            ?>
            <div class="messages">
            <?php
                while($throw_messages = mysqli_fetch_array($fetch_messages)) {
            ?>
            <div class="message_container">
                <div class="display_pic">
                    <img src="<?php echo $throw_messages['user_pic']; ?>" />
                </div>
                <div class="user_name">
                    <?php
                        echo $throw_messages['real_name'];
                    ?>
                </div>
                <div class="message"><?php smilies($throw_messages['message']); ?></div>
                <div class="msg_date_time"><?php echo explode_date_time($throw_messages['record_date']); ?></div>
            </div>
            <?php } ?>
            </div>
            <?php
                if(mysqli_num_rows($fetch_messages) > 9) {
            ?>
            <form method="POST">
                <input type="submit" value="Load More" name="load_more" />
            </form>
            <?php
                }
            ?>
        </div>
    </body>
</html>
4

2 に答える 2

0

わかったので、ttを使用していたため、ページの読み込み時にカウンターをリセットしていましたが、load_moreフォームが送信された場合にのみ値を加算していましたが、jQueryによって更新されたため、カウンター値をリセットしていました。これは正しい状態です

if(!isset($_SESSION['total_counter'])) {
    $_SESSION['total_counter'] = 10;
}

if(isset($_POST['load_more'])) {
    $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
    $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
} else {
    $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
}
于 2013-01-02T21:18:49.770 に答える
0

session_start()スクリプトの先頭にあることを確認してください。

于 2013-01-02T21:10:38.013 に答える