foreach loop and jQuery - php

I have a PHP and a jQuery script that I use to display a few images. A large image on the left side and 4 thumbnails on the right side. Each time the user clicks an image-thumbnail it will show up on the large image placeholder on the left side.
This is the PHP code I'm using to display the large image and thumbnails:
<div class="pic"><img title="<?php echo $current->alttext ?>" alt="<?php echo $current->alttext ?>" src="<?php echo $current->imageURL; ?>" />
</div>
<ul class="ngg-gallery-list-ir">
<!-- Thumbnail list -->
<?php foreach ( $images as $image ) : ?>
<?php if ( $image->hidden ) continue; ?>
<li id="ngg-image-<?php echo $image->pid ?>" class="ngg-thumbnail-list <?php if ($image->pid == $current->pid) echo 'selected' ?>" >
<a href="<?php echo $image->imageURL ?>" title="<?php echo $image->description ?>" >
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
</li>
<?php endforeach; ?>
This is the jQuery I'm using to update the large image when an user clicks on any thumbnail-image:
jQuery(document).ready(function($){
// handle the click of thumbnail images
// redirect it to change the main image
$(".ngg-thumbnail-list a").click(function(){
var src = $(this).attr("href");
$(".ngg-galleryoverview-ir .pic img").attr("src", src);
return false;
});
// preload the large images
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
});
}
// populate the list of images to load
preload(images);
});
Everything works fine in this setup but I also need to display below the main large-image its title and description. This is the code I'm using:
<div class="container-title-description">
<div class="title"><?php echo $current->alttext ?></div>
<div class="descripton"><?php echo $current->caption ?></div>
</div>
The problem is this: if I add this code inside the foreach loop I get the title and description below each thumbnail-image. If I add this code outside the foreach loop when the main image changes the title and description will stay the same. How can I solve this?
You can view how this setup looks like on this website.

You already add the title and description as hidden title attributes inside the anchor element, so just extract them and update the HTML on demand:
$(".ngg-thumbnail-list a").click(function(){
var src = $(this).attr("href"),
desc = $(this).attr('title'),
title = $(this).find('img').attr('title');
$(".ngg-galleryoverview-ir .pic img").attr("src", src);
$('.container-title-description .title').text(title);
$('.container-title-description .description').text(desc);
return false;
});
Initial HTML (outside your foreach loop):
<div class="container-title-description">
<p class="title"></p>
<p class="description"></p>
</div>

Related

Why Isnt js / jquery Events working in PHP foreach

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

How to retrieve images after clicking thumbnail from a repeat region using Mysql and PHP

I need some help, I have a repeat region which displays pics from a database, when I click on the image I would like to go to another page where there is a photo gallery to display my pictures bigger.
My problem, no matter which thumb I click I get the same set of pictures from the database, which is usually the last thumb listed.
I am new to php, so i am wondering if anyone can help me, thank you.
Code for thumb page
`
<?php do { ?>
<div class="display-property-pic">
<input type="image" id="imageIn1" src="upload/images/<?php echo $row_repeatRegion['thumb']; ?>" alt="Submit" width="100" height="100" ><input name="picHid" type="hidden" value="<?php echo $row_repeatRegion['property_pic1']; ?>">
</div>
<?php } while ($row_repeatRegion = mysqli_fetch_assoc($repeatRegion)); ?>
`
Code for Gallery page
if(isset($_POST['picHid'])){
$pic = $_POST['picHid'];
}
mysqli_select_db($wiplisttest, $database_wiplisttest);
$query_getPics = "SELECT photos.photo_id, photos.pic1, photos.pic2, photos.pic3 FROM photos WHERE photos.property_pic1 LIKE '%$pic%'";
$getPics = mysqli_query($wiplisttest, $query_getPics) or die(mysql_error($wiplisttest));
$row_getPics = mysqli_fetch_assoc($getPics);
$totalRows_getPics = mysqli_num_rows($getPics);
<div class="album">
<div class="description">fixed images</div>
<ol>
<?php do { ?>
<li>
<h4><?php echo "Pic"." ".$row_getPics['pic3']; ?></h4>
<div class="description">Salzburg, Austria</div>
<a href="upload/images/<?php echo $row_getPics['pic3']; ?>">
<img src="upload/images/<?php echo $row_getPics['pic2']; ?>" />
</a>
</li>
<?php } while ($row_getPics = mysqli_fetch_assoc($getPics)); ?>
</ol>
</div>
As you have stored images info in database, there should be id assigned to each image, so when you click on any specific image, get id of that image and retrieve that image on another page using the id.

LightBox plugin not working php jquery

I have following html in which I am using LightBox plugin by Lokesh Dhakar. I am getting images dynamically from database and done all required things to carry out with plugin, but still not working. Only the empty lightbox window appears when page loads.
Here is my code:
while ($fetch = mysql_fetch_array($selectImgResult)) { ?>
<div class="col-md-2 thumbnail">
<span style="color:slateblue;">
<?php echo $fetch['CreateDate']; ?>
</span>
<a href="ProfilePic\<?php echo $fetch['ImageUrl'];?>" data-lightbox="images">
<img src="ProfilePic\<?php echo $fetch['ImageUrl']; ?>" alt="<?php echo $fetch['ImageUrl']; ?>" style="height: 178px;" />
</a>
</div>
<?php }

Get image from database and append into slider of bxslider

I am using jquery.bxslider.js for sliding my images.
In my html code, I use like this:
<ul class="slide">
<li>
<a href="#">
<img src="../img/pic1.png" id="1" />
</a>
</li>
<li>
<a href="#">
<img src="../img/fish.png" id="2" />
</a>
</li>
<li>
<a href="#">
<img src="../img/dog.jpg" id="3" />
</a>
</li>
By default, I use this three images and then when user click next button, I want to get one more image from database,then I append it into with this ajax request to php file.
function load_image() {
var id=$("ul li img:last-child").attr("id");
var result=$.ajax({
type: "GET",
url: "getImage.php?id="+id,
dataType: "html",
success: function(data){
slider.destroySlider();//clear all old slider structure and then insert one more image in the list
$(".slide").append(data);
loadSlide(); // function to call to bxslider to generate slide
}
});
};
My problem, when I call function loadSlide() , the slider always reload and show the 1st image of the list.
But my purpose is not to reload slide, just show the next image when click on next button and while the slide go to next image, it should load one more image from DB to slider.
Any helps will be appreciated !!!
Best regards,
Sokly
You can use Callback API, for join in new image.
This is an easy way for you to get it done, assuming you want to slide the images by news category id, as implemented below.I simply query using the news category id. just embed your statement between the "bxslider" class. I Guess this help because i just implemented it on a site i am working on and it work fine. you can also fetch the title, or picture description along with the image slide, if you want.
<ul class="bxslider">
<?php
$slider_str="select * from news where newscatid=4";
$i=1;
$result=mysql_query($slider_str);
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_assoc($result))
{
if($i%2==1 && $i!=1)
{
}
?>
<li> <div class="slideimagesize"><img src="<?php echo 'extras/'.$row['image'];?>"
width="200" height="150"/></div></li>
<?php
if($i%2==0)
{
}
$i++;
}
}
?>
</ul>
OR You can also achieve this by also using WOWSLIDER, below is the code
//Place this in between the head tag
<link rel="stylesheet" type="text/css" href="engine1/style.css" />
<script type="text/javascript" src="engine1/jquery.js"></script>
//Closing Head tag
<div id="wowslider-container1">
<div class="ws_images">
<ul>
<?php
$slider_str="select * from news where newscatid=4";
$i=1;
$result=mysql_query($slider_str);
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_assoc($result))
{
if($i%2==1 && $i!=1)
{
}
?>
<li><div class="slideimagesize"><a href="news.php?newsid=<?php echo $row['newsid'];?>"><img src="<?php echo 'extras/'.$row['image'];?>" width="350" height="160" alt=" <?php echo $row['subject']; ?>" title="<?php echo $row['subject']; ?>"/>
</a>
</div></li><br>
<?php
if($i%2==0)
{
}
$i++;
}
}
?>
</ul>
</div>
</div>
<script type="text/javascript" src="engine1/wowslider.js"></script>
<script type="text/javascript" src="engine1/script.js"></script>
<!-- End WOWSlider.com BODY section -->
</div>

jquery fadeToggle not working on echoed script

I have a div whose contents are retrieved asynch by $.post().
The contents retrieved are multiple images each with seperate ids.
I want each of the images to fadeToggle on hover()
So i calculate image id in my php on the spot, generate the image and add a jquery handler
and echo back the results:
<script type="text/javascript">
$(document).ready(function ()
{
$("<?php echo '#'.$postid.'image'; ?>").hover(function()
{
$(this).fadeToggle("fast");
},
function()
{
$(this).fadeToggle("fast");
});
});
HTML:
<div id="<?php echo $postid; ?>" class="post_box" >
<img src="<?php echo '/way2tweek'.$path.$name; ?>" width="500" class="img_get"
draggable="false" alt="<?php echo $postid; ?>" id="<?php echo $postid.'image'; ?>">
<div class="imageproperty" id="<?php echo $postid.'props'?>">
</div>
</div>
on the page images are received. scripts are received. the calculated ids match but the images are not fadeToggled. No Console debug message too. Please help.
You have both the hash and the dot in your PHP-generated selector:
<?php echo '#'.$postid.'.image'; ?>
produces a string like
#mypostid.image
I don't know your HTML structure, but you may want the selector to be #mypostid .image (note the space), so:
<?php echo '#'.$postid.' .image'; ?>

Categories