 |
| Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 10th, 2003, 10:19 PM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
document form options value sql
how do i put an sql statement inside if-else statement?? or is that possible?
my aim is to check if the listbox is not selected(options=0) then I will add sql statement ("AND table.field=value")
my code:
<%
sql = "SELECT * FROM ..WHERE table.field ='"& value & "'"
if (document.formname.listboxname.options[0].value==0)
sqlsort = "AND table1.field1 ='"& value1 & "'"
end if
%>
|
|

September 11th, 2003, 04:05 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You want to generate an SQL statements, but if some value is 0 you want to append something more!? (you could probably structure it better than me)...
<%
...
sql = "SELECT * FROM table"
if (document.formname.listboxname.options[0].value == 0)
sql = sql & ", table1 WHERE table.field='" & value & "' AND table1.field1='" & value1 & "'"
else
sql = sql & " WHERE table.field='" & value & "'"
end if
...
%>
Hope it helps!
Jacob.
|
|

September 11th, 2003, 04:14 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
The problem with this code is that document.formname.listboxname.options refers to the client, while the ASP will execute at the server. You'll need Request.QueryString (when your form uses GET) or Request.Form (when your form uses POST) to retrieve the data from the form.
The general syntax is like this:
Request.Form( FieldName)
This will return the value of the HTML field FieldName. In your code, something like this will work:
Code:
<%
If Request.Form("ListBoxName") = "" Then
' Do something with your SQL statement
End If
%>
Request.Form("ListBoxName") will be "" when no selection is made, or when the user has chosen an item with a value of "" (<option value="">Option without a value</option>)
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

September 11th, 2003, 04:24 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ohhh! Sorry. I didn't saw that. Jacob.
|
|

September 11th, 2003, 04:25 AM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi guys. thanks for the reply.
it should be when NOT EQUAL TO ZERO THEN I ADD THE SQL STATEMENT. sorry about that.
|
|

September 11th, 2003, 04:28 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
!= has to be <>
Jacob.
|
|

September 11th, 2003, 04:33 AM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
actualy my aim is to 'search' the database. i have 5 listboxes: user, task, project etc. when i choose user X, all of X's data will be shown. if i choose task Y, all records with task Y will show. If they're not chosen, it will show all. Or if i choose X and Y, only records with X and Y will show. .... there are many variations. 
|
|

September 11th, 2003, 10:18 PM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
anyway, the <'"&value1&"'> part in <sqlsort = "AND table1.field1 ='"& value1 & "'"> part doesn't work. it shows no data. only when i put it equal to some actual value then it works.
|
|

September 12th, 2003, 01:42 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Not sure what you're saying here. It makes sense you need to set the value equal yo some value in the database, otherwise you won't get a record, no?
Can you post a bit more of your code and explain what you're trying to do?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

September 12th, 2003, 02:37 AM
|
|
Authorized User
|
|
Join Date: Sep 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i have 5 dynamic listboxes- user,task,country etc. i also have a submit button.
eg of user: John, sally
eg of tasks: Admin, R&D
eg of country: SG, US
i meant that <sqlsort = "AND table1.field1 ='"& value1 & "'"> doesn't work.
but sqlsort = "AND table1.field1 ='John'"> does.
it not dynamic.
code eg.
showUser= Request.Form("user")
...
//listbox code
<select name="user" >
<option value="1" Selected> Choose User..</option>
...
<option value="<%=userRetrieved%>"> <%=userRetrieved%> </option>
...
<%
//tbl_Team is the table with user,task,project etc..tbl_user just lists all the usernames
sql="SELECT * FROM tbl WHERE tbl_Team.ID = tbl_users.ID"
If showUser="John" Then
sql = sql & " AND tbl_TeamEffort.User ='John'"
end if
%>
the IF-ELSE part is the part which I do not know how to write.
i want to write SQL/IF-ELSE statements where
IF user listbox's option value <> 1(or if listbox selected)
THEN sql=sql&"AND (tbl_Team.ID = (SELECT ID FROM tbl_users WHERE tbl_users.name ='"& showUser & "')"
END IF
IF task listbox's option value <>1
....
>>>>>>>
This is so that I can have any combination of listboxes. for example i can list [John and R&D]. Then ALL my records with user=john and task=R&D will show.
OR if i want to show [R&D and US], all records with task=r&D and country=US will show.
So, if the listboxes are not chosen, they show all the values.
THANKS in advance
|
|
 |