jquery只返回php while循环的最后结果

发布于 2024-11-27 19:13:05 字数 3556 浏览 0 评论 0原文

以下代码仅获取 PHP while 循环中最后一个结果的值。非常感谢任何/所有帮助。谢谢!

PHP/HTML/JS:

        <section id="info">
            <?php
                $user = $session->username;
                $q = sprintf("SELECT * FROM mail WHERE UserTo = '%s' ORDER BY SentDate DESC",
                      mysql_real_escape_string($user));
                $getMail = mysql_query($q, $link) or die(mysql_error());

                if(mysql_num_rows($getMail) == 0) {
                    echo "<p>you have no mail</p>";
                }
                else {
                ?>
            <form id="inbox" class="mail">
                <fieldset>
                    <ul>
                        <li style="border: 2px solid purple; width: 100%;">
                            <span style="width: 8%; margin-left: 13%;">Status</span>
                            <span style="width: 15%;">From</span>
                            <span style="width: 45%;">Subject</span>
                            <span style="width: 16%;">Time</span>
                        </li>
                <?php
                        while($mail = mysql_fetch_object($getMail)) {
                            $status         =       $mail->status;
                            $mailId     =       $mail->mail_id;
                            $from           =       $mail->UserFrom;
                            $subject        =       $mail->Subject;
                            $received       =       $mail->SentDate;
                            $theMessage =       $mail->Message;
                        ?>
                        <li class="outerDiv" style="border: 2px dotted purple;">
                            <button style="display: inline;" class="viewButton">View</button>
                            <button style="display: inline;">Delete</button>
                            <?php
                            echo "<span>" . $mail_id . "</span>";
                            echo "<span style="display: inline-block; width: 8%; border: 1px solid red;'>" . $status . "</span>";
                            echo "<span style='display: inline-block; width: 15%; border: 1px solid red;'>" . $from . "</span>";
                            echo "<span style='display: inline-block; width: 45%; border: 1px solid red;'>" . $subject . "</span>";
                            echo "<span style='display: inline-block; font-size: x-small; width: 17%; border: 1px solid red;'>" . $received . "</span>";                    
                            ?>
                        </li>
                <?php   }

                    } ?>
                    </ul>
                </fieldset>
            </form>
        </section>
        <section id="details">
            <div class="theMessage" style="display: none;"><?php echo $theMessage; ?></div>
            <script type="text/javascript">
                $(document).ready(function() {
                    $(".outerDiv").click(function(e) {
                        if($(e.target).is(".viewButton")) {
                        $(".theMessage").fadeIn(1000);
                        }
                    });
                    return false;
                });
            </script>
        </section>

The following code is only grabbing the value of the last result in the PHP while loop. Any / all help much-appreciated. Thanks!

PHP/HTML/JS:

        <section id="info">
            <?php
                $user = $session->username;
                $q = sprintf("SELECT * FROM mail WHERE UserTo = '%s' ORDER BY SentDate DESC",
                      mysql_real_escape_string($user));
                $getMail = mysql_query($q, $link) or die(mysql_error());

                if(mysql_num_rows($getMail) == 0) {
                    echo "<p>you have no mail</p>";
                }
                else {
                ?>
            <form id="inbox" class="mail">
                <fieldset>
                    <ul>
                        <li style="border: 2px solid purple; width: 100%;">
                            <span style="width: 8%; margin-left: 13%;">Status</span>
                            <span style="width: 15%;">From</span>
                            <span style="width: 45%;">Subject</span>
                            <span style="width: 16%;">Time</span>
                        </li>
                <?php
                        while($mail = mysql_fetch_object($getMail)) {
                            $status         =       $mail->status;
                            $mailId     =       $mail->mail_id;
                            $from           =       $mail->UserFrom;
                            $subject        =       $mail->Subject;
                            $received       =       $mail->SentDate;
                            $theMessage =       $mail->Message;
                        ?>
                        <li class="outerDiv" style="border: 2px dotted purple;">
                            <button style="display: inline;" class="viewButton">View</button>
                            <button style="display: inline;">Delete</button>
                            <?php
                            echo "<span>" . $mail_id . "</span>";
                            echo "<span style="display: inline-block; width: 8%; border: 1px solid red;'>" . $status . "</span>";
                            echo "<span style='display: inline-block; width: 15%; border: 1px solid red;'>" . $from . "</span>";
                            echo "<span style='display: inline-block; width: 45%; border: 1px solid red;'>" . $subject . "</span>";
                            echo "<span style='display: inline-block; font-size: x-small; width: 17%; border: 1px solid red;'>" . $received . "</span>";                    
                            ?>
                        </li>
                <?php   }

                    } ?>
                    </ul>
                </fieldset>
            </form>
        </section>
        <section id="details">
            <div class="theMessage" style="display: none;"><?php echo $theMessage; ?></div>
            <script type="text/javascript">
                $(document).ready(function() {
                    $(".outerDiv").click(function(e) {
                        if($(e.target).is(".viewButton")) {
                        $(".theMessage").fadeIn(1000);
                        }
                    });
                    return false;
                });
            </script>
        </section>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2024-12-04 19:13:05

当您说“获取最后结果的值”时,我猜您正在谈论以下行?

<div class="theMessage" style="display: none;"><?php echo $theMessage; ?></div>

这是在 while() 循环之外,所以是的,$theMessage 只会保留循环中的最后一个值。

看看如何在 else 块内启动

但在 外部关闭 else

块,我猜你太早结束了 whileif-else

When you say "grabbing the value of the last result," I'm guessing you're talking about the following line?

<div class="theMessage" style="display: none;"><?php echo $theMessage; ?></div>

That's outside the while() loop, so yeah, $theMessage will just retain the last value from the loop.

Seeing as how you're starting the <form> within the else block but closing the </form> outside the else block, I'm guessing you're ending your while and if-else too early.

魄砕の薆 2024-12-04 19:13:05

首先,您的代码中有语法错误。
对于 HTML,转义双引号或使用单引号。

First of all you have a syntax error in your code.
Escape double quotes or use single quotes for HTML.

罪#恶を代价 2024-12-04 19:13:05

你使用什么 JavaScript 库?看起来可能是JQuery?

如果您希望选择器返回一个数组,那么您需要使用迭代器来作用于每个元素。尝试每个迭代器函数: http://api.jquery.com/each/

$('.outerDiv').each(function() {
    this.click( function(e) {....

What javascript library are you using? Looks like it could be JQuery?

If you expect your selector to return an array, then you need to use an iterator to act on each element. Try the each iterator function: http://api.jquery.com/each/

$('.outerDiv').each(function() {
    this.click( function(e) {....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文