PDA

View Full Version : Changing main image by clicking on thumbnails



Snorii
02-27-2003, 11:59 PM
I'm using Christina's fork, and I currently have my images displayed horizontally under the main image. (Just had to change the renderListingsImages function in style.php).

Now what I want to do is use a little JavaScript to change the main pic by clicking on the thumbnails. I have the JavaScript worked out it and it runs fine in html.

Basically, the main image has a name tag added. I changed the renderMainImage function in style.php to accomplish that part. So I added
name=\"mainpic\" in the img src tag.

On each thumbnail is an anchor tag that calls my JavaScript function and passes a parameter to it.

<a href=\"javascript:picchange($count)\">

I am passing count since it is incremented in a while loop. I may have to change this since it starts at zero, but I'll worry about that later.

And I added the JavaScript function into the page


//JavaScript Function to switch images
echo "<SCRIPT Language=\"JAVASCRIPT\">
function picchange&#40;num&#41;&#123;
if&#40;document.images&#41;&#123;
document.mainpic.src = \"http&#58;//mywebsite.com/openrealty/property-images/$mlsNumber-\" + num + \".jpg\";
&#125; else &#123;
document.mainpic.src = \"http&#58;//mywebsite.com/openrealty/property-images/$mlsNumber-1.jpg\";
&#125;
&#125;
</SCRIPT>";


The code accepts a parameter (num) and then changes the url for the image. I honestly don't think the way I'm doing it now is the best way. Unforunately, I don't know SQL and I'm having trouble figuring out what to use to change the image.

The normal JavaScript is
<SCRIPT Language="JAVASCRIPT">
function changepic&#40;num&#41;&#123;
if&#40;document.images&#41;&#123;
document.main.src = "http&#58;//.../1-" + num + ".jpg";
&#125; else &#123;
document.main.src = "http&#58;//.../1-1.jpg";
&#125;
&#125;
</SCRIPT>

<img src="http&#58;//..../1-1.jpg" width="258" height="193" border="1" name="main">

<br> //whatever html code

<a href="javascript&#58;changepic&#40;1&#41;">
<img src="http&#58;//.../1-1.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;
<a href="javascript&#58;changepic&#40;2&#41;">
<img src="http&#58;//.../1-2.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;
<a href="javascript&#58;changepic&#40;3&#41;">
<img src="http&#58;//.../1-3.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;
<a href="javascript&#58;changepic&#40;4&#41;">
<img src="http&#58;//.../1-4.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;
<a href="javascript&#58;changepic&#40;5&#41;">
<img src="http&#58;//.../1-5.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;
<a href="javascript&#58;changepic&#40;6&#41;">
<img src="http&#58;//.../1-6.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;
<a href="javascript&#58;changepic&#40;7&#41;">
<img src="http&#58;//.../1-7.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;
<a href="javascript&#58;changepic&#40;8&#41;">
<img src="http&#58;//.../1-8.jpg" width="54" height="37" border="0"></a>&amp;nbsp;&amp;nbsp;

Basically, the images are stored as an incremented number. But with a database, there should be a way to call the image name and pass that to the JavaScript function as a string.

As always, Thanks for any help in advance.

- Snorii

Snorii
02-28-2003, 12:45 AM
I tried to change things up a little. Instead of passing the parameter $count to changepic, I changed it to $thumb_file_name.

(I know it doesn't sound right, but it is the right file. I turned create thumbnails off in common.php)

And now my JavaScript looks like (Keep in mind this is in php too.)

echo "<SCRIPT Language='JAVASCRIPT'> function picchange&#40;name&#41;&#123;if&#40;document.images&#41;&#123;document.mainp ic.src = 'http&#58;//mywebsite.com/openrealty/images/listing_photos/' + name; &#125; else &#123; document.mainpic.src = 'http&#58;//mywebsite.com/openrealty/images/listing_photos/2_27686-2.jpg'; &#125;&#125;</SCRIPT>";

There aren't any errors, but the pic isn't changing. I tried to open the link in a new window, and got the following:

javascript:picchange(2_27686-3.jpg)

That at least means that the parameter is being passed, so good news at least. Now to figure out why the pic isn't changing. :?

My next idea is to put the javascript in the header rather than the php part. Not sure why I didn't try that to begin with.

Snorii
02-28-2003, 01:08 AM
Well, I tried to put it in the view_top.html file, but that didn't work either. Everything looks right, but it isn't working.

*Yaaaawwwwwnnnnnn*

Time for some sleep. :(

Snorii
03-02-2003, 12:55 AM
I fixed it. See http://jonroig.com/freecode/phpBB/viewtopic.php?p=5305&amp;sid=55a008eafbb378bab4690ff76 9d1ab0b#5305