problem using php and javascript for a photogallery.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roondog
Forum Newbie
Posts: 18
Joined: Sun Jul 22, 2007 11:56 am

problem using php and javascript for a photogallery.

Post by roondog »

I'm trying to make a photo gallery with thumbnails and when the3y are clicked the full size picture is shown.
here is my code

Code: Select all

<?php

$con = mysql_connect("localhost","******","*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("model", $con);

$result = mysql_query("SELECT * FROM fashion");

while($row = mysql_fetch_array($result))
  {
  echo "<a onClick='document.getElementById('main').src=". $row[picname] . ";><img class='t_nail' src=". $row[picname] . " alt='thumbnail' /></a>";
}
  
mysql_close($con);
?>
I get the thumbnails but can get the onClick to work.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

double check your HTML source -- it doesn't appear to have anything to do with PHP.
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post by Steve Mellor »

your problem is with the javascript. This is (in HTML) what you're after

Code: Select all

<a href="#" onclick="myPic.src='big.jpg'";>
<img name="myPic" id="myPic" class="t_nail" src="small.jpg" alt="thumbnail" />
</a>
But you'll need to switch some of the quote marks so that you don't escape the string before you want to.

Try this:

Code: Select all

echo "<a href=\"#\" onclick=\"myPic.src='$row[picname]'\";>
<img name=\"myPic\" id=\"myPic\" class=\"t_nail\" src=\"$row[picname]\" alt=\"thumbnail\" />
</a>";
Javascript can be funny at times.

[NINJA STEALTH EDIT]

Also, you will want to increment the id and name of the img element if you're going to be looping them.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Just double checking, you realize you are using the same data?

Code: Select all

echo "<a onClick='document.getElementById('main').src=". $row[picname] . ";><img class='t_nail' src=". $row[picname] . " alt='thumbnail' /></a>"; 
$row['picname'] is used both times.
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post by Steve Mellor »

Zoxive wrote:Just double checking, you realize you are using the same data?
Either way his code wouldn't have worked. Mine does :D

It's a very good point though. You won't notice a picture change if you have the same picture twice. Of course you could put the picture in a different directory and get around the naming problem.
Post Reply