jquery只返回php while循环的最后结果
以下代码仅获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您说“获取最后结果的值”时,我猜您正在谈论以下行?
这是在 while() 循环之外,所以是的,$theMessage 只会保留循环中的最后一个值。
看看如何在
else
块内启动块,我猜你太早结束了
while
和if-else
。When you say "grabbing the value of the last result," I'm guessing you're talking about the following line?
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 theelse
block but closing the</form>
outside theelse
block, I'm guessing you're ending yourwhile
andif-else
too early.首先,您的代码中有语法错误。
对于 HTML,转义双引号或使用单引号。
First of all you have a syntax error in your code.
Escape double quotes or use single quotes for HTML.
你使用什么 JavaScript 库?看起来可能是JQuery?
如果您希望选择器返回一个数组,那么您需要使用迭代器来作用于每个元素。尝试每个迭代器函数: http://api.jquery.com/each/
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/