Skip to main content
Inspiring
September 21, 2009
Question

Comparing values in db against values in form

  • September 21, 2009
  • 1 reply
  • 1316 views

Hi,

This should be straight forward, but I'm missing something.

I have a query.  I need to compare the value of each record to the value in a form field.

<cfquery name="getInfo" datasource="#dsn#">
    SELECT      id, text
    FROM         table
    WHERE       (id= 102)
   </cfquery>

There could be 10 items in the table, but what I'm doing (below), displays only the last item in the table.  Any suggestion would be greatly appreciated!

When I do this, I get the last record in the table.

     <cfif #(getInfo.text)# EQ "Reception">
        <input type="Checkbox" name="session_text" value="Reception" checked>Reception
        <cfelse>
        <input type="Checkbox" name="session_text" value="Reception">Reception
       </cfif> 

This topic has been closed for replies.

1 reply

ilssac
Inspiring
September 22, 2009

If you are not inside a <cfquery...> or <cfloop...> block, (and sometimes even inside such blocks) you need to fully qualify your query variable.

I.E.  QueryName.columnName[row] OR queryName["columnName"][row]

jenn1Author
Inspiring
September 22, 2009

Exactly.

<cfquery name = "getInfo" datasource = "#dsn#">

SELECT id,text,

FROM session_lkup

WHERE       (id = 134)

</cfquery>

There are currently 4 rows for id 134.

Bio

Chem

MPS

Physics

When I do the following:

<cfoutput>

<cfif  #getInfo.text# EQ 'Bio'>

      do some thing

      <cfelse>

      do nothing

</cfif>

</cfoutput>

I don't get any results.

However if I wrap it within <cfoutput query...>, the 4 values get repeated for times!

<cfoutput query ="getInfo">

<cfif  #getInfo.text# EQ 'Bio'>

      do some thing

      <cfelse>

      do nothing

</cfif>

</cfoutput>

What am I doing wrong?  This shouldn't be so complicated.  I'm not seeing something....

ilssac
Inspiring
September 22, 2009

You have a record set with four records in it.

If you use a <cfoutput query...> OR <cfloop query...> block, it will loop over each record and execute the code inside the block once per record.

ColdFusion allows one to short cut the reference to record set inside such loop blocks.  It allows one to only reference the column name and the block fills in the query name and the row value.

Outside such a loop block one has to fully qualify the record set.  IF one leaves off the row part of the reference, ColdFusion will just get one row of the record set.

It looks like you want to search all four records and do some action if a value exists in ANY row.  You will either have to adjust your logic to loop over all the records look for a value, then do something if the value was found after the loop.

OR

Use some functions that may make this easier.  valueList() and listFind() are a common combonation.