7 Replies Latest reply: Apr 4, 2008 1:29 PM by Newsgroup_User RSS

    Initializing an array

    ayuso_15 Community Member
      I have an array that needs to be initialized 1096 times, I don't want to use a cfloop because I am looking for a better performance. So I use a query that brings all the records that have been initialized, the problem is if some of them hasn't been initialized and I use some conditionals as CFIF the application will throw an error because the specified position into the array doesn't exist yet.

      Here's my code.

      <cfquery name="lista_espacios" datasource="#Application.BD#">
      SELECT ISNULL(id_espacio,0) as id_espacio, ruta, url, alt FROM ESPACIOS
      ORDER BY id_espacio
      </cfquery>
      <cfloop query="lista_espacios">
      <cfset lista[lista_espacios.id_espacio][1] = lista_espacios.id_espacio>
      <cfset lista[lista_espacios.id_espacio][2] = lista_espacios.ruta>
      <cfset lista[lista_espacios.id_espacio][3] = lista_espacios.url>
      <cfset lista[lista_espacios.id_espacio][4] = lista_espacios.alt>
      </cfloop>
      <cfset cont=1>
      <cfif #lista[cont][1]# EQ #cont# ><a href="#lista[cont][3]#" onclick="abrirURL(#lista[cont][1]#);" target="_blank"><img style="border:0px" width="13" height="13" src="#lista[cont][2]#" title="#lista[cont][4]#" alt="#lista[cont][4]#" />
      </a>
      <cfelse>
      <img width="13" height="13" src="img/bad.jpg" />

      </cfif>
      <cfset cont = #cont#+1>

        • 1. Re: Initializing an array
          Newsgroup_User Community Member
          what kind of weird thing are you trying to do? why do you need that
          array? care to explain a bit more?

          did you know you can access query results with array notation? i.e.
          queryname.columnname[rownumber] or queryname[columnname][rownumber]

          also, your <cfif> statement(s) is not in any cfloop or cfoutput, as far
          as i can see...



          Azadi Saryev
          Sabai-dee.com
          http://www.sabai-dee.com/
          • 2. Re: Initializing an array
            Dan Bracuk Community Member
            Start with your database design. Is id_espacio part of the primary key for the espacios table?

            Next, your query. Why are you pulling every single record?

            Next, if your db supports it, you can probably use a case construct in your query to replace the loop and if/else logic afterwards. Then it would be a simple cfoutput, with no arrays to worry about.
            • 3. Re: Initializing an array
              ayuso_15 Community Member
              Ok, the thing is I need to access the database to pull out an image in case the image doesn't exist then it will put a default image.
              But at the beginning there will be some Id_espacios that will be not created yet (Id_espacios will be the primary key),
              Anyway the problem is when I go to the cfloop query... It will only loop the record set that was pull out of the database that's the main reason I am using an array so that way you can initialize your 1096 registries.
              • 4. Re: Initializing an array
                Newsgroup_User Community Member
                If I understand your requirements you have a database that has some
                records, but not necessarily all 1096 records at this time. You want to
                pull out these records, but have a full set of 1096 records even if some
                of them do not yet exist in the dataset.

                There are database tricks that can be used to create a record set like
                this. Hopefully somebody will chime in with the details. This would
                greatly simplify your processing after that.
                • 5. Re: Initializing an array
                  Dan Bracuk Community Member
                  quote:

                  Originally posted by: Newsgroup User
                  If I understand your requirements you have a database that has some
                  records, but not necessarily all 1096 records at this time. You want to
                  pull out these records, but have a full set of 1096 records even if some
                  of them do not yet exist in the dataset.

                  There are database tricks that can be used to create a record set like
                  this. Hopefully somebody will chime in with the details. This would
                  greatly simplify your processing after that.


                  There is not enough information to know what approach would be most appropriate.

                  What is the database type? Also, do the existing records have sequential values for idespacio, starting with 1?
                  • 6. Re: Initializing an array
                    ayuso_15 Community Member
                    Thank you Dan,

                    I have fix this initializing all the records in the database and I am using a cfloop to pull out all the records, anyway I will always need to loop 1096 times. At least to compare weather the image exist or not.
                    • 7. Re: Initializing an array
                      Newsgroup_User Community Member
                      ayuso_15 wrote:
                      > Thank you Dan,
                      >
                      > I have fix this initializing all the records in the database and I am using a
                      > cfloop to pull out all the records, anyway I will always need to loop 1096
                      > times. At least to compare weather the image exist or not.
                      >


                      Well you can always use a for next loop.

                      <cfoutput>
                      <cfloop from="1" to="1096" index="i">
                      #i#<br/>
                      </cfloop>