Lesson 10 - Web Development
Lesson 10 - Web Development
php
?>
<p> So, HTML formats a webpage and CSS defines how that formatting should be displayed.
Sounds like that is all we need to make a nice website, so what is PHP for? I am glad you asked!
PHP, as you recall, stands for PHP Hypertext Preprocessor and it does what its name implies: it
preprocesses hypertext.</p>
Competency 10: Develops websites incorporating multi-media technologies (using HTML 5)
The World Wide Web (WWW) invented by Tim Berners Lee in 1989 at the European Centre for
Nuclear Research, as a major application on the Internet. It was the first application on the Internet
that caught the attention of the public and it dramatically changed how people interact with the
network of computers. Essentially, the World Wide Web (WWW) is an application that runs on top
of the Internet, which is in turn made up of a lot of interconnected computers.
According to Wikipedia, World Wide Web is an information system where documents and other
web resources are identified by Uniform Resource Locators(URLs), which may may be interlinked
by hypertext, and are accessible over the Internet. The basic element in WWW is the web page that
a user views when he or she accesses it. Some of the computers on the Internet, called web
servers, are designed to serve the web pages. These web servers are always on computers and are
ready to serve the web pages all the time. The other computers request web pages from the web
servers and are called the client computers. The WWW operates on-demand, the users request
web pages from web servers whenever they want and receive them from web servers. The WWW
has many wonderful features that people like a lot; it provides a platform for any individual who
has access to a computer and the Internet to make information available on the web at very less
cost. Formatted Text, multimedia, graphics, forms, and applets are part of WWW and are accessible
by the client computers.
WWW also serves as a platform for many innovative applications like search engines such as
Google, other popular Google applications such as e-mail, Social media such as Facebook,
multimedia streaming platforms such as YouTube, online photo sharing applications such as Flickr,
etc.
A Web page can consist of a number of web objects such as a simple text file, for example, an
HTML file, an image file such as a jpeg file, a Java applet, an audio file or a video clip. The text in a
web page called hypertext means that it is the text, which contains links to other text. Any word in
a hypertext document can be specified as a pointer to a different hypertext document where more
information pertaining to that word can be found. A web page stored in a server computer can be
accessed by issuing the address of the server, which houses the resource or object, called a
Uniform Resource Locator (URL), or a hyperlink. Each URL has three components: the first part is
the protocol, which tells the browser how to communicate with a website’s server, in order to send
and retrieve information, the second part is the domain name, which is an identifier to a specific
site, and the third part is the path, which directs the browser to a specific page on the website.
Typically a web page will consist of a base HTML file and 80 several other objects that are
embedded into the base HTML file. Each embedded object will be referred to using its URL. If a
given web page has n number of other objects referred in it, then we have a total of (n+1) objects
on the web page including the base HTML file.
Web pages requested by clients can be viewed using a special application called web browsers.
Present-day web browsers are intelligent applications and they provide many features to the users.
Google Chrome, Mozilla Firefox, Internet Explorer and Apple’s Safari are examples of popular web
browsers. Web pages are written in HyperText Markup Language (HTML). The markup facility of
the HTML enables the web browsers to render plain text, combined with additional tags, as easily
readable attractive text on the display of computers. HTML Tags are symbols that indicate a visual
element for the page, such as a heading, that governs the size, color, and other properties of the
element. Each of these web browser applications have their strengths and weaknesses, and can
render the HTML code slightly differently. It would be better that when HTML code is written it
should be tested on each of this web browser applicaton software for completeness.
Parts of a URL
Domain name is used to uniquely identify a web site. This makes the users remember it somewhat
easily. However, an IP address is used to uniquely identify a computer on the Internet. What the
Domain Name Server does is converting the domain name to an IP address.
Analyses user requirements (multimedia contents) Objectives of a web site should be defined well
to build a great web site. It helps the designer to identify the requirements of the users, to define
them and to satisfy the user requirements. A set of well defined such objectives and their usability
solutions can increase the user experience and will keep the users regularly visit the web sites.
Some possible questions about the objectives of a website are:
User requirements of a web site describe the needs of users, the functionalities that the web site
should do, and the goals of a web site. The designer of the website should define, document and
describe the user requirements of the web site. These requirements would change over time and
the designers should keep the website updated based on the changing requirements. Some of the
possible requirements that may be collected before commencing the design are listed below:
Content – Determining the content of the web site that needs to be updated on the
web site is an essential part of the design.
Layout of the design and the navigation pattern – Determining what would appear in
the layout of the website, and how the user would be able to navigate from the
home page to other pages.
Usability – identifying the accessibility requirements of the web site.5
Security – identifying the security requirements of the web site.
Loading times – determining the loading speed of a website depending on the kind
of service it provides.
Legal – identifying the legal implications that the web site must adhere to.
Competency Level 10.3: Identifies appropriate HTML tags to design a single web page.
Identifies appropriate HTML tags to design a single web page HTML Tags are symbols that
indicate a visual element for the page, such as a heading, that governs the size, color, and other
properties of the element. When a web browser loads a web page, it renders the content of the
web page on the computer’s display according to the included HTML markup and content. A
number of tools are available to create HTML pages and for learning purposes, the notepad
application available in desktop computer is sufficient. Notepad is a lightweight application that
enables the typing of HTML pages without much trouble. Typed web pages could be saved using
the extension .html. There exist multiple sophisticated tools that enable HTML typing and there
even other online resources available to type in HTML.
A number of tools are available to create HTML pages and for learning purposes, the notepad
application available in desktop computer is sufficient. Notepad is a lightweight application that
enables the typing of HTML pages without much trouble. Typed web pages could be saved using
the extension .html. There exist multiple sophisticated tools that enable HTML typing and there
even other online resources available to type in HTML.
Tag or Description Example Output
Heading 1
<h1>Heading 1</h1>
<h2>Heading 2</h2>
Heading 2
Headings <h3>Heading 3</h3>
<h4>Heading 4</h4>
Heading 3
<h5>Heading 5</h5> Heading 4
<h6>Heading 6</h6>
Heading 5
Heading 6
This is a paragraph.
<p>This is a paragraph.</p>
Paragraphs This is a paragraph.
<p>This is a paragraph.</p>
This is a paragraph.
<p>This is a paragraph.</p>
Images are not technically inserted into a web page; images are linked to web pages.
The <img> tag creates a holding space for the referenced image.
The <img> tag is empty, it contains attributes only, and does not have a closing tag.
_self - Default. Opens the document in the same window/tab as it was clicked
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;">
</a>
Bookmarks can be useful if a web page is very long.To create a bookmark - first create the
bookmark, then add a link to it. When the link is clicked, the page will scroll down or up to the
location with the bookmark.
HTML Tables
An HTML form is used to collect user input. The user input is most often sent to a server for
processing.
Text Fields
<form>
First name: <br>
<input type="text" name="fname" value="John"><br>
Last name: <br>
<input type="text" name="lname" value="Doe">
</form>
Radio Buttons
<form>
<input type="radio" name="fav_language"
value="HTML">
HTML<br>
<input type="radio" name="fav_language" value="CSS">
CSS<br>
<input type="radio" name="fav_language"
value="JavaScript">
JavaScript
</form>
Checkboxes
<form>
<input type="checkbox" name="vehicle1" value="Bike">
I have a bike<br>
<input type="checkbox" name="vehicle2" value="Car">
I have a car<br>
<input type="checkbox" name="vehicle3" value="Boat">
I have a boat
</form>
The select Element
Choose a car:
<select id="cars" name="cars">
<option value="volvo" selected>Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
The textarea Element
<form action="/action_page.php">
<textarea name="message" rows="10" cols="30">The
cat was playing in the garden.</textarea>
</form>
Grouping Form Data with Fieldset
<form action="/action_page.php">
<fieldset>
<legend>Personalia:</legend>
First name: <br>
<input type="text" name="fname"
value="John"><br>
Last name: <br>
<input type="text" name="lname"
value="Doe"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
Password field
<form>
Password: <br>
<input type="password" name="pwd">
</form>
Submit and Reset button
<form>
Birthday:
<input type="date" name="birthday">
</form>
File upload
Select a file:
<input type="file" name="myfile">
Others
Ex – <input type="color">
<input type="color">
<input type="datetime-local">
<input type="email">
<input type="month">
<input type="number">
<input type="range">
<input type="reset">
<input type="search">
<input type="tel">
<input type="time">
<input type="url">
<input type="week">
Input Restrictions
checked Specifies that an input field should be pre-selected when the page loads (for
type="checkbox" or type="radio")
The controls attribute adds video/audio controls, like play, pause, and volume.
It is a good idea to always include width and height attributes. If height and width are not set, the
page might flicker while the video loads.
The <source> element allows you to specify alternative video/audio files which the browser may
choose from. The browser will use the first recognized format.
The text between the <video> and </video> tags will only be displayed in browsers that do not
support the <video> element.
Playing a Video
Playing a Audio
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
With CSS, you can control the color, font, the size of text, the spacing between elements, how
elements are positioned and laid out, what background images or background colors are to be
used, different displays for different devices and screen sizes, and much more!
Advantages of CSS:
Web designers needs to use few lines of programming for every page improving site speed.
Cascading sheet not only simplifies website development, but also simplifies the
maintenance as a change of one line of code affects the whole web site and maintenance
time.
CSS changes are device friendly. With people employing a batch of various range of smart
devices to access websites over the web, there’s a requirement for responsive web design.
It has the power for re-positioning. It helps us to determine the changes within the position
of web elements who are there on the page.
These bandwidth savings are substantial figures of insignificant tags that are indistinct from
a mess of pages.
CSS Syntax
1) Inline CSS
The following example sets the text color of the <h1> element to blue, and the text color of
the <p> element to red:
2) Internal CSS
The following example sets the text color of ALL the <h1> elements (on that page) to blue, and
the text color of ALL the <p> elements to red. In addition, the page will be displayed with a
"powderblue" background color:
<html>
<head>
<style>
body {background-color: powderblue;}
h1{color: blue;}
p{color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
3)External CSS
An external style sheet is used to define the style for many HTML pages.
To use an external style sheet, add a link to it in the <head> section of each HTML page:
<head>
<link rel="stylesheet" href="styles.css">
</head>
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the following rules, where
number one has the highest priority:
3. Browser default
So, an inline style has the highest priority, and will override external and internal styles and
browser defaults.
Example
Assume that an external style sheet has the following style for the <h1> element:
h1 {
color: navy;
}
If the internal style is defined after the link to the external style sheet, the <h1> elements will be
"orange":
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
<body>
<h1>This is a heading</h1>
</body>
</head>
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
The element selector selects HTML elements based on the element name.
Example
Here, all <p> elements on the page will be center-aligned, with a red text color:
p{
text-align: center;
color: red;
}
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique
element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element.
Example
The CSS rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
}
3) The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with class="center" will be red and center-aligned:
.center {
text-align: center;
color: red;
}
The grouping selector selects all the HTML elements with the same style definitions.
h1, h2, p {
text-align: center;
color: red;
}
Select and style every <p> element with the class "intro":
p.intro {
background-color: yellow;
}
6) Anchor Pseudo-classes
font-family: family-name|generic-family|initial;
Font-family property can have values like family name (times, courier, arial, etc.) and
CSS background property is used to define the background effects on element. There are 5 CSS
background properties that affects the HTML elements:
background-color
background-image
background-image: url("paper1.gif");
background-repeat
background-attachment
background-position
The CSS border properties are use to specify the style, color and size of the border of an element.
The CSS border properties are given below,
border-style
border-color
border-width
border-radius
CSS Lists
list-style-type: This property is responsible for controlling the appearance and shape of the
marker.
list-style-type:decimal;
list-style-type:lower-alpha;
list-style-type:lower-roman;
list-style-type:circle;
list-style-type:square;
list-style-type:disc;
list-style-image: It sets an image for the marker instead of the number or a bullet point.
CSS Table
We can apply style on HTML tables for better look and feel. There are some CSS properties that are
widely used in designing table using CSS:
border
<style>
table, th, td {
border: 1px solid black;
}
</style>
border-collapse:collapse; (single border)
padding
width
height
text-align
color
background-color
CSS Text Propeties
In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of:
margins, borders, padding, and the actual content.
The following diagram illustrates how the CSS properties of width, height, padding, border and
margin dictate that how much space an attribute will occupy on a web page.
div {
width: 300px;
padding: 50px;
margin: 20px;
Total element width = width + left padding + right padding + left border + right border + left
Using the <div> elements is the most common method of creating layouts in HTML. The <div>
(stands for division) element is used for marking out a block of content, or set of other elements
inside an HTML document. It can contain further other div elements if required.
HTML5 has introduced the new structural elements such as <header>, <footer>, <nav>,
<section>, etc. to define the different parts of a web page in a more semantic way. You can
consider these elements as a replacement for commonly used classes such
as .header, .footer, .nav, .section, etc.
The following table provides a brief overview of new HTML5 structural elements.
Tag Description
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code is executed on the server, and the result is returned to the browser as plain HTML
PHP can create, open, read, write, delete, and close files on the server
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
A variable starts with the $ sign, followed by the name of the variable
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _
)
Variable names are case-sensitive ($age and $AGE are two different variables)
Examples –
<?php
$txt = "Hello world!"; //string
$x = 5; //integer
$y = 10.5; //float
?>
Output Variables
The PHP echo statement is often used to output data to the screen. The following example will
show how to output text and a variable:
<?php
$txt = "ICT"; I love ICT!
echo "I love $txt!";
?>
<?php
$txt = "ICT";
I love ICT!
echo "I love " . $txt . "!";
?>
<?php
$x = 5;
$y = 4; 9
echo $x + $y;
?>
PHP Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical
operations, such as addition, subtraction, multiplication etc.
% Modulus $x % $y Remainder of $x
divided by $y
The PHP increment operators are used to increment a variable's value. The PHP decrement
operators are used to decrement a variable's value.
The PHP comparison operators are used to compare two values (number or string):
Or Or $x or $y True if either $x or $y
is true
||
Very often when you write code, you want to perform different actions for different conditions.
You can use conditional statements in your code to do this.
if...else statement - executes some code if a condition is true and another code if that
condition is false
if...elseif...else statement - executes different codes for more than two conditions
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
Example
Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the
current time is less than 20. Otherwise it will output "Have a good night!":
<?php
$t = date("H");
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated
once. The value of the expression is then compared with the values for each case in the structure. If
there is a match, the block of code associated with that case is executed. Use break to prevent the
code from running into the next case automatically. The default statement is used if no match is
found.
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Loops
Often when you write code, you want the same block of code to run over and over again a certain
number of times. So, instead of adding several almost equal code-lines in a script, we can use
loops.
Loops are used to execute the same block of code again and again, as long as a certain condition
is true.
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
Besides the built-in PHP functions, it is possible to create your own functions.
In the example below, we create a function named "writeMsg()". The opening curly brace ( { )
indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of
the function. The function outputs "Hello world!". To call the function, just write its name followed
by brackets ():
<?php
function writeMsg() {
echo "Hello world!";
}
Information can be passed to functions through arguments. An argument is just like a variable.
familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");
?>
PHP Arrays
What is an Array?
An array is a special variable, which can hold more than one value at a time, and you can access
the values by referring to an index number.
<?php
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Output-
The count() function is used to return the length (the number of elements) of an array
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Output-
3
PHP Form Handling
The PHP superglobals $_GET and $_POST are used to collect form-data. The method attribute
specifies how to send form-data (the form-data is sent to the page specified in
the action attribute).
Get Post
History Parameters remain in browser history Parameters are not saved in
because they are part of the URL browser history.
Bookmarked Can be bookmarked. Can not be bookmarked.
BACK GET requests are re-executed but may The browser usually alerts the
button/re- not be re-submitted to server if the user that data will need to be re-
submit HTML is stored in the browser cache. submitted.
behaviour
Encoding type application/x-www-form-urlencoded multipart/form-data or
(enctype application/x-www-form-
attribute) urlencoded Use multipart
encoding for binary data.
Parameters can send but the parameter data is Can send parameters, including
limited to what we can stuff into the uploading files, to the server.
request line (URL). Safest to use less than
2K of parameters, some servers handle
up to 64K
Restrictions on Yes, only ASCII characters allowed. No restrictions. Binary data is
form data type also allowed.
Security GET is less secure compared to POST POST is a little safer than GET
because data sent is part of the URL. So because the parameters are not
it's saved in browser history and server stored in browser history or
logs in plaintext. in web server logs.
Restrictions on Yes, since form data is in the URL and No restrictions
form data URL length is restricted. A safe URL
length length limit is often 2048 characters but
varies by browser and web server.
Usability GET method should not be used when POST method used when
sending passwords or other sensitive sending passwords or other
information. sensitive information.
Visibility GET method is visible to everyone (it will POST method variables are not
be displayed in the browser's address displayed in the URL.
bar) and has limits on the amount of
information to send.
Cached Can be cached Not cached
PHP MySQL Insert Data
We will create a table named "MyGuests", with five columns: "id", "firstname", "lastname",
"email" and "reg_date":
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
PHP MySQL Select Data
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Output
First, we set up an SQL query that selects the id, firstname and lastname columns from the
MyGuests table. The next line of code runs the query and puts the resulting data into a variable
called $result.Then, the function num_rows() checks if there are more than zero rows returned.If
there are more than zero rows returned, the function fetch_assoc() puts all the results into an
associative array that we can loop through. The while() loop loops through the result set and
outputs the data from the id, firstname and lastname columns.
PHP MySQL Select Data (Using Table)
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " .
$row["lastname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
Excersices
What would be the output when the following HTML,CSS or PHP codes are executed?
1) 2)
<?php <?php
$x = 5 /* + 15 */ + 5; $txt = "ICT";
echo $x; echo "I love $txt!";
?> ?>
3) 4)
<?php <?php
$txt = "ICT"; $var1 = "Hello";
echo "I love " . $txt . "!"; $var2 = "World";
?> echo "$var1$var2";
?>
5) 6)
<?php <?php
$t = date("H"); //$t=20 $x = 1;
<?php <?php
$x = 0; $x = 6;
<?php <?php
for ($x = 0; $x <= 100; $x+=10) { $colors = array("red", "green", "blue", "yellow");
echo "The number is: $x <br>";
} foreach ($colors as $value) {
?> echo "$value <br>";
}
?>
11) 12)
<?php <?php
for ($x = 0; $x < 10; $x++) { for ($x = 0; $x < 10; $x++) {
if ($x == 4) { if ($x == 4) {
break; continue;
} }
echo "The number is: $x <br>"; echo "The number is: $x <br>";
} }
?> ?>
13) 14)
<?php <?php
$cars = array("Volvo", "BMW", "Toyota"); $cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . echo count($cars);
"."; ?>
?>
15) 16)
<?php <?php
function familyName($fname) { function familyName($fname, $year) {
echo "$fname Refsnes.<br>"; echo "$fname Refsnes. Born in $year <br>";
} }
<?php <?php
$favcolor = "red"; $x = 10;
$y = 3;
switch ($favcolor) {
case "red": echo $x ** $y;
echo "Your favorite color is red!"; ?>
break;
case "blue":
echo "Your favorite color is blue!"; <?php
break; $x = 10;
case "green": $y = 6;
echo "Your favorite color is green!";
break; echo $x % $y;
default: ?>
echo "Your favorite color is neither red, blue, nor
green!";
}
?>
19) 20)
<?php <?php
$x = 10; $x = 100;
echo ++$x; $y = "100";
?>
var_dump($x == $y);
<?php ?>
$x = 10;
echo $x++; <?php
?> $x = 100;
$y = "100";
<?php
$x = 10; var_dump($x === $y);
echo --$x; ?>
?>
<?php
$x = 10;
echo $x--;
?>
21) 22)
Fill the blanks. Note - add a new record to the Fill the blanks. Note - add a new record to the "Student"
"MyGuests" table, with 3 columns: "firstname", table, with 3 columns: "firstname", "lastname", "email".
"lastname", "email". User Name:root, User Name:root, Password:12#$%,Dbname:school
Password:12#$%,Dbname:mydb
<?php
<?php
$conn =
$conn = new mysqli_connect("localhost",.................................................
mysqli("localhost",........................................................... .);
.);
if (!$conn) {
if ($conn->connect_error) { die("Connection failed: " . mysqli_connect_error());
die("Connection failed: " . $conn->connect_error); }
}
$sql = "INSERT INTO ..........................
$sql = "INSERT INTO (........................................................................................)
.................(....................................................................... VALUES ('Aruna', 'Perera', 'Aruna@gmail.com')";
)
VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
if ($conn->query($sql) === TRUE) { } else {
echo "New record created successfully"; echo "Error: " . $sql . "<br>" . mysqli_error($conn);
} else { }
echo "Error: " . $sql . "<br>" . $conn->error;
} mysqli_close($conn);
?>
$conn->close();
?>
23) 24)
MyGuests table
id firstname lastname <style>
1 John Doe table, th, td {
border: 1px solid black;
2 Mary Moe
}
3 Julie Dooley </style>
<?php
<?php
$conn = new
$conn = new mysqli("localhost","admin","15%#","mydb");
mysqli("localhost","admin","15%#","mydb");
if ($conn->connect_error) {
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);
die("Connection failed: " . $conn->connect_error); }
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql);
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if ($result->num_rows > 0) { echo "<table><tr><th>ID</th><th>Name</th></tr>";
while($row = $result->fetch_assoc()) { while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . echo "<tr><td>" . $row["id"]. "</td><td>" .
$row["firstname"]. " " . $row["lastname"]. "<br>"; $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
} }
} else { echo "</table>";
echo "0 results"; } else {
} echo "0 results";
$conn->close(); }
?>
$conn->close();
?>
25) 26)
<html> <html>
<head> <head>
<style> <style>
p.center { h1, h2, p {
text-align: center; text-align: center;
color: red; color: red;
} }
</style>
p.large { </head>
font-size: 300%; <body>
}
</style> <h1>Hello World!</h1>
</head> <h2>Smaller heading!</h2>
<body> <p>This is a paragraph.</p>
</body>
</html>
27) 28)
<html> <html>
<head> <head>
<style> <style>
#para1 { *{
text-align: center; text-align: center;
color: red; color: blue;
} }
</style> </style>
</head> </head>
<body> <body>
</body>
</html>
29) 30)
<html> <html>
<head> <head>
<style> <style>
table, th, td { table, th, td {
border: 1px solid black; border: 1px solid black;
border-collapse: collapse; border-collapse: collapse;
} }
</style> </style>
</head> </head>
<body> <body>
<form>
<label>
Course :
</label>
<select name=”course”>
<option>Course</option>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option>
<option value="B.Tech">B.Tech</option>
</select>
<br>
<br>
<label>
Gender :
</label><br>
<input type="radio" value="male" name=”gtype”/> Male
<br>
<input type="radio" value="female" name=”gtype”/>
Female <br>
<br>
<br>
<label>
Phone :
</label>
<input type="text" name="country code" value="+91"
size="2"/>
<input type="text" name="phone" size="10"/> <br> <br>
Address
<br>
<textarea cols="80" rows="5" value="address">
</textarea>
<br> <br>
<input type="button" value="Submit"/>
</form>