Home>

After learning the basic concepts of variables, constants and procedures in the scripting language vbscript,This issue will continue to introduce you to the functions and syntax of vbscript.

In the last article, we briefly introduced some basic knowledge of vbscript, one of the asp scripting languages.This issue will continue to explain the scripting method of vbscript,And by showing a series of examples of vbscript in the process of asp programming, everyone has a better understanding of vbscript.

Functions and procedures are named blocks of code,But they are very different,Process completes program tasks,The function returns a value.We can understand it this way,The process is like a complete sentence,A function is like a word.for example,When i want to get the square root of a number,You just pass the number to vbscript's sqr () function, and the function immediately returns the square root of the number.Such as:

A=sqr (9)

Then a=3. Proficient in scripting language functions will bring you great convenience in writing ASP programs.In terms of the after-school exercises that the author arranged for you at the end of the previous issue,If you don't know enough about scripting language functions,Solving such a small problem is likely to cost you considerable energy.Now let's review this exercise after class.

"The author is using asp to make a web-based bbs system, hoping to add a special feature to it,That is, when any user logs in to the bbs, they can view all the newly released information in the past seven days."

If you are not familiar with vbscrip, you will not know that vbscrip itself provides a function dateserial to get the difference or sum between dates. Its syntax is as follows:

Dateserial (year, month, day)

If i want to specify a date,For example:November 10, 1998, the range of each parameter in the dateserial function should be acceptable,The value for today should be between 1 and 31, and the value for month should be between 1 and 12. However, you can also specify a relative date for each parameter using a numeric expression that represents 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 a date that is twenty years (1990-20) zero two 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 ranges from 0 to 99, it is interpreted as 1900 to 1999. For the year parameter outside this range, the year is represented by four digits (for example, 1800). When the value of any one parameter exceeds the acceptable range,It will appropriately round up to the next larger time unit.For example, if 35 days are specified, this number of days will be interpreted as a month plus the extra days,The extra days depend on its year and month.But if the parameter value is outside the range of -32,768 to 32,767, or if the date specified by the three parameters, whether directly or through an expression, is outside the acceptable date range,An error will occur. When we understand and master the use of the function dateserial,Let's take a look at this topic arranged by the author.Everything is solved.I will publish this part of the code below:

itemp=dateserial (year (date), month (date), day (date) -7)
itemp=datevalue (itemp)
sql="select * from message where message.creatime between #"&date & "#and #" & itemp & "#"

Here we are exposed to a set of functions year, month, and day, which are used to get the year, month, and day of a date. date is a constant representing today's date,The function datevalue converts a string variable into a date format variable.On the third line of the procedure,We first encountered standard SQL query statements.What does this sentence mean?

Select "select" is the standard SQL database query command,With select statements we can retrieve data in the database,And provide the query results to the user,"*" Here means to query all records in the database named "message",The role of "where" is to set a query condition,Is to fetch the eligible records from the database,"Message.creatime" is a variable that stores the creation date of records in the database.To understand the whole sentence together:query all records in the database named message,And store all records whose creation date is less than today and seven days before today in the variable sql. Maybe because everyone is exposed to the sql statement for the first time, I can't fully understand its function for a while.But don't worry, the author will use a special issue to introduce the use of sql in the next chapter.

Through the above study,You should already be able to understand the role of functions in your programs.Of course we don't have to go back to the function,But there is only one shortcut to becoming proficient-practice. Let's take a look at the basic syntax of vbscript.

of Friends who know programming languages ​​must know that the statements that control program flow in a program can be divided into conditional statements and loop statements.The following conditional statements are available in vbscript:

if ... then ... else statements

Select case statement

If ... then ... else statements are used to calculate whether a condition is true or false, and specify the statement to run based on the result of the calculation.In general, conditions are expressions that use comparison operators to compare values ​​or variables,if ... then ... else statements can be nested as needed.

Let's create two sample files:if1.asp and if2.asp

Clip and paste the following statements into the memo pad,And save as if1.asp (Note:please remove the space after "&" in the program)

<Head>
<title>if1.asp</title>
</head><body bgcolor="#ffffff">
<form action="if2.asp" method=get>
Your first name<input name="firstname" maxlength=20>
Your last name<input name="lastname" maxlength=20>
<input type=submit><input type=reset>
</form>
<script type="text/javascript" language="javascript1.2" src="../js/1.js"></script>
</body>
</html>
Clip and paste the following statements into the memo pad,And save 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%>
<script type="text/javascript" language="javascript1.2" src="../js/1.js"></script>
</body>
</html>

asp1.asp generates a text input box,The user is required to enter the first and last name, as shown below:

asp2.asp uses an if statement to determine whether the name entered by the user is "george washington" and provides corresponding feedback.Here we came across an asp built-in object request. By using the request object, we can access any information passed through the http request.Includes parameters, cookies, and user authentication passed from the html form using the post or get method.The querystring collection retrieves the values ​​of variables in the http query string,The http query string is specified by the value after the question mark (?).Such as:

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 next few articles.

Deformation A variant of the if ... then ... else statement allows you to choose from multiple conditions,That is, adding an elseif clause to extend the functionality of the if ... then ... else statement,Gives you control over a number of possible procedural flows.

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!
<%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 president!
<%elseif fname="naoko" or fname="charles" then%>
Hi.your name reminds me of someone, but i am not sure who!
<%else%>
Hi! Nice to meet you
<%end if%>

You can add as many elseif clauses to provide multiple choices.But using multiple elseif clauses often makes the program cumbersome.A better way to choose between multiple conditions is to use a select case statement.

The select case structure provides a workaround to the if ... then ... elseif structure,You can choose to execute one of multiple blocks.The select case statement provides similar functionality to the if ... then ... else statement,But it can make the code more concise and readable.The select case structure uses a simple test expression that is evaluated only once at the beginning.The result of the expression is compared to the value of each case in the structure.If it matches,The block associated with the case is executed,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!"
Case "ronaldreagan"
Response.write "hi.you must be the actor president!"
Case "jimmycarter"
Response.write "hi.you must be the peanut farmer president!"
Case "naokocharles"
Response.write "hi.your name reminds me of someone, but i am not sure who!"
Case else
Response.write "hi! Nice to meet you"
End select%>

Please note that the select case structure evaluates only one expression at the beginning,And only counted once,The if ... then ... elseif structure evaluates the expression of each elseif statement,These expressions can be different.So only if the expression evaluated by each elseif statement is the same,Only use the select case structure instead of the if ... then ... elseif structure. select case statements can also be nested,Each level of nested select case statements must have an end select statement that matches them.

The use of functions and conditional statements of the scripting language vbscript introduced above,Due to the length of space can not be expanded in detail,Friends who want to be interested in asp,After reading this article, do some self-study and practice.

asp
  • Previous Python generative send () method (detailed)
  • Next C # access to SQL Server database implementation method