Solved

Image previews with dropdown

Posted on 2008-10-08
14
438 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 8

Expert Comment

by:fcardinaux
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 8

Assisted Solution

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

Author Comment

by:Russelauto
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now