I'm trying to teach myself cold fusion, javascript, and mysql. I created a page where user can click on an image and then a pop up window will show the full image. My code compiles, but it always displays the first image from my image database. I spent a lot of time on this but couldn't figure out what I'm missing. Can someone give me a hint on how to fix this? Thanks in advance for your help!
File 1: full_article_view.cfm
<!--- retrieve the full article as well as its images --->
<CFQUERY NAME="myQuery1" Datasource="mydb" >
SELECT articles.article_ID, articles.article_title, articles.article_author,
articles.article_date, articles.article_content, article_image_mapping.aim_articleID
FROM articles
INNER JOIN article_image_mapping ON articles.article_ID = article_image_mapping.aim_articleID
WHERE articles.article_ID = #URL.ID#
GROUP BY article_image_mapping.aim_articleID
</CFQUERY>
<CFQUERY NAME="myQuery2" Datasource="mydb" >
SELECT images.image_ID, images.image_thumbpath, images.image_fullpath, article_image_mapping.aim_articleID, article_image_mapping.aim_imageID
FROM images
INNER JOIN article_image_mapping ON images.image_ID = article_image_mapping.aim_imageID
WHERE article_image_mapping.aim_articleID = #URL.ID#
</CFQUERY>
<!DOCTYPE html>
<html>
<head>
<title>Learning</title>
<meta charset="UTF-8">
<script Language="JavaScript">
function popup()
{
settings="toolbar=yes,location=yes,directories=yes,"+
"status=no,menubar=no,scrollbars=yes,"+
"resizable=yes";
MyNewWindow = window.open("full_img.cfm?toshow=images.image_ID");
}
</script>
</head>
<body>
<!--- Page Title --->
<h3>Full Article View</h3>
<!--- Page Content --->
<div align="left">
<!--- Display article title, author, date, and full content --->
<cfoutput query="myQuery1">
<b>#ucase(myquery1.article_title)#</b>
<hr>
<p style="color:##848181; font-size:12px">#myquery1.article_author# :: #myquery1.article_date#</p>
#myquery1.article_content#<br/>
</cfoutput>
<br>
<!--- Display images associated with article--->
<cfoutput query= "myQuery2">
<a href="" onClick="popup();">
<img src="#myquery2.image_thumbpath#" alt="image thumbnail">
</a>
</cfoutput>
</div>
</body>
</html>
File 2: full_img.cfm
<CFQUERY NAME="myQuery3" Datasource="mydb" >
SELECT image_ID, image_thumbpath, image_fullpath
FROM images
WHERE image_ID = #URL.toshow#
</CFQUERY>
<cfoutput>
<img src="#myquery3.image_fullpath#">
</cfoutput>
In your popup() function you are referncing images.image_ID. This will come over as "images.image_ID", and not translate to the id.
You could change your popup to pass in the ID that you want.
<script Language="JavaScript">
function popup(id)
{
settings="toolbar=yes,location=yes,directories=yes,"+
"status=no,menubar=no,scrollbars=yes,"+
"resizable=yes";
MyNewWindow = window.open("full_img.cfm?toshow=" + id);
}
</script>
Then in your HTML/CFML code
<!--- Display images associated with article--->
<cfoutput query= "myQuery2">
<a href="" onClick="popup(#myQuery2.image_ID#);">
<img src="#myquery2.image_thumbpath#" alt="image thumbnail">
</a>
</cfoutput>
I know you are teaching yourself ColdFusion. I'd recommend you use the cfqueryparam tag in your queries to protect against SQL attacks. You would need to use it around your #URL.ID# in your queries.
So this:
...WHERE articles.article_ID = #URL.ID#
would be this:
...WHERE articles.article_ID = <cfqueryparam value = "#URL.ID#" cfsqltype = "cf_sql_integer">
Hope that helps some.