Solved

Using index [i] in cfloop

Posted on 2009-05-06
9
449 Views
Last Modified: 2013-12-24
Hello experts.
On this page:
http://www.experts-exchange.com/Software/Server_Software/Web_Servers/ColdFusion/Q_24380974.html
 i did ask for help to replace the number 1 i have with index [i] and use cfloop.
But because i did not get an answer i will try to separate the question in subquestions.
The getDBimagequery will be:
<cfset numImages = 8>
<cfloop index="i" from="1" to="#numImages#">
<cfquery name="getDBimage#i#" datasource="#request.dsn#">
SELECT img_Text
FROM dbo.All_Images
WHERE img_ID = <cfqueryparam value="#i#" cfsqltype="cf_sql_numeric">
AND Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric">
</cfquery>
</cfloop>
But for the query outputs :
<cffile action="delete" file="#thispath#/#getDBimage1.img_Text#">

The updatequery and serverfile#i#:
<CFSET "ServerFile#i#" = CFFILE.SERVERFILE>
             <cfquery name="updateimg_#i#" datasource="#request.dsn#">
             Update dbo.All_images
             SET img_text = '#ServerFile[#i#]#'
             WHERE Artimages_ID = <cfqueryparam value="350" cfsqltype="cf_sql_numeric">
             AND  img_ID = <cfqueryparam value="1" cfsqltype="cf_sql_numeric">
             </cfquery>
The insert query:
<cfquery name="imageupload" datasource="#request.dsn#">
            Insert into All_images (Artimages_ID,img_ID,img_text)
            values (#Url.art_id#, 1,'#ServerFile1#')
            </cfquery>

i need a help.


0
Comment
Question by:Panos
  • 5
  • 4
9 Comments
 
LVL 17

Accepted Solution

by:
anandkp earned 500 total points
Comment Utility
I am not sure what you are trying to do ... but i'll give it a try ... & see if it helps you ...

your existing code is fine (i think)

<cfset numImages = 8>
<cfloop index="i" from="1" to="#numImages#">
<cfquery name="getDBimage#i#" datasource="#request.dsn#">
SELECT img_Text
FROM dbo.All_Images
WHERE img_ID = <cfqueryparam value="#i#" cfsqltype="cf_sql_numeric">
AND Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric">
</cfquery>
</cfloop>

>> But for the query outputs :

i think u need to loop all over again ...
<cfloop index="i" from="1" to="#numImages#">
     <cffile action="delete" file="#thispath#/#Evaluate('getDBimage#1#.img_Text')#"> <!--- should this be file upload instead of file delete ? --->
     <CFSET "ServerFile#i#" = CFFILE.SERVERFILE>
</cfloop>

similarly ... u'll need to use evaluate again in the subsequent code ....

>> The updatequery
             <cfquery datasource="#request.dsn#">
             Update dbo.All_images
             SET img_text = '#Evaluate("ServerFile#i#")#'
             WHERE Artimages_ID = <cfqueryparam value="350" cfsqltype="cf_sql_numeric">
             AND  img_ID = <cfqueryparam value="1" cfsqltype="cf_sql_numeric">
             </cfquery>


>> The insert query:
<cfquery name="imageupload" datasource="#request.dsn#">
            Insert into All_images (Artimages_ID,img_ID,img_text)
            values (#Url.art_id#, 1,'#Evaluate("ServerFile#i#")#')
</cfquery>

>> i need a help

does this help ?
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Hi anandkp:
In the link i posted ihave the whole code.
In my page i wrote this code 8 times.replacing each time the number 1 with 2,3,......
so I'm asking how to use the cfloop.
I want (if it is correct) to start the loop before the cffile upload function and ending before :<cfif isdefined ("Deletimg_1")>

(I use first delete to delete the image that is allready in image folder.)

0
 
LVL 17

Expert Comment

by:anandkp
Comment Utility
yes, i made some changes to ur code to enable u to use cfloop ...
notice the use of Evaluate() in my reply above ... that should help ...
kindly check & let me know if it helps ...
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Ok
Here is the whole code.I have no errors but i can't upload any image
<cfparam name="Url.Art_ID" default="350">

<cfset numImages = 8>

<cfloop index="i" from="1" to="#numImages#">

<cfquery name="getDBimage#i#" datasource="#request.dsn#">

SELECT img_Text

FROM dbo.All_Images

WHERE img_ID = <cfqueryparam value="#i#" cfsqltype="cf_sql_numeric">

AND Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric">

</cfquery>

</cfloop>
 

<cfloop index="i" from="1" to="#numImages#">

<cfparam name="SERVERFILE#i#" default="">

</cfloop>

<cfset thumbWidth = 80>

   <cfset thumbHeight = 60>

 

   <cfset newWidth = 300>

   <cfset newHeight = 225>

   

   <cfset bigWidth = 640>

   <cfset bigHeight = 480>

   <!--- expandpath() gets the current

         drive and path of the file we are running --->

  <CFSET thispath = expandpath("./images")>       

<cfif isdefined ("uploadf#i#") AND CGI.REQUEST_METHOD IS "POST">

    <cfif structKeyExists(form,"foto"&#i#) and len(form.foto&#i#)>

                

  <!--- uploads a file to our website --->

      <cftry>

     <CFFILE action="upload" filefield="foto#i#" destination="#thispath#" nameconflict="MAKEUNIQUE" accept="image/gif, image/jpg, image/jpeg">

         <cfcatch type="any">

         <cfset   errors = errors & "Image #i# is not an image file!<br />">

         </cfcatch>

        </cftry>

      <!--- check the image size --->

    <cfif errors EQ "">

        <cfif cffile.Filesize GT 500 * 1024>

          <cffile  action="delete" file="#thispath#/#cffile.serverfile#">

          <cfset errors = errors & "Image #i# is bigger than alowed!<br />">

        <cfelse>

    

     <!--- check if there is allready img_1 then delete the file to replace --->

          <cfif  #Evaluate('getDBimage#1#.Recordcount')# NEQ 0>

         

           <cffile action="delete" file="#thispath#/#Evaluate('getDBimage#1#.img_Text')#">

           <cffile action="delete" file="#thispath#/thumbs/#Evaluate('getDBimage#1#.img_Text')#">

           <cffile action="delete" file="#thispath#/bigsize/#Evaluate('getDBimage#1#.img_Text')#">

          </cfif>

     <!--- end --->

     <!--- resize the file --->

             <cfimage name="uploadedImage"

             source="#thispath#/#cffile.serverFile#" >

             <cfimage action="resize"

             height="#newHeight#"

             width="#newWidth#"

             source="#uploadedImage#"

             destination="#thispath#/#cffile.serverFile#"

             overwrite="true"/> 

     

             <cfimage action="resize"

             height="#thumbHeight#"

             width="#thumbWidth#"

             source="#uploadedImage#"

             destination="#thispath#/thumbs/#cffile.serverFile#"

             overwrite="true"/>

    

             <cfimage action="resize"

             height="#bigHeight#"

             width="#bigWidth#"

             source="#uploadedImage#"

             destination="#thispath#/bigsize/#cffile.serverFile#"

             overwrite="true"/>

             
 

    <!--- end --->

          <CFSET "ServerFile#i#"  = CFFILE.SERVERFILE>

   <!--- If there is allready img1 in DB then replace it --->

          <cfif #Evaluate('getDBimage#1#.Recordcount')# NEQ 0>

             <cfquery name="updateimg_1" datasource="#request.dsn#">

             Update dbo.All_images

             SET img_text = '#Evaluate("ServerFile#i#")#'

             WHERE Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric"> 

             AND  img_ID = <cfqueryparam value="#i#" cfsqltype="cf_sql_numeric">

             </cfquery>

   <!--- end --->

         <cfelse>

   <!--- If there is isn't img1 in Db then insert ---> 

            <cfquery name="imageupload" datasource="#request.dsn#">

            Insert into All_images (Artimages_ID,img_ID,img_text)

            values (#Url.art_id#, #i#,'#Evaluate("ServerFile#i#")#')

            </cfquery>

<!--- end --->

        </cfif>

     </cfif>

  </cfif>

  </cfif>

  </cfif>

Open in new window

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

 
LVL 17

Expert Comment

by:anandkp
Comment Utility
do u have rights to upload on server ?
i mean if u do not use loop ... are u able to upload images properly ?
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Yes.
Without cfloop i have no problem.
Here is the form and the output query:

<cfloop index="i" from="1" to="#numImages#" >       

<cfquery name="getimage#i#" datasource="#request.dsn#">

SELECT img_Text

FROM dbo.All_Images

WHERE img_ID = <cfqueryparam value="#i#" cfsqltype="cf_sql_numeric"> 

AND Artimages_ID = <cfqueryparam value="#URL.Art_ID#" cfsqltype="cf_sql_numeric"> 

</cfquery>

</cfloop>

<cfloop index="i" from="1" to="#numImages#" > 

<cfif #Evaluate('getimage#i#.img_Text')# EQ "">

 <cfset 'image#i#' = 'images_1.jpg'>

 <cfelse>

 <cfset 'image#i#' = #Evaluate('getimage#i#.img_Text')#>

 </cfif>

 </cfloop>
 

<form action="#CGI.SCRIPT_NAME#" method="post" enctype="multipart/form-data" name="WADAUpdateForm1" id="WADAUpdateForm1">

<table width="500">

  <tr>

    <td><input name="imageField1" type="image" class="imageFieldinsert" id="imageField" src="images/thumbs/<cfoutput>#image1#</cfoutput>" width="80" height="60" /><br />

    <input name="foto1" class="textfieldfoto"  id="foto1"type="file"  /><br />

    <input name="uploadf1" type="submit"  id="uploadf1"   value="upload" width="60"  height="30" />

    <input name="Deletimg_1" type="submit"  id="Deletimg_1" value="Delete"  <cfif getimage1.img_Text EQ "">disabled="disabled" </cfif> /></td>

    <td><input name="imageField2" type="image" class="imageFieldinsert" id="imageField2" src="images/thumbs/<cfoutput>#image2#</cfoutput>" width="80" height="60" /><br />

    <input name="foto2" class="textfieldfoto"  id="foto2"type="file"  /><br />

    <input name="uploadf2" type="submit"  id="uploadf2"   value="upload" width="60"  height="30" />

    <input name="Deletimg_2" type="submit"  id="Deletimg_2" value="Delete"  <cfif getimage2.img_Text EQ "">disabled="disabled" </cfif> /></td>

  </tr>

  <tr>

    <td><input name="imageField3" type="image" class="imageFieldinsert" id="imageField" src="images/thumbs/<cfoutput>#image3#</cfoutput>" width="80" height="60" /><br />

    <input name="foto3" class="textfieldfoto"  id="foto3"type="file"  /><br />

    <input name="uploadf3" type="submit"  id="uploadf3"   value="upload" width="60"  height="30" />

    <input name="Deletimg_3" type="submit"  id="Deletimg_3" value="Delete"  <cfif getimage3.img_Text EQ "">disabled="disabled" </cfif> /></td>

    <td><input name="imageField4" type="image" class="imageFieldinsert" id="imageField4" src="images/thumbs/<cfoutput>#image4#</cfoutput>" width="80" height="60" /><br />

    <input name="foto4" class="textfieldfoto"  id="foto4"type="file"  /><br />

    <input name="uploadf4" type="submit"  id="uploadf4"   value="upload" width="60"  height="30" />

    <input name="Deletimg_4" type="submit"  id="Deletimg_4" value="Delete"  <cfif getimage4.img_Text EQ "">disabled="disabled" </cfif> /></td>

  </tr>

</table>

</form>

Open in new window

0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Hi again anandkp:
Your code is working
I missed the cfloop before the <cfif isdefined ("uploadf#i#") ......and the </cfllop> end tag
0
 
LVL 2

Author Closing Comment

by:Panos
Comment Utility
Thank you again for your help.
You can post the solution to this question too to get the points:
http://www.experts-exchange.com/Software/Server_Software/Web_Servers/ColdFusion/Q_24380974.html
regards
Panos
0
 
LVL 17

Expert Comment

by:anandkp
Comment Utility
Glad I could help ... :)
Cheers :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

743 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