I have a list with some fake sale notification elements. They all have the same class. With the code I got, all notifications are shown at the same time but I only need to show 1 random notification each time. How can I do that?
This is my code at the moment:
PHP to generate the elements:
$notification .= '
<section class="custom-social-proof">
<div class="custom-notification">';
// Get fake sale notification info
$specarticles = '
SELECT
c.alias as content_alias,
c.title as content_title,
c.catid,
c.state,
c.featured,
c.images,
ca.id,
ca.published,
ca.title as cat_title,
ca.alias as cat_alias
FROM snm_content c
LEFT JOIN snm_categories ca
ON c.catid = ca.id
WHERE c.featured = 1
AND c.state = 1
AND ca.published = 1';
$specarticlescon = $conn->query($specarticles);
while($specarticles = $specarticlescon->fetch_assoc()){
$imagesjson = json_decode($specarticles['images']);
if(!empty($imagesjson->image_intro)){
$image = 'https://website.nl/cms/'.$imagesjson->image_intro;
}else{
$image = '';
}
$notification .= '
<div class="custom-notification-container">
<div class="custom-notification-image-wrapper">
<img src="'.$image.'">
</div>
<div class="custom-notification-content-wrapper">
<p class="custom-notification-content">
Piet De Graaf - Spijkenisse<br>reserveerde <a href="'.$specarticles['cat_alias'].'/'.$specarticles['content_alias'].'">'.$specarticles['content_title'].'</a>
<small>vandaag</small>
</p>
</div>
</div>';
}
$notification .= '
<div class="custom-close"></div>
</div>
</section>';
echo $notification;
And my jquery:
setInterval(function(){ tpj(".custom-social-proof").stop().slideToggle('slow'); }, 8000);
tpj(".custom-close").click(function() {
tpj(".custom-social-proof").stop().slideToggle('slow');
});
I found out you can get something random with eq. So I tried the following:
var len = tpj(".custom-social-proof").length;
var random = Math.floor( Math.random() * len ) + 1;
setInterval(function(){ tpj(".custom-social-proof").eq(random).stop().slideToggle('slow'); }, 8000);
tpj(".custom-close").click(function() {
tpj(".custom-social-proof").stop().slideToggle('slow');
});
But this stops the notification from showing up entirely. What am I doing wrong?