Solved

Image previews with dropdown

Posted on 2008-10-08
14
442 Views
Last Modified: 2010-04-16
I'm trying to piece together some code to get image previews to work with my existing php site. For my site to work, the option values in the selects are the id numbers of the images, not the path of the image. The image preview code I've found needs the option value to be the path of the image instead of the id. Can I just add an id for the image preview function to look at instead? This is what I have so far:

Does not work:
<select name="centerphoto" onchange="document.getElementById('img_preview').src=this.options[this.selectedIndex].value; document.getElementById('img_preview').style.display='';">
<?php
foreach($artwork['centerphotos'] AS $centerphoto)
{
   echo <<<HTMLOUT
<option value="$centerphoto[id]">$centerphoto[name]</option>
HTMLOUT;
}
?>
</select>
<img id='img_preview' style='display:none'>
0
Comment
Question by:Russelauto
  • 8
  • 5
14 Comments
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22675081
Given the image ID, is there a way build the image path?

If yes, do something like:

... document.getElementById('img_preview').src='/path/to/image' + this.options[this.selectedIndex].value + '.gif'; ...
0
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22675151
If no, and if the url is stored in your array, you may do something like this:
<?php
foreach($artwork['centerphotos'] as $centerphoto)
{
   echo '<input type="hidden" id="url_' . $centerphoto['id'] . '" name="url_' . $centerphoto['id'] . '" value="' . $centerphoto['url'] . '">';
}
?>
<select name="centerphoto" onchange="document.getElementById('img_preview').src=document.getElementById('url_' + this.options[this.selectedIndex].value).value; document.getElementById('img_preview').style.display='';">
<?php
foreach($artwork['centerphotos'] as $centerphoto)
{
   echo '<option value="' . $centerphoto['id'] . '">' . {$centerphoto['name'] . '</option>';
}
?>
</select>
<img id="img_preview" name="img_preview" style="display:none">

Open in new window

0
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22675171
In my previous comment I mean by "url" the image path. I assume that there is a $centerphoto['url'] beside each $centerphoto['id'] and $centerphoto['name'] that contains the image path.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 8

Expert Comment

by:fcardinaux
ID: 22675242
Here is a tested solution. Just replace the URLs by image locations you know:
<?php
$artwork = array('centerphotos' => array(
    array('id' => 1, 'name' => 'sun',  'url' => 'relative/path/to/image_of_the_sun.jpg'), 
    array('id' => 2, 'name' => 'moon', 'url' => 'relative/path/to/image_of_the_moon.jpg')
));
foreach($artwork['centerphotos'] as $centerphoto)
{
   echo '<input type="hidden" id="url_' . $centerphoto['id'] . '" name="url_' . $centerphoto['id'] . '" value="' . $centerphoto['url'] . '">';
}
?>
<select name="centerphoto" onchange="document.getElementById('img_preview').src=document.getElementById('url_' + this.options[this.selectedIndex].value).value; document.getElementById('img_preview').style.display='';">
<?php
foreach($artwork['centerphotos'] as $centerphoto)
{
   echo '<option value="' . $centerphoto['id'] . '">' . $centerphoto['name'] . '</option>';
}
?>
</select>
<img id="img_preview" name="img_preview" style="display:none">

Open in new window

0
 
LVL 13

Accepted Solution

by:
MasonWolf earned 450 total points
ID: 22675466
I'm sure you have a way to get the image path names somehow, be it from a database or whatever. You can use a different attribute to store the path property. It's a little unorthodox, but as a quick and dirty fix, it might be just what you want.


<select name="centerphoto" onchange="document.getElementById('img_preview').src=this.options[this.selectedIndex].href; document.getElementById('img_preview').style.display='';">
 
<?php
foreach($artwork['centerphotos'] AS $centerphoto)
{
   echo <<<HTMLOUT
<option value="$centerphoto[id]" href="$centerphoto[path]">$centerphoto[name]</option>
HTMLOUT;
}
?>
</select>
<img id='img_preview' style='display:none'>

Open in new window

0
 

Author Comment

by:Russelauto
ID: 22675519
Hey fcardinaux, I apologize if my level of understanding isn't good enough to fix any small issues.. the result from your code was ' . $centerphoto['id'] or something like that, appeared in the dropdown with no image.

Mason,
Your example almost worked. The dropdown was populated correctly and I got a red x for the image. The image referenced the url like so...  http://mywebsite.com/home/jaenguar/sig_images/army/centerphotos/acu.png
I'm trying to reference matching thumbnails in the images/army/centerphotos/ directory. I just need to figure out how to correct the path but I think you're solution works...
0
 

Author Comment

by:Russelauto
ID: 22675641
So I just need to cut off the first part and make this:

http://mywebsite.com/home/jaenguar/sig_images/army/centerphotos/acu.png

into this:

images/army/centerphotos/acu.png
0
 
LVL 8

Assisted Solution

by:fcardinaux
fcardinaux earned 50 total points
ID: 22675695
Where is the string "acu.png" stored? Or is it the same as $centerphoto['name'] in your script?
0
 

Author Comment

by:Russelauto
ID: 22675714
I tried

id="images/army/centerphotos/$centerphoto[name].png

but it comes back with the name of the drop down option, not the file name. So flag.png gets spit out as US%20Flag

The images are stored in a database. The true path can't be referenced throug a web server. Only the webserver can reference the files (theyre protected) but there are matching thumbnails in the images directory accessible from a url.
0
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22675715
If "acu.png" is stored in $centerphoto['name'], do this:

<?php
$artwork = array('centerphotos' => array(
    array('id' => 1, 'name' => 'acu.png'), 
    array('id' => 2, 'name' => 'other_image.png')));
foreach($artwork['centerphotos'] as $centerphoto)
{
   echo '<input type="hidden" id="image_' . $centerphoto['id'] . '" name="image_' . $centerphoto['id'] . '" value="' . $centerphoto['name'] . '">';
}
?>
<select name="centerphoto" onchange="document.getElementById('img_preview').src='images/army/centerphotos/' + document.getElementById('image_' + this.options[this.selectedIndex].value).value; document.getElementById('img_preview').style.display='';">
<option value="">please select the image</option>
<?php
foreach($artwork['centerphotos'] as $centerphoto)
{
   echo '<option value="' . $centerphoto['id'] . '">' . $centerphoto['name'] . '</option>';
}
?>
</select>
<img id="img_preview" name="img_preview" style="display:none">

Open in new window

0
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22675735
Oops, we posted our comments at the same time...

You write "The images are stored in a database". You probably mean the file names. In this case, why can't your web server read them in the database?
0
 

Author Comment

by:Russelauto
ID: 22675780
Ok I got it. The truth is Mason code worked. It was easier for me to dicipher it. However, you pointed me to look at the database and from there I filled in the blanks..

This works:
<select name="centerphoto" onchange="document.getElementById('img_preview').src=this.options[this.selectedIndex].id; document.getElementById('img_preview').style.display='';">
 images/army/centerphotos
<?php
foreach($artwork['centerphotos'] AS $centerphoto)
{
   echo <<<HTMLOUT
<option value="$centerphoto[id]" id="$centerphoto[thumb]">$centerphoto[name]</option>
HTMLOUT;
}
?>
</select>

In fairness, it's Mason's solution so I owe him the majority of the points.

I'm going to start a new question, because the default image doesn't appear. I want to fix that..
0
 
LVL 8

Expert Comment

by:fcardinaux
ID: 22675892
You wrote "In fairness, it's Mason's solution so I owe him the majority of the points."

Giving me only 10% of the points is clearly unfair, on the contrary. My comment posted 1 hour before Mason's accepted solution did work with less changes. As I wrote, I had tested it. Furthermore, the HREF attribute used by Mason in an OPTION element is not standard-compliant: http://www.w3.org/TR/html401/interact/forms.html#h-17.6.

Not to speak of the time spent asking you where or how to find the image path. When an expert makes true and efficient efforts to answer a question, he deserves a fair part of the points.
0
 

Author Comment

by:Russelauto
ID: 22683112
Hey fcardinaux, I appreciate your answers, but the end result was Mason's solution worked. I explained the results I got when using your code. Again, I aplogize if my lack of knowlege on the subject prevented me from adapting your code to make it work with my scenario. I'll try to be more descriptive in the future.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question