I have read the other threads on this topic but have not found an answer to my particular situation. I have modified the code from http://jsfiddle.net/g9R4H/ to produce an example of what I'm trying to accomplish. In my implementation I may have multiple duplicates of multiple images. One image may have two thumbnails and another may have 3 all on the same page. I have narrowed down the problem to the eq(0) setting and have determined that changing that value to 1 selects a different image when fancy box opens. I have not been able to determine how to set that value based on the image that is clicked. In my actual scripts the links are being generated via php and I do know when I create the trigger links what image value it relates to. I'm quite the rookie when it comes to javascript so keep that in mind when you answer. TIA
<a data-trigger-rel="gallery" class="fancybox-trigger" href="http://fancyapps.com/fancybox/demo/2_b.jpg"><img src="http://fancyapps.com/fancybox/demo/2_s.jpg" alt=""/></a>
<a data-trigger-rel="gallery" class="fancybox-trigger" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
<br />
<br />
<a rel="gallery" class="fancybox" href="http://fancyapps.com/fancybox/demo/2_b.jpg"><img src="http://fancyapps.com/fancybox/demo/2_s.jpg" alt=""/></a>
<a rel="gallery" class="fancybox" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
$(".fancybox-trigger").click(function() {
$("a[rel='" + $(this).data('trigger-rel') + "']").eq(0).trigger('click');
return false;
});
$(".fancybox").fancybox();
I haven't had any responses, but after some thought I come to a solution using PHP. Basically I am using php to create a separate trigger for each occurrence of duplicate images. When the trigger links are created the trigger array is updated. After all the links have been added, the trigger array is processed and a separate trigger created for each. If someone else is having this issue, hopefully this will help.
<?php
$a=0;
$image_id_array=array();
$trigger_array=array();
$result_set= get_collection_standards($sel_collection);
while ($standard = mysqli_fetch_array($result_set)) {
$image_id=$result["image_id"];
$path=$result["path"].$result["file_name"];
$caption = htmlentities($result["caption"]);
If (!array_key_exists($image_id,$image_id_array)){
$image_id_array[$image_id]=$a;//image_id receives the key value and the value is the photo number in the gallery - 1.
$a+=1;
$link="<a rel=\"feature_gallery\" class=\"fancybox\" href=\"{$path}-l.jpg\"";
} Else { //if the image does exist create a trigger link.
$link="<a data-trigger-rel=\"feature_gallery\" class=\"fancybox-trigger". $image_id_array[$image_id] .
"\" href=\"{$path}-l.jpg\" ";
if (!array_key_exists($image_id,$trigger_array)) { //update the trigger array to be used below.
$trigger_array[$image_id]=$image_id_array[$image_id];
}
}
$link .= " title=\"{$caption}\"><img src=\"{$path}-s.jpg\" ></a>";
echo $link;
}
foreach ($trigger_array as $position) {
echo "<script>";
echo " $(\"a.fancybox-trigger{$position}\").click(function() {";
echo " $(\"a[rel='\" + $(this).data('trigger-rel') + \"']\").eq(" . $position . ").trigger('click');";
echo " $.fancybox.open($(this).attr('tirgger-rel'));";
echo " return false;";
echo "});";
echo "</script>";
}
?>
Related
I am fetching data from database table and showing result in HTML table. What I want to achieve is that, there is image column in every row, and every image is fetched from its URL (stored in database). I am opening an image in new tab whenever it is clicked.
My question is that, how can I store url of image so that on the next page I don't want to show that real url?
The next page will look like this link:
www.example.com/full_size_image.php
and add
img tag
there to show that page
how can I store that specific image url when someone click on it?
<img src= "<?php echo $_SESSION['link'] ;">
My current code is :
while($result = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>".$result['c']."</td>";
echo "<td>".$result['UserName']."</td>";
echo "<td>".$result['UserProblemKeyword']."</td>";
echo "<td> <a href ='".$result['UserProblemPicture']."' target='_blank'><img src='".$result['UserProblemPicture']."' height='62' width='62'> </a> </td>";
echo "</tr>";
}
Use a parameter via the GET Request?
So instead of directing to just /full_size_image.php direct to
/full_size_image.php?img=filename.png
then in your php code for /full_size_image.php
You can fetch the file name via
<?php
$img = isset($_GET['img']) ? htmlspecialchars($_GET['img']) : "";
if ($img == "") {
//img not set
}
//do whatever with the filename
I have Images stored in my DB, I want to retrieve them all and display them in the browser. It works just fine, but I want to give the user the ability to click on the picture so that they can have a better view of the picture (so display it with its original size). I tried many things but didn't work. second question: if I want my code to support multiple images type (such gif, jpg, etc..), if there a way to do it without having to save the image type (when I insert the images) and play with a whole bunch of if/else (when I retrieve them)?
This is my code
$count = 0;
echo " <div class=\"row\">";
while($row = $result->fetch_assoc()) {
$imagename = base64_encode( $row['Image'] );
if(($count%3) ==0){
echo "</div>";
echo " <div class=\"row\">";
echo " <div class=\"col-sm-2\">";
echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}else{
echo " <div class=\"col-sm-2\">";
echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}
}
echo "</div>" ;
The answer to your first question is "You have to use light-box jQuery plugin", in order to display larger (original size) of a thumbnail you need to use any lightbox plugin, I recommend using this one
http://www.jqueryscript.net/zoom/Responsive-jQuery-Lightbox-Like-Image-Zooming-Plugin-Lighter.html
Or you can search for any other plugin, there are thousands of them.
Regarding your second question, If you will use data as an images source, you have to specify a type, no way you can do that without mentioning types.
I have something that im currently working on, however it appears that the $_GET doesn't completely work.
I have a JavaScript light box that brings up an image in a little window, this works however i can only guess that it is using the same URL over and over again.
However when i view the source for the page (and even click one of the links in the source) it will display the correct data.
But the lightbox only seems to display the first image.
This is the JavaScript
<script>
//Checkes if any key pressed. If ESC key pressed it calls the lightbox_close() function.
window.document.onkeydown = function (e)
{
if (!e){
e = event;
}
if (e.keyCode == 27){
lightbox_close();
}
}
</script>
<script>
//This script makes light and fade divs visible by setting their display properties to block.
//Also it scrolls the browser to top of the page to make sure, the popup will be on middle of the screen.
function lightbox_open(){
window.scrollTo(0,0);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
</script>
<script>
//This makes light and fade divs invisible by setting their display properties to none.
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
</script>
I wont show the CSS i dont think thats relivant (If someone wants it then ask away)
The relevant part that creates the links is this, its part of a ForEach statement all PHP
$i = 0;
foreach ($nrows as $nrow)
{
$id = $nrow['id'];
$rid = $nrow['RaidID'];
$bid = $nrow['BossID'];
$normal = $nrow['NormalKills'];
$heroic = $nrow['HeroicKills'];
$boss = substr($nrow['BossName'], 0, 3);
$p1 = $id + $bid.".php";
$image = $boss . $p1;
#echo $image;
echo $bid;
if ($oid != $rid)
{
$i = 0;
}
if ($i == 0) {
?><td style="width: 176px;"><center><b><?php echo $nrow['raid']; ?> </b></center></td> </tr><?php
$i++;
}
?><tr><td style="width: 176px;"><div align="left"><?php echo $nrow['BossName']; ?><div id="light"><img src="bossdata/template.php?boss=<?php echo $bid;?>"></a></div><div id="fade" onClick="lightbox_close();"></div>
</div>
<?php
if ($heroic == 0)
{
if ($normal > 0)
{
echo '<img src="images/whiteskull.png" align="right" alt="Normal Kill">';
}
else
{
echo '<img src="images/redx.png" align="right" alt="Not Killed">';
}
}
else
{
echo '<img src="images/redskull.png" align="right" alt="Normal Kill">';
}
?>
</td></tr><?php
$oid = $id;
}
Now this all works, and it actually displays an image with data, however no matter what link i click the boss data is always from the first one on the list.
To me this means that the data is getting through, and reaching the the right parts on image so its "Working", but all the links do the same thing and show the same data :(
*Removed last code Bulk
You have multiple div with the same ID "light" since you create them in a foreach loop.
<div id="light">
Your function lightbox_open() opens all the divs that have id "light".
document.getElementById('light').style.display='block';
That's why you always see the first lightbox. Because the others are behind the first one.
you should try something like this :
function lightbox_open(elem){
window.scrollTo(0,0);
elem.getElementByClass('light').style.display='block';
elem.getElementByClass('fade').style.display='block';
}
And change this :
<a href="#" onclick="lightbox_open();">
By this :
<a href="#" onclick="lightbox_open(this);">
And replace id by class in your div definition :
<div class="light">
$_GET is working correctly in your code.
The issue is in the way you are combining JavaScript and PHP in the second code box. First, all of your divs have the same ID: "light" which is wrong because they all IDs are meant to be unique within the HTML document. You need to identify them uniquely, for example appending the BossID to them.
After identifying each div uniquely you'll have to edit lightbox_open and lightbox_close so they can receive the BossID of the divs that you want to show and hide.
I have a script which will list all the files from the image folder on the server into a drop down box.
$dir='images';
$list=scandir($dir);
if(isset($_POST['submit'])) echo 'Selected: ' . $_POST['image'];
foreach($list as $file)
{
//ignore . (current dir) and .. (parent dir)
if($file!=='.'&&$file!=='..')
{
echo "<option value=\"$file\"";
if(isset($_POST['submit'])&&$_POST['image']==$file) echo 'selected="selected"';
echo ">$file</option>";
}
}
What I am trying to do is when the file name of the image is selected that image is displayed.
echo "<img src='images/". $file ."' />";
I tried using the file verable but it only seams to display the last image in the directory. How would I go about fixing this.
You will have to do this with Javascript. Once the php script loads it has no one of knowing what is happening in the users window. You will want to create an event listener to the onChange even of your option group. And when the selection changes you will want to replace the src of the image tag being used to show a preview.
I'd highly suggest using a JS Library such as Jquery or simliar to help with the event listening, and dom manipulation.
If you mean doing this on the form load, I’d say replace
if(isset($_POST['submit'])&&$_POST['image']==$file) echo 'selected="selected"';
with
if(isset($_POST['submit'])&&$_POST['image']==$file)
{
echo 'selected="selected"';
$selected_file = $file;
}
then this instead
echo "<img src='images/". $selected_file ."' />";
Because at the moment, it’ll just load which ever one is last out of the foreach
This is my problem: I have an ecommerce website, and I would like my search results to come up with products that have their links avaiable either within the image of the product on the search page, or next to the image. My current code looks like this:
if($_GET['searchBox'] !='')
{
if(mysql_num_rows($searchresult)==0) {
echo 'Your search returned no results';
}
else
{
while ($row = mysql_fetch_assoc($searchresult))]
{
echo "<img src='".$row['image']."'/>".' '.$row['name'].' £'.$row['price'].' '.$row['ProductUrl'];
}
}
}
?>
I have spent ages trying to get an URL into the image area, but I can't make it work.
Please help!!
What the problem put image in to the anchor tag.
Have you tried like this
echo "<a href='".$row['ProductUrl']."'><img src='".$row['image']."'/></a>";
EDIT
echo "<img src='".$row['image']."'/>".' '.$row['name'].' £'.$row['price'].'
<a href="'.$row['ProductUrl']."'>".$row['ProductUrl']."</a>";