[英]JQuery and PHP don't show all query results but some (randomly)
1st time asking here!第一次在这里问! I have a MySQL SELECT query and array of results.我有一个 MySQL SELECT 查询和结果数组。 I have a container and within that container I want to display child div(s) displaying the array result + JQuery to add some nice fadeIn and fadeOut.我有一个容器,在该容器内我想显示子 div(s) 显示数组结果 + JQuery 以添加一些漂亮的淡入和淡出。 The issue is the script display some of the array but NEVER all.问题是脚本显示了一些数组,但从不显示全部。 I have 7 but the page shows 5 sometimes even 2 only and the data seems to be picked randomly.我有 7 个,但页面显示 5 个,有时甚至只有 2 个,而且数据似乎是随机选取的。
// Fetch user notifications
$stmt0 = $conn->prepare("SELECT notifid, notification FROM notifications WHERE userid = :userid AND username = :uname;");
$stmt0->execute(['userid' => $userid, 'uname' => $username]);
$results0 = $stmt0->fetchAll();
foreach ($results0 as $row0) {
$notifid = $row0['notifid'];
$notification = $row0['notification'];
$notificationid = substr(sha1(mt_rand()), 0, 8);
echo "<div class='bluenot' id='".$notificationid."'>✔ <b><i>$notification</i></b></div>";
echo '<script>
$(document).ready(function(){
var notifid = ' . $notificationid . ';
$("#notifybox").append($ (notifid) );
setTimeout(function() {
$(notifid).fadeIn("fast", function () { $(this).delay(5000).fadeOut("fast"); });
$(notifid).css("display", "block");});
});
</script>';
CSS : CSS :
.notifybox {
width: auto;
height: auto;
position: fixed;
bottom: 0;
left: 15px;
z-index: 1000 important;
}
.bluenot {
width: 400px;
height: 80px;
padding: 20px 40px 20px 40px;
margin-top: 8px;
margin-bottom: 8px;
display: none;
background: #aaa9ff;
border-left: #2933aa 10px solid;
border-radius: 10px;
color: #353759;
text-align: center;
}
SOLVED: Made a copy of class .bluenot and named it .dbnot then changed the code to :已解决:复制类 .bluenot 并将其命名为 .dbnot 然后将代码更改为:
echo "<div class='dbnot' id='".$notificationid."'>✔ <b><i>$notification</i></b></div>";
echo '<script>
$("#notifybox").append($ (".dbnot") );
$(".dbnot").fadeIn( 800 ).delay( 1000 ).fadeOut( 400 ).$(".dbnot").css("display", "block");
</script>';
Hope this helps anyone who would face the same issue.希望这可以帮助任何面临同样问题的人。
You are missing the closing curly brackets for your foreach loop but assuming you have it after your <script> block then i would guess you are over writing the variable "notifid"您缺少 foreach 循环的结束大括号,但假设您在 <script> 块之后拥有它,那么我猜您正在编写变量“notifid”
I would try to rearrange your logic and if you are deadset on this approach then create the variable "notifid" with a another variable to avoid duplicate declarations我会尝试重新排列您的逻辑,如果您对这种方法感到厌烦,则使用另一个变量创建变量“notifid”以避免重复声明
something maybe like可能像
$(document).ready(function(){
var notifid_' . $notificationid . ' = ' . $notificationid . ';
$("#notifybox").append($ (notifid_' . $notificationid . ') );
setTimeout(function() {
$(notifid_' . $notificationid . ').fadeIn("fast", function () { $(this).delay(5000).fadeOut("fast"); });
$(notifid_' . $notificationid . ').css("display", "block");});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.