Skip to main content
April 4, 2008
Question

Initializing an array

  • April 4, 2008
  • 2 replies
  • 850 views
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>

This topic has been closed for replies.

2 replies

Inspiring
April 4, 2008
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.
April 4, 2008
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.
Inspiring
April 4, 2008
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/