SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 9th, 2004, 07:15 PM
Damiano3 Damiano3 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 4 Damiano3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question PLEASE HELP!!! Problem with extended SELECT statement and END IF's.

Hello.

I'm trying to search on 3 database fields, but am trying to cater for every
logical occurence of people leaving one or more search fields blank.

This is the only way (very long winded) that I can see of doing this, and it still doesn't work.

Could someone please tell me a) how I can get this script working
b) if there is an easier and shorter way to do this.

Regards Damiano3


sqlString = "SELECT image_id, image_thumbnail, image_name, image_keywords "
sqlString = sqlString & "FROM images WHERE ("

IF searchFor <> "" AND iref <> "" AND itype <> "" THEN
sqlString = sqlString & "image_name LIKE '%" & searchFor & "%' OR "
sqlString = sqlString & "image_keywords LIKE '%" & searchFor & "%' AND "
sqlString = sqlString & "image_id = " & iref & " AND "
sqlString = sqlString & "image_type LIKE '%" & itype & "%' "

ELSEIF searchFor <> "" AND iref <> "" THEN
sqlString = sqlString & "image_name LIKE '%" & searchFor & "%' OR "
sqlString = sqlString & "image_keywords LIKE '%" & searchFor & "%' AND "
sqlString = sqlString & "image_id = " & iref & " "

ELSEIF searchFor <> "" AND itype <> "" THEN
sqlString = sqlString & "image_name LIKE '%" & searchFor & "%' OR "
sqlString = sqlString & "image_keywords LIKE '%" & searchFor & "%' AND "
sqlString = sqlString & "image_type LIKE '%" & itype & "%' "

ELSEIF searchFor <> "" THEN
sqlString = sqlString & "image_name LIKE '%" & searchFor & "%' OR "
sqlString = sqlString & "image_keywords LIKE '%" & searchFor & "%' "

ELSEIF iref <> "" AND itype <> "" THEN
sqlString = sqlString & "image_id = " & iref & " AND "
sqlString = sqlString & "image_type LIKE '%" & itype & "%' "

ELSEIF iref <> "" THEN
sqlString = sqlString & "image_id = " & iref & " "

ELSEIF itype <> "" THEN
sqlString = sqlString & "image_type LIKE '%" & itype & "%' "

ELSE
%>

YOU MUST ENTER SOMETHING

<%

END IF

sqlString = sqlString & ") ORDER BY image_id;"

Reply With Quote
  #2  
Old August 9th, 2004, 07:54 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
Example:
Code:
strSql = "SELECT image_id, image_thumbnail, image_name, image_keywords FROM images WHERE "

If Len(SearchFor) <> 0 Then
   strWhere = "ImageName LIKE '%" & SearchFor & "%'"
   blnFirstParam = True
End If

If Len(iRef) <> 0 Then
   If blnFirstParam = True Then
      strWhere = " AND ImageId = " & iRef & "
   Else
      strWhere = "ImageId = " & iRef & "
      blnFirstParam = True
   End If
End If

If Len(iType) <> 0 Then
   If blnFirstParam = True Then
      strWhere = " AND ImageType LIKE '%" & iType & "%'"
   Else
      strWhere = "ImageType LIKE '%" & iType & "%'"
      blnFirstParam = True
   End If
End If

etc...

SqlString = strSql & strWhere

Reply With Quote
  #3  
Old August 9th, 2004, 08:24 PM
Damiano3 Damiano3 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 4 Damiano3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

cheers for the quick response, checked over your code and updated mine.

Its much better thanks, but only works if at least the searchFor field is full.

What next ????


Here's my updated code.

Thanks again Damiano3



strSql = "SELECT image_id, image_thumbnail, image_name, image_keywords FROM images WHERE "

If Len(searchFor) <> 0 Then
strWhere = "image_name LIKE '%" & searchFor & "%'"
blnFirstParam = True
End If

If Len(iref) <> 0 Then
If blnFirstParam = True Then
strWhere = strWhere & " AND image_id = " & iref & ""
Else
strWhere = "image_id = " & iref & ""
blnFirstParam = True
End If
End If

If Len(itype) <> 0 Then
If blnFirstParam = True Then
strWhere = strWhere & " AND image_type LIKE '%" & itype & "%'"
Else
strWhere = "image_type LIKE '%" & itype & "%'"
blnFirstParam = True
End If
End If

SqlString = strSql & strWhere

Reply With Quote
  #4  
Old August 10th, 2004, 09:07 AM
Damiano3 Damiano3 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 4 Damiano3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post Still not quite working

Update:

Changed the AND's to OR's. but still get the same problem.

Any more help would be greatly appreciated.

Damiano3

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > PLEASE HELP!!! Problem with extended SELECT statement and END IF's.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT