web development part 03.pdf

Full Transcript

Web development – PART 03 Static web pages Static web pages are web pages that are delivered to the user's web browser exactly as stored, without any server-side processing or dynamic content generation. They are simple H...

Web development – PART 03 Static web pages Static web pages are web pages that are delivered to the user's web browser exactly as stored, without any server-side processing or dynamic content generation. They are simple HTML files that can include text, images, and links, and they remain unchanged unless manually updated by the web developer. They include; Fixed content Faster processing and loading Simplicity Dynamic web pages Dynamic web pages are web pages that are generated in real-time based on user interactions, database queries, or other server-side processing. Unlike static web pages, dynamic web pages can display different content and allow for interactive features based on user input or other factors. They include; Dynamic content Database interaction Interactive web content with the user Scripts In the context of web development, a "script" refers to a piece of code written in a programming or scripting language that automates tasks, controls the behavior of web pages, or manages data and logic on a server. Scripts can be executed either on the client side (in the user's browser) or on the server side (on the web server). Client side scripting Client-side scripting refers to scripts that run in the user's web browser, directly on the client side (the user's device). These scripts are primarily used to create interactive web pages and improve user experiences without needing to constantly communicate with the server. Server side scripting Server-side scripting refers to scripts that run on the web server. These scripts generate dynamic content, interact with databases, manage user sessions, and handle business logic before sending the final output to the client's browser. ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 1 Web development – PART 03 PHP PHP (Hypertext Preprocessor) is a widely-used open-source scripting language especially suited for web development and can be embedded into HTML. It is powerful and flexible, making it one of the most popular server-side scripting languages. Its features; Server side execution Can be embedded with HTML Cross platform (supported by multiple OSs.) Embedding PHP into web pages PHP codes can be embedded with HTML codes easily. To define a PHP script, we use tags (unlike HTML tags for elements). Printing using PHP We can use 2 methods to display something on the web page. Print Echo o This is the most used method to display something on the web page. And also, this can be used with HTML tags too. o E.g.: Output :- ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 2 Web development – PART 03 Variables Every identifier used for variables in PHP should start with ‘$’. The first character after the dollar sign must be a letter (a-z, A-Z) or an underscore (_). The subsequent characters can be letters, numbers (0-9), or underscores (_). No other characters are allowed. Variable names in PHP are case-sensitive. $Variable and $variable would be considered different variables. E.g.: o $var = "value"; o $_var = "value"; o $var1 = "value"; o $variable_name = "value"; Data types Integer o $int = 1234; o $negative_int = -5678; o $octal = 0755; o $hex = 0x1A; Float o $float = 10.365; o $scientific = 2.4e3; // 2.4 x 10^3 String o $string1 = "Hello, World!"; o $string2 = 'Hello, PHP!'; Boolean o $true = true; o $false = false; Array o // Indexed array $array = array(1, 2, 3, 4); o // Associative array $assoc_array = array("name" => "John", "age" => 30); ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 3 Web development – PART 03 Operators Arithmetic operators ** Exponent + Addition - Subtraction / Floor division * Multiplication % Remainder Comparison operators == Equality operator – checking value only Identical operator – checking both value and data === type != Inequality operator Inequality operator !== Not identical operator > Greater than < Less than >= Greater than or equal value pairs. Key and value can be any type of data. ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 5 Web development – PART 03 E.g.: Selection in PHP Syntax; if ( statement ) { command ; } else if ( statement ) { command ; } else { command ; } E.g.: ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 6 Web development – PART 03 while loop structure Syntax; while ( statement ) { loop body; - - } E.g.: for loop structure Syntax; for( variable starting value; checking condition; increment or decrement ) { loop body; - - } E.g.: ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 7 Web development – PART 03 for each loop structure This is used to iterate over arrays and objects. It provides a convenient way to loop through each element in an array or each property in an object without needing to know the keys or indexes. Syntax; foreach ($array as $value) { // code to be executed for each element in the array } E.g.: Functions functions are blocks of code that can be called to perform a specific task. They help in organizing code, promoting code reuse, and improving readability and maintainability. PHP supports both built-in functions and user-defined functions as same as Python. User defined functions: Syntax; function funtion_name(parameter_list) { function_body - - return returning_item } E.g.: ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 8 Web development – PART 03 Server and database connection As we know PHP is a server-side scripting language. We can use PHP to handle data sent from an HTML form and save in a database runs on a remote server. PHP uses different types of methods to connect with a server. Two of them are o mysqli() – the object oriented style o mysqli_conncet() – procedural style Both of above accepts 4 values. They are; o Server name o Username o Password o Database name E.g.: Using mysqli() class; o die() – this is used to print some text and terminate the following code’s process. o connect_error – this is used to return any kind of errors occurred when connecting to a server. ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 9 Web development – PART 03 Using mysqli_connect() function; o die() – this is used to print some text and terminate the following code’s process. o Mysqli_connect_error() – this is used to return any kind of errors occurred when connecting to a server. In both cases; o localhost – server name (or IP address) o webweb – username of the account o mysql – password of the account o testing – database should connect Insertion to a table o Now we can make changes in the database from the PHP file. o Suppose there is a table in the database called details. o Now we are going to insert some records to the table from the PHP code. ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 10 Web development – PART 03 In above code; 1. First, we create the connection to the server and the database. 2. Then we define an SQL statement which should be executed in the database. 3. After that, we execute the SQL statement in the connection. 4. Then we can check the statement was executed successfully or not. Like this, we can execute any SQL statement in the PHP code which is connected to the server and the database. ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 11 Web development – PART 03 Data retrieval from a table This is the final stage in this PHP+SQL chapter. So far, we made changes in the database table. In the same way, we can retrieve data from a table into the web page by using select SQL command. E.g.; ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 12 Web development – PART 03 Forms action and method The action Attribute The action attribute specifies the URL of the server-side script (often a PHP script) that will handle the form submission. If the action attribute is omitted, the form data is submitted to the same page that contains the form. The method Attribute The method attribute specifies how the form data should be sent to the server. It can take one of two values: get or post. 1. method="get" When the form uses the GET method, the form data is appended to the URL specified in the action attribute as query parameters. This method is suitable for non-sensitive data and when the form data needs to be bookmarked or shared. 2. method="post" When the form uses the POST method, the form data is sent in the body of the HTTP request. This method is more secure than GET as it does not expose form data in the URL. It is suitable for handling sensitive information such as passwords and large amounts of data. Capturing data sent from an HTML form in the PHP script. When a set of data is sent from an HTML form, as we discussed earlier, we use action and method attributes in the tag. In order to receive them (or to capture them), we use $_GET[] or $_POST[] superglobals in the PHP script. E.g.: Consider the simple HTML form below. It gathers some data about items. ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 13 Web development – PART 03 The destination for the form input data is set to: Now, when the data is entered and user clicks the submit button, data entered is sent to the index.php script. The PHP script is: ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 14 Web development – PART 03 Local publishing Typically refers to the distribution of printed materials or digital content within a specific geographic area or community. This can include newspapers, magazines, books, newsletters, and other types of publications aimed at a local audience. Internet publishing Internet publishing refers to the creation and dissemination of content via the internet. This broad field encompasses various forms of digital content such as blogs, online newspapers and magazines, eBooks, social media posts, podcasts, and video content. Website performance Website performance is critical for ensuring a positive user experience, high search engine rankings, and effective engagement with content. Several factors influence website performance, and they can be broadly categorized into technical, user experience, and content-related aspects. Page Load Time: The time it takes for a web page to fully load. Faster load times reduce bounce rates and improve user satisfaction. Caching: Storing copies of web pages to reduce server load and speed up loading times for returning visitors. Server Response Time: The speed at which the server responds to user requests. Meta Tags: Optimizing title tags, meta descriptions, and headers for better search engine rankings. ADVANCED LEVEL ICT - BHANUKA WICKRAMASINGHE 15

Use Quizgecko on...
Browser
Browser