Solved

coldfusion if then statement

Posted on 2009-05-10
10
845 Views
Last Modified: 2013-12-24
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
Comment
Question by:smfmetro10
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 39

Expert Comment

by:gdemaria
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

by:smfmetro10
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">
 

Please upload an image: <cfinput type="file" name="image">
 

<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> 

<div id="header"><img src="images/header.gif" width="870" height="111" alt="Federico Beauty Institute" /></div>

<!---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. --->

    <cffile action="upload" fileField="image" destination="#thisDir#" result="fileUpload"

        nameconflict="overwrite">

    <!--- Determine whether the image file is saved. --->

    <cfif fileUpload.fileWasSaved>

    <!--- Determine whether the saved file is a valid image file. --->

        <cfif IsImageFile("#fileUpload.serverfile#")>

    <!--- Read the image file into a variable called myImage. --->

            <cfimage action="read" source="#fileUpload.serverfile#" name="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"

                    file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">

                <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 & "\" &

                    fileUpload.serverFilename & "." &

                    fileUpload.serverFileExt>

                  

                <!--- 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"

                file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">

            <cfoutput>

            <p>

            The file you uploaded, #fileUpload.clientFile#, was not a valid image.

            </p>

            </cfoutput>

        </cfif>

    </cfif>

</cfif>
 
 
 

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

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

<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>

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

<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>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
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

by:azadisaryev
azadisaryev earned 500 total points
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))>
.... your current code here
<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>

Azadi
0
 
LVL 27

Expert Comment

by:azadisaryev
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...

Azadi
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:vonRogue
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

by:gdemaria
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

by:smfmetro10
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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<body>

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

<cfquery name="qviewimage" datasource="mssalons">

SELECT s_Image FROM uploadtest

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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>
 

<body>

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

SELECT s_Image FROM

uploadtest

</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. --->

    <cffile action="upload" fileField="image" destination="#thisDir#" result="fileUpload"

        nameconflict="overwrite">

    <!--- Determine whether the image file is saved. --->

    <cfif fileUpload.fileWasSaved>

    <!--- Determine whether the saved file is a valid image file. --->

        <cfif IsImageFile("#fileUpload.serverfile#")>

    <!--- Read the image file into a variable called myImage. --->

            <cfimage action="read" source="#fileUpload.serverfile#" name="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"

                    file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">

                <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 & "\" &

                    fileUpload.serverFilename & "." &

                    fileUpload.serverFileExt>

                  

                <!--- 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"

                file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">

            <cfoutput>

            <p>

            The file you uploaded, #fileUpload.clientFile#, was not a valid image.

            </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">

   UPDATE uploadtest

   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>

Open in new window

0
 
LVL 27

Accepted Solution

by:
azadisaryev earned 500 total points
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...

Azadi
<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 

  FROM uploadtest

  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))>
 

      <cffile action="upload" fileField="image" destination="#thisDir#" result="fileUpload" nameconflict="overwrite">
 

      <cfif fileUpload.fileWasSaved>
 

        <cfif IsImageFile(fileUpload.serverDirectory & "/" & fileUpload.serverfile)>
 

        <cfimage action="read" source="#fileUpload.serverDirectory#/#fileUpload.serverfile#" name="myImage">
 

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

          <cffile action="delete" file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">

          <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")>

          <cfset newImageName = fileUpload.serverDirectory & "/" & fileUpload.serverFilename & "_thumbnail." & fileUpload.serverFileExt>

          <cfimage source="#myImage#" action="write" destination="#newImageName#" overwrite="yes">

          <cfquery name="qupdate" datasource="mssalons">

          UPDATE uploadtest

          SET s_Image =<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#fileUpload.serverDirectory & '/' & fileUpload.serverFile#">

          WHERE PKey =<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#url.PKey#">

          </cfquery>

          <cfoutput>

            <p><img src="#getFileFromPath(newImageName)#"></p>

          </cfoutput>
 

        </cfif>
 

      <cfelse>
 

        <cffile action="delete" file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">

        <cfoutput>

          <p>The file you uploaded, #fileUpload.clientFile#, was not a valid image.</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>
 

  </cfif>
 

</cfif>

Open in new window

0
 

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

706 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

14 Experts available now in Live!

Get 1:1 Help Now