I am implementing a page on which when page loads, the slideshow starts. The pictures for that slideshows are coming form back-end(database). Each picture has its own timer as there are some pictures with the content while some with only heading or logos. At present, I have the same time interval set to each pictures, but now I want to set the time intervals according to their values in database.
here is the code that I have for now with the same time interval:
index.php
<section class='images'>
<? foreach($model->slides as $slide){ ?>
<div class='image'>
<img class='image' src="<?= $slide->image ?.'/pictures/'.$slides->image : '' ?>">
</div>
<? } ?>
</section>
<script type="text/javascript">
$(document).ready(function(){
$('.images').slick({
slidesToShow: 1,
autoplay: true,
autoplaySpeed: 10000,
});
});
</script>
In indexController.php(Controller)
public function index(Array $params = []){
$this->view->slides = \Model\Slide_Picture::getList(['orderBy'=>"image_order asc"]);
$this->loadView($this->view);
}
My table 'slide_images' consist of 3 fields: image, image_order and timer.
As mentioned earlier, this code helps me to get the image slideshow with the time interval of 10s, but now I want to set it different for each images. Say for example, there are 3 images in the database, Picture 1 has a timer value 30s, picture 2 has that of 10s, and picture 3 has value of 40s. So is this possible? If yes, how can I achieve it. Thanks in advance. Appreciate the help.
You would need to add your timing to the database for each image, then once you do that you can return the timing value, and use it in a data attribute. (seems you have done that timer)
Then utilise the afterChange event to get the value and then dynamically change the autoplaySpeed speed.
Below is an example, you would need to integrate it.
$(document).ready(function() {
var images = $('.images');
images.slick({
slidesToShow: 1,
autoplay: true,
autoplaySpeed: 1000,
}).on("afterChange", function(e, slick) {
var slideTime = $('div[data-slick-index="' + slick.currentSlide + '"]').data("timer");
images.slick("setOption", "autoplaySpeed", slideTime);
});
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.css" />
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js"></script>
<section class='images'>
<div class='image' data-timer="2000">
<img class='image' src="http://via.placeholder.com/350x150">
</div>
<div class='image' data-timer="100">
<img class='image' src="http://via.placeholder.com/350x150">
</div>
<div class='image' data-timer="1000">
<img class='image' src="http://via.placeholder.com/350x150">
</div>
<div class='image' data-timer="3000">
<img class='image' src="http://via.placeholder.com/350x150">
</div>
<div class='image' data-timer="1000">
<img class='image' src="http://via.placeholder.com/350x150">
</div>
</section>
Run the snippet to see it in action.
So your loop would look something like:
<section class="images">
<?php foreach ($model->slides as $slide): ?>
<div class="image"<?= !empty($slide->timer) ? ' data-timer="'.$slide->timer.'"' : null ?>>
<img class="image" src="<?= $slide->image ? '/pictures/'.$slides->image : null ?>">
</div>
<?php endforeach ?>
</section>
Related
I understand foreach but this is new for me because within the foreach in PHP I have some script for each item that shows up and wondering why it only works for the item in the first item
In the code, I am getting an image from an MYSQL database. instead of using onclick in js I am learning more into jquery and only when I click the first image it will generate the
$("#main").load("includes/product.php");
path and change I want to change but in the product name and the cart button do not change the visuals.
<section id="new-products" class="clearfix">
<div class="title-block">
<h4>New Products</h4>
</div>
<?PHP
foreach ($item as $items){
?>
<div class="left-col-block">
<article class="product-box clearfix">
<a href="#store/category/<?PHP echo $items['category'];?>/<?PHP echo $items['brand'];?>/<?PHP echo $items['name'];?>" id="product-link">
<img src="<?PHP echo $items['image']; ?>" alt="Card Games" title="Card Games" height="80" width="80">
</a>
<?PHP echo $items['name']; ?><br>
<span class="price">$<?php echo $items['price'];?></span>
</article>
`<script>
$( "#product-link" ).click(function() {
$("#main").load("includes/product.php");
});
</script>
</div>
<div class="left-col-block">
</div>
<?PHP
}
?>
</section>
Expected:
I am wanting each link with the id of product-link to give the event trigger with jquery on each product and each id
Actual Results:
$("#main").load("includes/product.php");
only works on the first id that is equal to product-link
because of you have 3 like for each item and you want to set on click for every 3 link, you must use class attribute and set it unique for each item.
I changed your code to this:
<section id="new-products" class="clearfix">
<div class="title-block">
<h4>New Products</h4>
</div>
<?PHP
foreach ($item as $key=>$items){
?>
<div class="left-col-block">
<article class="product-box clearfix">
<a href="#store/category/<?PHP echo $items['category'];?>/<?PHP echo $items['brand'];?>/<?PHP echo $items['name'];?>" class="product-link-<?=$key?>">
<img src="<?PHP echo $items['image']; ?>" alt="Card Games" title="Card Games" height="80" width="80">
</a>
<?PHP echo $items['name']; ?><br>
<span class="price">$<?php echo $items['price'];?></span>
</article>
`<script>
$( ".product-link-<?=$key?>" ).click(function() {
$("#main").load("includes/product.php");
});
</script>
</div>
<div class="left-col-block">
</div>
<?PHP
}
?>
</section>
Found the answer was
<script>$( ".product-box a" ).click(function() {
$("#main").load("includes/product.php");
});</script>
since product-bot was a class and I didn't realize the # makes it check for id and the. makes it search for a class and class is able to be called multiple times whereas an id has to be unique
I have following script to show the ratings for courses. But it is working properly for pagination first page showing 5 stars, when it comes to second page it showing 10 stars. Code is like below,
<div class="ft-item">
<div class="rating_<?php echo $row->courseId;?>">
</div>
<script>
$('.rating_<?php echo $row->courseId;?>').raty({
starOff : '<?php echo base_url()?>public/images/star/icon-star-2.png',
starOn : '<?php echo base_url()?>public/images/star/icon-star-1.png',
starHalf: '<?php echo base_url()?>public/images/star/icon-star-3.png',
readOnly : true,
half : true,
score : <?php echo $r;?>,
space : true
});
</script>
First page course image
In this course five stars are displaying and located in first page.
Second page course image
In this course 10 stars are displaying and located in second page.
Is the above script is not binding in second page?
As you are calling the pagination via ajax, then you need to call your script after ajax response in the success function too. But the problem is that your score needs to be specific to every book/item, you need to change the
score : <?php echo $r;?>
and
$('.rating_<?php echo $row->courseId;?>')
and get the score via javascript rather than php and move the function into ajax success. Now to get the data from the javascript and removing php you need to use data attributes for this purpose add the rating like data-rating=<?=r?> whie populating the html via php and then use .each and iterate all elements and get the respective data-rating attribute in javacript via $(element).data('rating') and assign to the rate option. You can see the below example how to populate the rating based on the data attrbutes you just need to copy this javascript and update you html accordingly and you are done.
$('.rating').each(function() {
var ele = $(this);
$(ele).raty({
starOff: 'https://cdnjs.cloudflare.com/ajax/libs/raty/2.8.0/images/star-off.png',
starOn: 'https://cdnjs.cloudflare.com/ajax/libs/raty/2.8.0/images/star-on.png',
starHalf: 'https://cdnjs.cloudflare.com/ajax/libs/raty/2.8.0/images/star-half.png',
readOnly: true,
half: true,
score: ele.data('rating'),
space: true
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/raty/2.8.0/jquery.raty.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/raty/2.8.0/jquery.raty.js"></script>
<div class="ft-item">
<div class="rating" data-rating="3"></div>
</div>
<div class="ft-item">
<div class="rating" data-rating="4"></div>
</div>
<div class="ft-item">
<div class="rating" data-rating="2"></div>
</div>
<div class="ft-item">
<div class="rating" data-rating="1"></div>
</div>
<div class="ft-item">
<div class="rating" data-rating="5"></div>
</div>
<div class="ft-item">
<div class="rating" data-rating="4"></div>
</div>
<div class="ft-item">
<div class="rating" data-rating="4"></div>
</div>
I created a menu with links (#) which aims to show & hide a div thanks to the onclick attribute.
For example the link "home" can hide/show the div "contentHome", the link "contact" can hide/show the div "contentContact".
The problem I have is that the container div "content" includes the div "contentHome" and "contentContact" when i click on the 2 links one time...
Here is a part of my code :
<li><a href="#" onclick="toggle('contentHome');" ><?php echo $lang['MENU_ACCUEIL'];?>
<div id="content">
<div id="contentHome">
<img src="Images/photo.jpg" class="imgphoto" alt="Simplepic" />
<?php echo $lang['TEXTE_ACCUEIL'];?>
<img src="Images/work.png" class="imgwork" height="100" width="100" alt="Travaux" />
<?php echo $lang['TEXTE_SOON'];?>
</div>
<div id="contentContact">
...
</div>
</div>
The position is static, I can't put a z-index and when I put display:none;, divs don't want to load.
What do you think ? I just wanted to create a dynamic menu with div but the php includes makes me struggle...
Here i've created some example of toggle with divs.
<li>Toggle Red</li>
<li>Toggle Blue</li>
<div id="content">
<div class="toggled-item" id="contentHome">
<div style="width:200px; background-color:#f00; height:200px;"></div>
</div>
<div class="toggled-item" id="contentContact">
<div style="width:200px; background-color:#00f; height:200px;"> </div>
</div>
</div>
<script>
$(document).ready(function(){
$("#lnk_contentHome, #lnk_contentContact").click(function(){
$(".toggled-item").hide();
var target = $(this).attr("id").split("_")[1];
$("#" + target).toggle();
});
});
</script>
https://jsfiddle.net/ow7dd12f/5/
Hope helps.
I need help passing row from a mysql database into a div popup window?
how doI pass product id into popup div which I call through View?
For Example I Want Like This
View</li>
Here My Code
<div class="latest_products_sec">
<div class="latest_products">
<div class="title_box">
</div>
<!--Latest Products Slider -->
<div class="latest_pro_box">
<h4>Latest Shirt's Collection</h4>
<div class="latest_products_slider">
<?php
$queryshirt=mysql_query("select image,id from products
where cid='1' LIMIT 5") or die ('die shirt query');
while($rowshirt=mysql_fetch_array($queryshirt))
{
echo '<ul>';
echo '<li><img src="admin/'.$rowshirt['image'].'"
width="225" height="300" alt="" />
View</li>';
echo '</ul>';?>
}
?>
#shirt Div Popup Window
<div id="shirt" class="proquickview">
<span class="close_btn" onclick="parent.jQuery.fancybox.close();">Close</span>
<h2 class="title">quick view</h2>
<div class="quickviewinfo">
<?php
// I Need To Get ID Here
echo $id=$_REQUEST['id'];
?>
<div class="quickviewinforight">
<div class="titlerow">
<h2>Latest Shirt's Collection</h2>
<div class="start_ratings">
<div class="start_rating">
</div>
</div>
<div>
<div class="quick_review">
<h3>Quick Discription</h3>
<p>TEST.</p>
</div>
<div class="qty_row">
<div class="qty_field">
<label>Select Quantity</label>
<span>
<select name="S">
<option>5</option>
</select>
</span>
</div>
<br class="clear" />
<span class="total_price">Price <strong>$88.00</strong>
<del>$102.00</del></span>
ADD TO CART
</div>
<div class="total_price_row">
</div>
</div>
</div>
</div>
Javascript For Popup Window
$(document).ready(function() {
$(".view_btn").fancybox({
'titlePosition' : 'inside',
'transitionIn' : 'none',
'transitionOut' : 'none'
});
});
$(document).ready(function(){
// Cufon Functions //
Cufon.replace ('.latest_products_slider ul li a.view_btn',{hover:true});
});
You can use new 'echo'
echo 'View</li>';
instead of using this
echo '<li><img src="admin/'.$rowshirt['image'].'" width="225" height="300" alt="" />
View</li>';
combined 'echo' statement for image and link to View
Use jQuery .load function. Then make a hidden div, tgen on an action, use the load function to load the hidden div and change the css selection to visible. Very simple, add effects as well.
i need help to passing row from mysql database into div popup window?
how do i passing product id into popup div which i called through View?
For Example I Want Like This
i want to fetch product details into `product quick view popup window`
through product `ID`?
Here My Code
<div class="latest_products_sec">
<div class="latest_products">
<div class="title_box">
</div>
<!--Latest Products Slider -->
<div class="latest_pro_box">
<h4>Latest Shirt's Collection</h4>
<div class="latest_products_slider">
<?php
$queryshirt=mysql_query("select image,id from products
where cid='1' LIMIT 5") or die ('die shirt query');
while($rowshirt=mysql_fetch_array($queryshirt))
{
echo '<ul>';
echo '<li><img src="admin/'.$rowshirt['image'].'"
width="225" height="300" alt="" />
View</li>';
echo '</ul>';?>
}
?>
#shirt Div Popup Window
<div id="shirt" class="proquickview">
<span class="close_btn" onclick="parent.jQuery.fancybox.close();">Close</span>
<h2 class="title">quick view</h2>
<div class="quickviewinfo">
<?php
// I Need To Get ID Here
echo $id=$_REQUEST['id'];
?>
<div class="quickviewinforight">
<div class="titlerow">
<h2>Latest Shirt's Collection</h2>
<div class="start_ratings">
<div class="start_rating">
</div>
</div>
<div>
<div class="quick_review">
<h3>Quick Discription</h3>
<p>TEST.</p>
</div>
<div class="qty_row">
<div class="qty_field">
<label>Select Quantity</label>
<span>
<select name="S">
<option>5</option>
</select>
</span>
</div>
<br class="clear" />
<span class="total_price">Price <strong>$88.00</strong>
<del>$102.00</del></span>
ADD TO CART
</div>
<div class="total_price_row">
</div>
</div>
</div>
</div>
Javascript For Popup Window
$(document).ready(function() {
$(".view_btn").fancybox({
'titlePosition' : 'inside',
'transitionIn' : 'none',
'transitionOut' : 'none'
});
});
$(document).ready(function(){
// Cufon Functions //
Cufon.replace ('.latest_products_slider ul li a.view_btn',{hover:true});
});
It's hard to tell what is wrong as you have only given part of the story.
Fundamentally you need:
Action by the user (usually a click) triggers AJAX call sending a unique ID onclick="updatefunction(this.id) in a SPAN or DIV will do the trick:
<SPAN onclick="updatefunction(this.id)" id="1234">Your Stuff to update</SPAN>
The JS function updatefunction must send the detail/ID to a separate PHP script (you can use the original but this just makes it clunky) which queries your database on the ESCAPED data you send.
Once the data is ready, the JS will update the window/pop it up.
AFAICS you are missing the extra bit that actually does the querying or not applying a suitable onclick.
Why not have a look at the excellent W3Schools Ajax tutorial - it will help though does not use jQuery.