Solved

Using index [i] in cfloop

Posted on 2009-05-06
9
462 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
ID: 24312426
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
ID: 24312482
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
ID: 24312561
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
ID: 24312651
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 17

Expert Comment

by:anandkp
ID: 24312763
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
ID: 24312775
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
ID: 24313137
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
ID: 31578363
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
ID: 24347002
Glad I could help ... :)
Cheers :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now