In the previous small series to brief you on the ASP scripting language, one of the basic common sense of VBScript, this issue will continue to explain the VBScript scripting method, and by showing VBScript in the ASP program in the process of writing a series of examples to make it more in VBScript A layer of understanding.
Functions and procedures are named blocks of code, but they are very different, the process completes the program task, and the function returns the value. We can understand that the process is like a complete sentence, while the function is like a word. For example, when you want to get the square root of a number, you simply pass the number to the SQR () function in VBScript, which immediately returns the square root of the number. Such as:
A=SQR (9)
Is a=3. Mastering the scripting language functions will bring you great convenience in writing ASP programs, for the after-school exercises that the authors have assigned to you at the end of the previous issue, if you don't have a complete grasp of the scripting language's functions, solving such a small problem is likely to cost you quite a lot of effort. Now let's review the practice after this lesson.
"The author is using ASP to make a web-based BBS system, in the hope of adding a special feature, that is, when any user login to the BBS will be able to access the nearly seven days of all new information published." ”
If you are unfamiliar with Vbscrip, you will not know that Vbscrip itself provides a function DateSerial to get the difference between dates, and its syntax is as follows:
DateSerial (year, month, day)
If you want to specify a date, for example: November 10, 1998, then the range of values for each parameter in the DateSerial function should be acceptable, and the value of the day should be between 1 and 31, and the value of the month should be between 1 and 12. However, you can also specify a relative date for each parameter by using a numeric expression that represents the number of years, months, or days before or after a day. The following sample uses a numeric expression instead of an absolute date. Here, the DateSerial function returns the date of 20 years (1990-20) 2 months (11-2) and one day (10-1) before November 10, 1998: September 9, 1978. The procedure is as follows:
Datep=dateserial (1998-20, 11-2,10-1)
For the year parameter, if the value range is from 0 to 99, it is interpreted as 1900-1999 years. For year parameters outside of this range, four-digit numbers are used to represent years (for example, 1800). When the value of any parameter exceeds an acceptable range, it is appropriately rounded to the next larger time unit. For example, if you specify 35 days, the number of days will be interpreted as a month plus extra days, depending on the year and month. However, an error occurs if the parameter value exceeds the range of 32,768 to 32,767, or if the date specified by three parameters, whether directly or through an expression, is outside the acceptable date range. When we understand and master the use of function DateSerial, and then look at the author's layout of this problem, everything will be solved. I'll publish this part of the code in the program as follows:
Itemp=dateserial (date), month (date), day (date) -7)
Itemp=datevalue (itemp)
sql= "SELECT * from Message Where message.creatime Between # "&date&" # and # "&itemp&" # "
Here we come to a set of function Year,month,day, which are used to get the year, month, and day of a date. Date is a constant that represents today's date, and function DateValue is a variable that converts a string variable into a date format. In the third line of this procedure, we first contacted the standard SQL query statement, what does this sentence mean?
"Select" is a standard SQL database query command, in which we can retrieve data from the database and provide the results to the user, where "*" means to query all records in the database named "Message", and "where" The function is to set a query condition to take out the qualifying records in the database, "Message.creatime" is a variable that stores the date the record was created in the database. The whole sentence is understood by querying all the records in the database named message, and storing all the records in the variable SQL that were created in today and 7th before today. Perhaps because of the first contact with SQL statements, a time can not fully understand its role, but do not worry in the future chapters of the author will be devoted to the introduction of the use of SQL.
Through the above study, we should have been able to understand the function in the program, of course, we do not have to die back function, but to be proficient in the use of only a shortcut-more practice. Let's look at the basic syntax of VBScript.
A friend who understands programming languages must know that in a program the statements of control program flow can be divided into conditional statements and circular statements, and the following conditional statements can be used in VBScript:
If ... Then ... Else statement
Select Case Statement
If ... Then ... The Else statement evaluates to whether the condition is True or False and specifies the statement to run based on the result of the calculation. Typically, a condition is an expression that compares a value or variable using a comparison operator, If ... Then ... The Else statement can be nested as needed.
Let's create two sample files: If1.asp and if2.asp
Clip the following statement into a memo book and save it as if1.asp (note: Remove the space after "<" in your program)
〈head> < title>if1.asp</title>
Asp1.asp produces a text input box that requires the user to enter a last name, a name, and the following figure:
Asp2.asp is to use if statements to determine whether the user entered the name is "George Washington", and make corresponding feedback. Here we encounter an ASP's built-in object request, which allows you to access any information that is delivered with an HTTP request, including parameters, cookies, and user authentication that are passed from the HTML table using the POST method or the Get method. The QueryString collection retrieves the value of the variable in the HTTP query string, which is specified by the value after the question mark (?). Such as:
Http://localhost/if2.asp?Firstname=George&Lastname=Washington
Generates a variable name string with a value of "Firstname=george&lastname=washington". About the ASP object the author will focus on the next few.
If ... Then ... A variant of the ELSE statement allows you to select from multiple conditions, that is, to add a ElseIf clause to augment the If ... Then ... Else statement, which allows you to control the process based on a variety of possible programs.
We will expand the Asp2.asp program part as follows: <%
Fname=lcase (Request.QueryString ("Firstname"))
Lname=lcase (Request.QueryString ("Lastname"))
If fname= " George "and Lname=" Washington "then%>
hi.you
must be the" <% elseIf Fname= "Ronald" and Lname= "Reagan" then
%> hi.you must be the actor president! <% elseIf Fname= "Jimmy" and Lname= "Carter" then%>
hi.you must be the peanut farmer <% elseIf Fname= "Naoko" or Fname= "Charles" then%>%1 hi.your
name reminds me of Someone,but I am not sure who!
<% Else%>
Hi! Nice to Meet your
<% End If%>
You can add any number of ElseIf clauses to provide multiple choices. But using multiple ElseIf clauses often makes a program cumbersome. A better way to choose among multiple conditions is to use the Select Case statement.
The Select case structure provides an If ... Then ... An alternative form of a ELSEIF structure that you can select from multiple statement blocks to execute one of them. The Select Case statement provides functionality with the If ... Then ... Else statements are similar, but can make the code more concise and readable. The Select case structure begins with a simple test expression that is evaluated only once. The result of an expression is compared to the value of each case in the structure. If it matches, then the statement block associated with the case is executed, and we can also write the asp2.asp file with the Select statement:
<%
Fname=lcase (request.querystring ("Firstname"))
Lname=lcase (Request.QueryString ("Lastname"
)) Name=fname+lname
Select Case name Case
"Georgewashington"
Response.Write "Hi.you must is the esident! "
Case "Ronaldreagan"
Response.Write "hi.you must to be the actor president!"
Case "Jimmycarter"
Response.Write "hi.you must to be the peanut farmer"
Case "Naokocharles"
Response.Write "Hi.your name reminds me to someone,but I am not sure who!"
Case Else
Response.Write "hi! Nice to Meet your "end
Select%>
Note that the Select case structure computes only one expression at the beginning and evaluates only once, and If ... Then ... The ELSEIF structure computes an expression for each ELSEIF statement, which can vary. Therefore, you can use the Select case structure instead of if when the expressions that are evaluated by each ELSEIF statement are the same. Then ... ELSEIF structure. The Select Case statement can also be nested, and each level of nested Select Case statements must have a matching End Select statement.
The above to introduce the scripting language VBScript functions and conditional statements of use, due to the length of the reason can not be carried out in detail, I hope to be interested in ASP friends, after reading this article to a certain degree of self-study and practice.