Lesson Five: ASP script variables, functions, procedures, and conditional statements

Source: Internet
Author: User
Tags date case statement continue http request numeric variables query square root
In the previous issue, the author briefed you on some of the basics of VBScript, one of the ASP scripting languages, and this issue will continue to explain how VBScript is scripted, and to show that VBScript has a series of examples in the process of ASP programming. into a layer of understanding.

After learning the basic concepts of variables, constants, and processes in the scripting language VBScript, this issue will continue to introduce you to the functions and syntax of VBScript.

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 one parameter is acceptable, 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.

Know the programming language friend must know in the program control process flow statements can be divided into conditional statements and circular statements, in VBScript can use the following conditional statement: 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

 

< Head>
< TITLE&G t;if1.asp</title>
< form action= "if2.asp" Method=get> ;
Your name< input name= "FirstName" maxlength=20>< p;
Your last name< input name= "LastName" maxlength=20>< p;
< input type=submit>< input type=reset>
</form>
</body> ;
to clip the following statement into a memo book and save it as if2.asp < Html>
< Head>
< title>ifrespond.asp< ; /title>
<% fname=request.querystring ("Firstname")
Lname=request.querystring ("  Lastname ")
If fname=" George "and Lname=" Washington "then%>
Hi.you must. <% else%>
hi! Nice to Meet you
<%end if%>
</body>

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 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
<% 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 be the 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!<"
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%>

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 for you to introduce the script language VBScript functions and conditional statements of use, due to the length of the reason can not be carried out in detail, I hope you have the desire to learn from the ASP's friends, in class after a certain degree of self-study and practice. In the daily development of ASP applications in the process of the author himself gradually realized the importance of scripting language, flexible use of scripting language will not only greatly improve the development of ASP application process, to the vast number of Web site production staff to save a lot of time, but also to enhance the implementation of ASP application efficiency and functionality. To good things its prerequisite, so the authors strongly recommend that you, proficient in scripting language, which will greatly help your ASP program development. Since this is not a VBScript tutorial, you can only use a small space to briefly introduce some of the basics of VBScript, after the next issue of VBScript Loop statement, we will formally start learning ASP's built-in objects, to go into VBScript, Suggest you find some textbooks for self-study. If you have any questions after reading this article, please Mail me in time, if you have any good suggestions also please write to inform, thank you.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.