Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# coldfusion if then statement

Posted on 2009-05-10
Medium Priority
877 Views
Hi,
I'm having trouble writing a cfif statement (coldfusion newbie) I have a page with a cfimage tag that pulls an image out of a database along with a file upload where the user can upload a new image. If the user leaves the the file blank I want to keep and transfer the image that is already on the screen over to the "preview" page. I just can't seem to make it work.
Thanks for helping!
0
Question by:smfmetro10
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3
• 3
• +1

LVL 39

Expert Comment

ID: 24351073
We could write a CFIF statement, but I don't think it would necessarily be a match for the code you have, so it would be confusing to you.   The best bet is for you to post your code and have us help you directly with what you have as a starting point.

0

Author Comment

ID: 24351123
I have an edit page and a preview page with the following code
Thanks
I have an edit page with the following code:

<cfquery name="listsalon" datasource="mssalons">
SELECT sName, s_Profile, s_Image
FROM salonsabb
WHERE PKey=<cfqueryparam value="#URL.PKey#" cfsqltype="CF_SQL_INTEGER">

</cfquery>

<cfoutput query="listsalon"><h1 style="text-align:center">#sName#</h1><br /><br /><br />

<cfimage action="WRITETOBROWSER" source="#s_Image#">

<cfform action="salon_previewA.cfm?PKey=#PKey#" method="post" name="form" enctype="multipart/form-data" preloader="no">

<cfinput type="hidden" name="hddimage" value="#image#">
<cfinput type="submit" name="submit" value="preview">

</cfform>
</cfoutput>

And a preview page with the following code

<cfquery name="listsalon" datasource="mssalons" result="qresult">

SELECT PKey, sName, s_Image
FROM salonsabb
WHERE PKey=<cfqueryparam value="#URL.PKey#" cfsqltype="CF_SQL_INTEGER">
</cfquery>
<!---Show Picture --->

<cfset thisDir = expandPath(".")>
<!--- Determine whether the form is uploaded with the image. --->
<cfif structKeyExists(form,"image") and len(trim(form.image))>
<!--- Use the cffile tag to upload the image file. --->
nameconflict="overwrite">
<!--- Determine whether the image file is saved. --->
<!--- Determine whether the saved file is a valid image file. --->
<!--- Read the image file into a variable called myImage. --->
<!--- Determine whether the image file exceeds the size limits. --->
<cfif ImageGetHeight(myImage) gt 800 or ImageGetWidth(myImage) gt 800>
<!--- If the file is too large, delete it from the server. --->
<cffile action="delete"
<cfoutput>
<p>
The image you uploaded was too large. It must be less than 800 pixels wide
and 800 pixels high. Your image was #imageGetWidth(myImage)# pixels wide
and #imageGetHeight(myImage)# pixels high.
</p>
</cfoutput>
<!--- If the image is valid and does not exceed the size limits,
create a thumbnail image from the source file that is 75-pixels
square, while maintaining the aspect ratio of the source image.
Use the bilinear interpolation method to improve performance.
--->
<cfelse>

<cfset ImageScaleToFit(myImage,275,275,"bilinear")>
<!--- Specify the new filename as the source filename with
"_thumbnail" appended to it. --->
<cfset newImageName = fileUpload.serverDirectory & "\" &

<!--- Save the thumbnail image to a file with the new filename. --->
<cfimage source="#myImage#" action="write"
destination="#newImageName#" overwrite="yes">
<cfoutput>

<p>
<!--- Display the thumbnail image. --->
<img src="#getFileFromPath(newImageName)#">
</p>

</cfoutput>

</cfif>
<!--- If it is not a valid image file, delete it from the server. --->
<cfelse>
<cffile action="delete"
<cfoutput>
<p>
</p>
</cfoutput>
</cfif>
</cfif>
</cfif>

<cfoutput query="listsalon"><h1 style="text-align:center">#sName#</h1></cfoutput><br /><br /><br />
<cfoutput>
Salon Profile:#txtProfile#<br /><br />
<!---
Hours Open :#lstSunHoursOpen#
--->
</cfoutput>

<cfinput type="hidden" name="hddProfile" value="#txtProfile#">
<cfinput type="hidden" name="hddimage" value="#image#">
<!---
<cfinput type="hidden" name="hddlstSunHoursOpen" value="#lstSunHoursOpen#">
--->
<cfinput type="submit" name="edit" value="edit">
</cfform>
<cfinput type="hidden" name="hddProfile" value="#txtProfile#">
<cfinput type="hidden" name="hddimage" value="#image#">

<cfinput type="hidden" name="hddnnewimagename" value="#newimagename#">

<cfinput type="submit" name="save" value="save">
</cfform>

0

LVL 39

Expert Comment

ID: 24351160
In the first section, the 'edit' page, you have a form within a loop.  That means you many forms on the page with many preview buttons.   Only ONE of these forms will be executed and only ONE image uploaded at a time.  Is that what you were going for?   The user may select many images from multiple forms, but when they click a preview button, only the image of the button pressed will be uploaded.

Just wanted to ensure that is what you were going for on that part..
0

LVL 27

Assisted Solution

ID: 24351194
you seem to have it all there... you are just missing a <cfelse> block to show the existing image if no new image was uploaded:

<cfif structKeyExists(form,"image") and len(trim(form.image))>
<CFELSE>
<cfif len(trim(listsalon.s_Image))>
<cfimage action="WRITETOBROWSER" source="#listsalon.s_Image#">
... any other code to go with existing image ...
<cflese>
...code for case when no image exists yet and no new image has been uploaded...
</cfif>
</cfif>

0

LVL 27

Expert Comment

ID: 24351198
@gdemaria:

i may be wrong, but the way i read his listsalon query, it looks like its WHERE clause actually limits it to just one image...

0

LVL 1

Expert Comment

ID: 24351201
Can you elaborate on exactly what is not working with your current code? Is the issue that the default image isn't being used if the user leaves the upload image field blank? Or are you experiencing any other problems?

Is it safe to assume that the PKey is a UNIQUE primary key, and that only one record will be returned in the query? Otherwise, like gdemaria said, you will have multiple forms displayed on the page.

Also, where is the UPDATE query? Do you want the database to be updated if/when the user uploads a new image?
0

LVL 39

Expert Comment

ID: 24351218
@Azadi, good point.  Assuming PKey is the primary key of the table, absolutely.

@smfmetro10,  Pkey is a terrible name for a primary key column.  When you join multiple tables together, you can easily get lost as to what column belongs to what table.   Also, if you were to put the column in another table as a foreign key then it would be PKey in the other table - which would be extremely confusing or would conflict with the pkey of that table...

Convention is that the primary key of a table could be named after the tablename with _ID at the end (or just ID).
So the Primary Key of SalonSabb would be SalonSabb_ID or  SalonSabbID

0

Author Comment

ID: 24358730
Thanks for all the replies,
I will try to clarify more.  I stripped some un necessary code out so it will be easier to understand.
Right now I have an edit page and a processing page (that goes to a preview page).  When the user goes to the edit page for the first time there will be a default picture that gets pulled in from the database. The problem is that when the user leaves the upload file blank and clicks the  "view" button, an error is thrown:
The source string is blank.
Verify your inputs. The source string is blank.
The error occurred in C:\ColdFusion8\wwwroot\Frederico\picuploadtest1\text_label.cfm: line 16
14 : <cfimage
15 : action="writetobrowser"
16 : source="#s_Image#"
17 : />
18 : </cfoutput>

Then when you go back to edit that page it throws the same error.

and now when the user trys to upload a different image it throws this error:
An exception occurred while trying to read the image.
File C:\ColdFusion8\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\neotmp61045.tmp does not exist.
The error occurred in C:\ColdFusion8\wwwroot\Frederico\picuploadtest1\text_label.cfm: line 16
14 : <cfimage
15 : action="writetobrowser"
16 : source="#s_Image#"
17 : />
18 : </cfoutput>
I would like to upload the picture in a folder on the server along with the path to store in the database
Thanks again for all the help
P.S. Thanks for the "PKey" lesson as well

text_edit page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<body>
<cfquery name="qviewimage" datasource="mssalons">
WHERE PKey=#PKey#
</cfquery>
<cfoutput query="qviewimage">
<cfimage
action="writetobrowser"
source="#s_Image#"
/>
</cfoutput>
<cfinput type="submit" name="submit" value="view site">
<cfinput type="file" name="image">
<cfinput type="hidden" name="hddnPKey" value="#PKey#">
</cfform>
</body>
</html>

text_edit_processing page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<body>
<cfquery name="listsalon" datasource="mssalons">
SELECT s_Image FROM
</cfquery>
<cfoutput query="listsalon">
<cfset thisDir = expandPath(".")>
<!--- Determine whether the form is uploaded with the image. --->
<cfif structKeyExists(form,"image") and len(trim(form.image))>
<!--- Use the cffile tag to upload the image file. --->
nameconflict="overwrite">
<!--- Determine whether the image file is saved. --->
<!--- Determine whether the saved file is a valid image file. --->
<!--- Read the image file into a variable called myImage. --->
<!--- Determine whether the image file exceeds the size limits. --->
<cfif ImageGetHeight(myImage) gt 800 or ImageGetWidth(myImage) gt 800>
<!--- If the file is too large, delete it from the server. --->
<cffile action="delete"
<cfoutput>
<p>
The image you uploaded was too large. It must be less than 800 pixels wide
and 800 pixels high. Your image was #imageGetWidth(myImage)# pixels wide
and #imageGetHeight(myImage)# pixels high.
</p>
</cfoutput>
<!--- If the image is valid and does not exceed the size limits,
create a thumbnail image from the source file that is 75-pixels
square, while maintaining the aspect ratio of the source image.
Use the bilinear interpolation method to improve performance.
--->
<cfelse>

<cfset ImageScaleToFit(myImage,275,275,"bilinear")>
<!--- Specify the new filename as the source filename with
"_thumbnail" appended to it. --->
<cfset newImageName = fileUpload.serverDirectory & "\" &

<!--- Save the thumbnail image to a file with the new filename. --->
<cfimage source="#myImage#" action="write"
destination="#newImageName#" overwrite="yes">
<cfoutput>

<p>
<!--- Display the thumbnail image. --->
<img src="#getFileFromPath(newImageName)#">
</p>

</cfoutput>

</cfif>
<!--- If it is not a valid image file, delete it from the server. --->
<cfelse>
<cffile action="delete"
<cfoutput>
<p>
</p>
</cfoutput>
</cfif>
</cfif>
<CFELSE>
<cfif len(trim(listsalon.s_Image))>
<cfimage action="WRITETOBROWSER" source="#listsalon.s_Image#">
... any other code to go with existing image ...
<cfelse>
...code for case when no image exists yet and no new image has been uploaded...
</cfif>
</cfif>
<cfquery name="qupdate" datasource="mssalons">
SET

s_Image =<cfqueryparam
cfsqltype="CF_SQL_VARCHAR"
value="#Image#">

WHERE PKey =<cfqueryparam
cfsqltype="CF_SQL_VARCHAR"
value="#PKey#">
</cfquery>
</cfoutput>
<cflocation url="text_label.cfm?PKey=#PKey#">
</body>
</html>

0

LVL 27

Accepted Solution

ID: 24361086
1) all your image processing code is inside a <cfoutput query="listsalon"> (which, btw, seems to be missing a WHERE clause!!!) - if that query does not return any records, none of the code inside <cfoutput> will ever execute!

2) you really do NOT need to have your image processing code inside a query output at all. you should probably move that listsalon query into the <cfelse> part of your <cfif structkeyexists...> block - there is no need to run that query if a file has been submitted with the form.

3) your qupdate query uses some variable named #Image# - but your are not setting this variable anywhere, at least not in the code you posted...

4) your qupdate query should probably be inside the <cfif structkeyexists...> block, after the image resizing code - you only need to update a db record if a new file has been uploaded (no need to update anything if no new file has been uploaded, is there?). in this case the qupdate query should use #fileUpload.serverFile# variable (maybe with fileUpload.serverDirectory if you need to store full path to image and not just image filename) instead of #Image# variable.

5) in the code you posted you resize the image and save it with same filename as the original uploaded image: it seems you have forgotten to actually add "_thumbnail" to the newImageName variable value, as you say you do in the comments...

see attached edited code, with some param checking additions...

one note: i could not figure out which of <cfif> statements should result in your <cflocation url="text_label.cfm?PKey=#url.PKey#">... so i did not include it anywhere - just stick it in where it belongs in your code...

<cfparam name="url.PKey" default="">

<cfif NOT len(trim(url.Pkey))>

<cfoutput><p>Invalid salon identifier!</p></cfoutput>
<cfabort>

<cfelse>

<cfquery name="listsalon" datasource="mssalons">
SELECT s_Image
WHERE PKey = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#url.PKey#">
</cfquery>

<cfif NOT listsalon.recordcount>

<cfoutput><p>Invalid salon identifier!</p></cfoutput>
<cfabort>

<cfelse>

<cfset thisDir = expandPath(".")>

<cfif structKeyExists(form,"image") and len(trim(form.image))>

<cfif ImageGetHeight(myImage) gt 800 OR ImageGetWidth(myImage) gt 800>

<cfoutput>
<p>The image you uploaded was too large. It must be less than 800 pixels wide and 800 pixels high. Your image was #imageGetWidth(myImage)# pixels wide and #imageGetHeight(myImage)# pixels high.</p>
</cfoutput>

<cfelse>

<cfset ImageScaleToFit(myImage,275,275,"bilinear")>
<cfimage source="#myImage#" action="write" destination="#newImageName#" overwrite="yes">
<cfquery name="qupdate" datasource="mssalons">
WHERE PKey =<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#url.PKey#">
</cfquery>
<cfoutput>
<p><img src="#getFileFromPath(newImageName)#"></p>
</cfoutput>

</cfif>

<cfelse>

<cfoutput>
</cfoutput>

</cfif>

</cfif>

<cfelse>

<cfif len(trim(listsalon.s_Image))>
<cfimage action="WRITETOBROWSER" source="#listsalon.s_Image#">
... any other code to go with existing image ...
<cfelse>
...code for case when no image exists yet and no new image has been uploaded...
</cfif>

</cfif>

</cfif>

</cfif>

0

Author Closing Comment

ID: 31580006
Thanks for insight. This definetly put me on the right track.
0

## Featured Post

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
###### Suggested Courses
Course of the Month10 days, 1 hour left to enroll