grab first 4 characters of string

Hi.  Ok, so I have displayed:

[Checkbox] Team 1.jpg (id#: 1)
[Checkbox] Individual 1.jpg (id#: 2)
[Checkbox] Action 1.jpg (id#: 3)

The user chooses 2 of the following 3 selections and clicks Next to go to the next page.  What gets passed to the next page is the id#.  BUT I want him to choose only the 2 files which start with "team" and "indi".  If they don't choose those options, it goes back with an error.  So let's say he chooses id #1 and id #3, what would get passed to the next page is 1,3.  I figured that I would then query for the name of the image, then grab the first 4 characters of the name.  If the result is not "team" or "indi", then throw back the error.  Not sure how to do this though.  Any ideas?
COwebmasterAsked:
Who is Participating?
 
Renante EnteraSenior PHP DeveloperCommented:
Hi COwebmaster!

Assuming that your database is MSSQL.  So, I think this is what you are looking for :
--------------
NEXT PAGE
--------------
<cfquery name="qGetRecord" datasource="#REQUEST.Datasource#">
  SELECT imageId, imageName
  FROM tblImage
  WHERE imageId IN ('#FORM.checkbox#')
  AND
  (
    LEFT(imageName, 4) = 'team' OR LEFT(imageName, 4) = 'indi'
  )
</cfquery>

<cfif qGetRecord.recordcount LT ListLen(Form.checkbox)>
  <cfoutput>
    <b>ERROR FOUND</b><br>
    Be sure that the file you have selected starts with 'team' or 'indi'<br>
    <input type="button" name="backButton" value="Back" onclick="history.back()">
  </cfoutput>
</cfif>

*Note : On the query, the LEFT() function is a database function not coldfusion.  Then for another workaround, you can also use any of these :
  a.  SUBSTRING(imageName, 1, 4)
  b.  CAST(imagename as varchar(4))

I hope that this helps you.  Just try it.


Goodluck!
eNTRANCE2002 :-)
0
 
COwebmasterAuthor Commented:
Hello eNTRANCE2002!  Let me check it out..
0
 
COwebmasterAuthor Commented:
I'm getting an error saying "Element CHECKBOX is undefined in FORM"
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
COwebmasterAuthor Commented:
Ok, I realized that I wasn't calling out the name of the checkbox.  Anyway, now I have another error:

[Macromedia][SQLServer JDBC Driver][SQLServer]Syntax error converting the varchar value &apos;1855,1856&apos; to a column of data type int.

0
 
Renante EnteraSenior PHP DeveloperCommented:
Hmmm... :-?

Maybe, this part has the problem :
  WHERE imageId IN ('#FORM.checkbox#')

Try to change it with this :
  <!--- Just remove single quote --->
  WHERE imageId IN (#FORM.checkbox#)

I hope that this makes sense.


Regards!
eNTRANCE2002 :-)
0
 
COwebmasterAuthor Commented:
Worked, thanks!
0
 
COwebmasterAuthor Commented:
Final:


<cfquery name="qGetRecord" datasource="#REQUEST.Datasource#">
  SELECT imageId, imageName
  FROM tblImage
  WHERE imageId IN (#checkbox#)
  AND
  (
    LEFT(imageName, 4) = 'team' OR LEFT(imageName, 4) = 'indi'
  )
</cfquery>

<cfif qGetRecord.recordcount LT ListLen(checkbox)>
  <cfoutput>
    <b>ERROR FOUND</b><br>
    Be sure that the file you have selected starts with 'team' or 'indi'<br>
    <input type="button" name="backButton" value="Back" onclick="history.back()">
  </cfoutput>
</cfif>
0
 
Renante EnteraSenior PHP DeveloperCommented:
Hi COwebmaster!

I'm glad that I have helped you.  Hope to help you again.

BTW, thanks for the points and the grade.


Best regards!
eNTRANCE2002 :-0
0
 
COwebmasterAuthor Commented:
Thanks again entrance2002!  I just posted another cfm question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.