In the previous issue, the author briefly introduced some basic common sense of VBScript, one of the ASP scripting languages. In this issue, we will continue to explain the scripting method of VBScript to you, and will show you a series of examples of VBScript in the ASP program writing process to help you Have a better understanding of VBScript.
After learning the basic concepts of variables, constants and processes of the scripting language VBScript, this issue will continue to introduce VBScript's functions and syntax to you.
Functions are named blocks of code like procedures, but they are very different. When the process completes the program task, the function returns the value. We can understand this way 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 certain number, you just pass the number to the Sqr() function in VBScript, and this function will immediately return the square root of the number. like:A=sqr(9)
Then A=3. Proficient in the scripting language functions will bring you great convenience to writing ASP programs. As for the after-class exercises assigned by the author at the end of the previous issue, if you do not have a comprehensive understanding of the scripting language functions, then this is the solution. A small problem will most likely cost you considerable energy. Now let's review this after-class exercise.The author is using ASP to create a WEB-based BBS system, hoping to add a special feature to it, that is, when any user logs into the BBS, he can view all newly released information in the past seven days.
If you are not familiar with VBScript, you will not know that VBScript itself provides a function DateSerial to obtain the difference or sum between dates. Its syntax is as follows:
DateSerial(year, month, day)
If you want to specify a date, for example: November 10, 1998, then the value range of each parameter in the DateSerial function should be acceptable, that is, the value of day should be between 1 and 31, and the value of month should be between 1 and 31, and the value of month should be Should be between 1 and 12. However, a relative date can also be specified for each parameter using a numeric expression representing the number of years, months, and days before or after a certain day. The following example uses numeric expressions instead of absolute dates. Here, the DateSerial function returns the date of twenty years (1990-20) and two months (11-2) and another 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 to 1999. For year parameters outside this range, four digits are used to represent the year (for example, 1800). When the value of any parameter exceeds the acceptable range, it will be properly carried to the next larger unit of time. For example, if 35 days are specified, the number of days will be interpreted as one month plus the number of extra days, which depends on its year and month. But an error occurs if the parameter value exceeds the range of -32,768 to 32,767, or if the date specified by three parameters (either directly or through an expression) is outside the acceptable date range.After we understand and master the use of the function DateSerial, let’s take a look at the question assigned by the author and everything will be solved. Below I will publish this part of the code in the program as follows: issue=DateSerial(Year(date), month(date), day(date)-7)
item=DateValue(itemp)
sql=Select * from message Where message.creatime Between #&date&# And #&itemp&#
Here we come into contact with a set of functions Year, month, and day, which are used to obtain a date. date is a constant representing today's date, while the function DateValue is a variable that converts a string variable into a date format. In the third line of this program, we first came into contact with the standard SQL query statement. What does this statement mean?
Select is a standard SQL database query command. Through the SELECT statement, we can retrieve data in the database and provide the query results to the user. The * here means querying all records in the database named message, and the function of where is Setting a query condition is to retrieve records that meet the conditions in the database. message.creatime is a variable that stores the date of creation of records in the database. To connect the entire sentence and understand it: query all records in the database named message, and store all records within seven days before today and before today in the variable SQL. Maybe because you are exposed to SQL statements for the first time, you cannot fully understand its function for a while, but don’t worry that in the future chapters, the author will use a special issue to introduce the usage of SQL to you.
Through the above learning, everyone should be able to understand the role of functions in programs. Of course, we don’t have to memorize functions, but there is only one shortcut to be proficient in using them - practice more. Next, let's take a look at the basic syntax of VBScript.
Friends who know programming languages must know that the statements that control program flow in programs can be mainly divided into conditional statements and loop statements. The following conditional statements can be used in VBScript: If...Then...Else statement
Select Case statement
The If...Then...Else statement is used to calculate whether the condition is True or False, and to specify the statement to run based on the calculation result. Typically, the condition is that an expression that compares values or variables is used using the comparison operator, and the If...Then...Else statement can be nested as needed.
Let's create two sample files: if1.asp and if2.asp
Scrap the following statement into the notepad and save it as if1.asp (Note: Please remove the space after < in the program) <html>
< head>
< TITLE>if1.asp< /TITLE>
< /head>< body bgcolor=#FFFFFF>
< form action=if2.asp method=get>
Your First Name< INPUT NAME=FirstName MaxLength=20>< p>
Your Last Name< INPUT NAME=LastName MaxLength=20>< p>
< INPUT TYPE=submit>< INPUT TYPE=reset>
< /form>
< /body>
< /html>
Scrap the following statement into the notepad and save it as if2.asp <html>
< head>
< TITLE>ifrespond.asp< /TITLE>
< /head>
< % fname=request.querystring(Firstname)
lname=request.querystring(Lastname)
If fname=George and lname=Washington then %>
Hi.You must be the first president!
< % else %>
Hi!Nice to Meet You
< %end if %>
< /body>
< /html>
asp1.asp generates a text input box that requires the user to enter his or her last name.
asp2.asp is to use the IF statement to determine whether the name entered by the user is George Washington, and provide corresponding feedback. Here we encounter an ASP built-in object request. By using the request object, you can access any information passed with HTTP requests, including parameters, cookies, and user authentication passed from HTML tables using POST or GET methods. The QueryString collection retrieves the value of a variable in an HTTP query string, and the HTTP query string is specified by the value after the question mark (?). like:
http://localhost/if2.asp?Firstname=George&Lastname=Washington
Generate a variable name string with the value Firstname=George&Lastname=Washington. The author of the ASP object will focus on the following articles.
A deformation of the If...Then...Else statement allows you to choose from multiple conditions, i.e. add the ElseIf clause to expand the functionality of the If...Then...Else statement, allowing you to control based on multiple conditions. Possible procedure flow.
We have expanded the program part of asp2.asp as follows: < %
fname=lcase(request.querystring(Firstname))
lname=lcase(request.querystring(Lastname))
If fname=george and lname=washington then %>
Hi.You must be the first president!< p>
< % elseIf fname=ronald and lname=reagan then %>
Hi.You must be the actor president!< p>
< % elseIf fname=jimmy and lname=carter then %>
Hi.You must be the peanut farmer president!< p>
< % elseIf fname=naoko or fname=charles then %>
Hi.Your name reminds me of someone, but I am not sure who!< p>
< % else %>
Hi!Nice to Meet You
< % end if %>
As many ElseIf clauses can be added to provide multiple options. But using multiple ElseIf clauses often makes the program very cumbersome. A better way to choose among multiple conditions is to use the Select Case statement.
The Select Case structure provides a workaround for the If...Then...ElseIf structure, you can select and execute one of them from multiple statement blocks. The Select Case statement provides similar functions to the If...Then...Else statement, but can make the code more concise and easy to read. The Select Case structure uses a simple test expression that is calculated only once at its beginning. The result of the expression is compared to the value of each Case in the structure. If it matches, execute the statement block associated with the Case. We can also use the Select Case statement to write the asp2.asp file:
< %
fname=lcase(request.querystring(Firstname))
lname=lcase(request.querystring(Lastname))
name=fname+lname
Select case name
case georgewashington
response.write Hi.You must be the first president!< p>
case ronaldreagan
response.write Hi.You must be the actor president!< p>
case jimmycarter
response.write Hi.You must be the peanut farmer president!< p>
case naokocharles
response.write Hi.Your name reminds me of someone, but I am not sure who!< p>
case else
response.write Hi!Nice to Meet You
End Select %>
Note that the Select Case structure only calculates one expression at the beginning and only once, while the If...Then...ElseIf structure calculates the expression for each ElseIf statement, which can vary. Therefore, the Select Case structure can be used instead of the If...Then...ElseIf structure only if the expression calculated by each ElseIf statement is the same. The Select Case statement can also be nested, and each layer of nested Select Case statement must have a matching End Select statement.
The above method of using functions and conditional statements of the script language VBScript introduced to you cannot be detailed due to length. I hope that all friends who are interested in learning ASP can perform a certain degree of self-study and practice after class. In the process of developing ASP applications on a daily basis, the author himself has gradually realized the importance of scripting language. Flexible use of scripting language will not only greatly improve the development process of ASP applications, save a lot of time for website producers, but also enhance the The execution efficiency and functionality of ASP applications. If you want to do something well, you must first sharpen your tools. Therefore, the author strongly recommends that you master the scripting language proficiently, which will be of great help to your ASP program development. Since this article is not a VBScript tutorial, we can only use a small space to briefly introduce some basic VBScript knowledge. After introducing the loop statements of VBScript in the next issue, we will officially start learning the built-in objects of ASP. We need to go deep into VBScript. We recommend that you go into VBScript. Everyone finds some textbooks to study on their own. If you have any questions after reading this article, please mail me in time. If you have any good suggestions, please write to inform me, thank you.