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

时间:2011-08-02 01:51:55

标签: php jquery while-loop

以下代码仅获取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>

3 个答案:

答案 0 :(得分:0)

当你说&#34;抓住最后一个结果的值时,&#34;我猜你在谈论下面一行?

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

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

看到你如何在<form>区块中else开始,但在</form>区块之外关闭else,我猜猜你& #39;过早结束whileif-else

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您使用的是哪个JavaScript库?看起来它可能是JQuery?

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

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