Skip navigation
ayuso_15
Currently Being Moderated

Initializing an array

Apr 4, 2008 8:39 AM

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>

 
Replies
  • Currently Being Moderated
    Apr 4, 2008 9:06 AM   in reply to ayuso_15
    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/
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2008 9:14 AM   in reply to ayuso_15
    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.
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2008 10:09 AM   in reply to Dan Bracuk
    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.
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2008 11:31 AM   in reply to Newsgroup_User
    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?
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2008 1:29 PM   in reply to Dan Bracuk
    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>
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points