This thread was a big help to me in getting this working for me. I thought I'd contribute some additions that I made that eliminate the need for hard-coding your dropdown boxes.
First, I put the showimage() function in a file called jscript_showimage.js. It is in the includes/modules/pages/product_info/ directory. I modified the function to grab the value from the dropdown, split it on "-", then set values as appropriate. Hopefully the comments in the code explain what's going on.
Code:
function showimage()
{
if (!document.images)
return;
// Pull the currently selected value from the visible dropdown and split it on hyphen
var imagevalueunsplit = document.getElementById("colorselection").value;
var imagevaluearray = imagevalueunsplit.split("-");
// Set the array values to variables. First array value is the identifying value for the dropdown, second is the image name
var imagevalue = imagevaluearray[0];
var imagename = imagevaluearray[1];
// Set the Zen Cart box (the hidden one) to the proper value so that the attribute is passed to the Shopping Cart
document.getElementById("attrib-1").value = imagevalue;
// Set the display image to the proper file
document.images.colorimage.src="images/" + imagename + ".gif";
}
I then pulled a query from the database of the attributes with the 3 fields that I need to build my SELECT. I'm sticking both the numeric value that is used to set the Zen Cart attribute selection (the one we hide with that STYLE there at the top) and the image I want to call. They are separated with a hyphen so the Javascript can split them.
Code:
<style type="text/css">
#attrib-1 {display: none;};
</style>
<!-- this is the table that is used you may need to change size based on your needs -->
<table border="0" cellspacing="0" cellpadding="0" width="234">
<tr>
<td width="134">
<?php
$colorsquery = mysql_query("SELECT options_values_id,attributes_image,products_options_values_name FROM ztest_products_options_values,ztest_products_attributes WHERE ztest_products_options_values.products_options_values_id = ztest_products_attributes.options_values_id AND options_id = 1 ORDER BY `products_options_values_sort_order`,`products_options_values_name`")
or die(mysql_error());
?>
<!--this calls the picture change function-->
<select name="colorselection" onChange="showimage()">
<?php
while ($currentcolor = mysql_fetch_array($colorsquery)) {
echo "<option value='$currentcolor[0]-$currentcolor[1]'>$currentcolor[2]</option>";
}
?>
</select>
</td>
<td width="100" align="center">
<align="center"><img src="images/attributes/black.gif" name="colorimage" style="border-width:1px; border-color:#000000; border-style:solid;">
</td>
</tr>
</table>
I also modified my tpl_module_attributes.php file to not output attribute images under the SELECT. To do this, remove the following code
Code:
<?php
if ($options_attributes_image[$i] != '') {
?>
<?php echo $options_attributes_image[$i]; ?>
<?php
}
?>
<br class="clearBoth" />
What all of this allows me to do is use the built-in Attributes Images function in the Admin tool and not have to hardcode anything. When I add a new color to the attributes, I upload the image to the color_samples/ folder and it's automatically there.
You can see it at work here. Login is guest/guest (may have to enter it a few times the first time). Only Black, Blue, Orange, and Red currently have images associated with them, so the others will show either a Red X or nothing depending on if you use IE or Firefox.
Thanks to all for their help and hopefully this addition helps someone.
Bookmarks