Chapter 1
Chapter 1
DATA-DRIVEN
WEBSITES
CREATING
DATA-DRIVEN
WEBSITES
An Introduction to HTML, CSS,
PHP, and MySQL
BOB TERRELL
Creating Data-Driven Websites: An Introduction to HTML, CSS, PHP,
and MySQL
10 9 8 7 6 5 4 3 2 1
KEYWORDS
Acknowledgments ix
Introduction xi
Chapter 1 HTML 1
Fundamentals 3
Tags and Elements 4
Attributes and Properties 5
Block Elements 6
Inline Elements 7
Form Elements 8
Entities 9
Comments 11
Project 11
Further Reading 14
Chapter 2 Cascading Style Sheets (CSS) 15
CSS Syntax 17
Selectors 17
The Cascade 21
Importance 21
Specificity 22
Code Order 23
Properties 23
Colors 23
Length Units 25
Property List 25
The Box Model 34
Comments 37
Working with Difficult Browsers 37
Project 38
Further Reading 47
Chapter 3 MySQL 49
Why MySQL? 49
phpMyAdmin 50
The Structure of MySQL 50
viii • CONTENTS
I would like to thank my wife for her patience and understanding and for
occupying the time of three children long enough to write this book; Lisa
MacLean, for being my favorite professor; my parents, for always seeing
the potential in me and wanting the best for me; and fate, for life has
given me many blessings and opportunities arising from circumstances
that reach far beyond my own actions.
Introduction
The purpose of this book is to teach you, the reader, the basics of web de-
velopment. Web development is made up of many different parts and uses
many different languages. The languages that this book uses are HTML,
CSS, PHP, and MySQL. These languages, and the software to interpret them,
are freely available and can run on Windows, macOS, and Linux, making
them a natural choice for the beginner.
PROJECT
c. Handle suggestions.
1. Allow users to create a suggestion.
2. Display a list of suggestions.
3. Display details about a particular suggestion.
4. Allow users to comment on a suggestion before voting, in case
clarification is needed.
5. Allow users to vote on a suggestion.
We may find as we proceed that even this isn’t quite detailed enough,
but it will be sufficient for now.
BEFORE WE BEGIN...
As part of the project in this book, you will need a host with PHP, MySQL,
and your choice of web server software. Apache is common on Mac and
Linux; Windows users may use IIS (Internet Information Services). The
host need not be from a web services provider; your personal computer is
sufficient to run all of these. If you do not have a host set up for you, search
for a guide to help you install these software packages on your machine.
CHAPTER 1
HTML
Save the file to your web root with the name hi.html (Figure 1.2).
View the results in your web browser of choice. Enter in the URL for
your web root, and add hi.html to the end (Figure 1.3).
You’re done!
FUNDAMENTALS
So what’s really going on here? The short answer is it is the browser’s job
to make as much sense of an HTML file as it can. We can see what it has
done by inspecting the web page. In Chrome: View -> Developer -> De-
veloper Tools. In the window or pane that opens, select the Elements tab.
We see that the browser has taken our file, which the web server has told it
is an HTML file (thanks to the.html extension) and added to it. We should
see something like the following (Figure 1.4):
The document is located on the left. We see our Hello, world! text on
the left. What’s the rest of that?
An HTML file is rendered by the browser into a document with
elements. Note the three elements in document: the <html> element,
which encompasses the entire document; the <head> element, which is
currently empty and provides information about the document; and the
<body> element, which contains the part of the document that the web
browser is intended to render. Because our file contained only plain text,
4 • CREATING DATA-DRIVEN WEBSITES
the browser created enough elements for our document to make sense. It
put the contents of our file into the <body> element.
Revisit the document in the text editor and add in some of the ele-
ments the browser filled in on its own. Change the document to be as such:
<html>
<head></head>
<body>Hello, world!</body>
</html>
Save the file and refresh the browser window. It should look pretty
much the same! Only this time, the browser is reading our file and parsing
(making sense of) it.
Note the differences. The </u> closing tag closes the <u> element,
which was the last one opened, followed by </b> to end the <b> opening
tag. We also properly closed the <p> tag with the closing </p> tag. Don’t
worry if you aren’t familiar with what these tags do! They’ll be explained
soon enough. For now, simply note what it means to have well-formed
HTML: that tags are closed in reverse order that they are opened.
BEST PRACTICE
Although web browsers give their best effort to make sense of an HTML
file, throughout this book we will endeavor to generate well-formed
documents. A well-formed document is one which always closes
HTML tags and closes them in the reverse of the order opened. HTML
documents that follow this guideline are easier for humans to read and
understand, and following this practice makes it harder to make a mis-
take in which the web browser becomes legitimately confused.
The HTML tags covered so far are tags at their most basic. They open an
element, and they close an element. Tags, however, can also have attrib-
utes, which define an element’s properties. (Sometimes, properties can be
changed on an HTML document. To be more precise, attributes define an
element’s initial properties.) The attributes that can be used in a tag vary
depending upon the tag type, but nearly every html tag can contain the at-
tributes id and class. Consider the following:
In the code above, we’ve added the id attribute, with the value myId,
and the class attribute, with the value myClass. These become properties
of the rendered p element. The id and class attributes don’t do anything on
their own, but we’ll be revisiting them later.
BLOCK ELEMENTS
Let’s take a moment to practice using these elements. Put the follow-
ing in a document.
<html>
<head></head>
<body>
<p>This text is inside a paragraph tag.
HTML • 7
INLINE ELEMENTS
Unlike block elements, inline elements do not create a new line. They exist
within the flow of the content around them. Inline elements should not
8 • CREATING DATA-DRIVEN WEBSITES
contain block elements, but may contain other inline elements. Most inline
elements make use of their attributes to help define their behavior. The
following are the most common inline elements:
• b, i, u: These elements simply make the text inside them bold, italic,
or underlined, respectively. It’s often easier and better to use CSS
for this (more later), though there’s no harm in using these to itali-
cize a book title, for example.
• em, strong: Short for emphasis and strong. These elements be-
have much like i and b, respectively, with the added bonus that
text-to-speech screen readers will read them differently. Whenever
you want to draw attention to words, you should prefer these ele-
ments to simply making the text bold or italic.
• a: Short for anchor tag. This tag is used to generate links to other
documents. Its notable attribute is href, the value of which is the
path on the server to the other document, relative to the current one.
An absolute path may also be used if linking to another site.
• img: This is the image tag, used to include an image in your docu-
ment. img typically uses the following attributes: src specifies the
source of the image. Much like the a tag, this is the path to the image
file, relative to the current document, or an absolute path if including
an image from elsewhere. alt specifies alternative text to display if the
image can’t be displayed. height and width are both optional. If used,
they are the height and width the image will be displayed, in pixels.
• span: This is the inline equivalent of div. It does nothing on its own,
but it can be used to mark off a section of the document to be used
by something else, such as CSS.
FORM ELEMENTS
In the earliest days of the Internet, websites were simply pages up for display.
You read them, you clicked around, and you went on your merry way. That’s
hardly the case nowadays, and HTML forms are a big reason why. Forms
allow data to be sent to the server to be processed. Forms can be used for
search fields, or for registering at a site. Any time a site submits user-provided
data back to the server, it’s likely to be using a form. On to the form elements:
ENTITIES
One last topic of note in the world of HTML is entities. Recall that HTML
tags are the < symbol, followed by a tag name, and any attributes are part
10 • CREATING DATA-DRIVEN WEBSITES
of the tag until the closing > character. This seems pretty straightforward,
but what if we need a web page to say the following?
At first glance, the problem may not be apparent, but put that in a
document and view it in a browser. All we see is if a. What happened?
Well, the <b and everything after it was treated as a tag. That’s not what
we wanted! Thankfully, HTML provides us with a way to output special
HTML characters without having them treated as tags: entities. The six
most common entities are listed in the table below:
COMMENTS
Comments may be used inside HTML. A comment begins with the string
<!-- and ends with -->. It may span multiple lines if necessary. HTML
comments are sent to the browser, but the browser ignores their contents.
However, because they are sent to the browser, they aren’t used very often;
comments are for you, not for the users of your site. Nevertheless, if there’s
a “secret” message you want to send to your users, an HTML comment
would be a fun place to put it.
The above would appear when a user viewed the source of your web
page, but it wouldn’t appear on the page when the browser renders it.
PROJECT
<html>
<head>
<title>Add User</title>
</head>
12 • CREATING DATA-DRIVEN WEBSITES
<body>
<p>Please add a new user below.</p>
<form action=“user-save.php” method=“post”>
<p>Username: <input type=“text” name=
“name”/></p>
<p>Password: <input type=“text” name=
“pass”/></p>
<p>First Name: <input type=“text” name=
“first”/></p>
<p>Last Name: <input type=“text” name=
“last”/></p>
<p>Title: <input type=“text” name=
“title”/></p>
<p><input type=“submit” name=“action”
value=“Save”/> <input type=“submit”
name=“action” value=“Cancel”/></p>
</form>
</body>
</html>
<html>
<head><title>Add Suggestion</title></head>
<body>
<p>Please add a suggestion for the Style Guide
below.</p>
<form action=“suggestion-save.php”
method=“post”>
<p>Section:
<select name=“section”>
<option>General</option>
<option>HTML</option>
<option>CSS</option>
<option>PHP</option>
<option>SQL</option>
</select></p>
HTML • 13
And finally, the index page. Our hypothetical management has de-
cided that our page should have our company logo and name. They have
provided us with a logo file to use: a white pen on a transparent back-
ground. Prominently displayed should be the hypothetical company name:
Pendity Software. Put the following into a file named index.html:
<html>
<head><title>Pendity Software Style Guide
</title></head>
<body>
<div><img src=“logo.png”/><p>Pendity
Software</p></div>
<div><h1>Pendity Software</h1>
<h2>Style Guide</h2></div>
<div>
<p><a href=“suggestion-new.html”>New
Suggestion</a></p>
<p>Admin Only!<br/>
<a href=“user-new.html”>New User</a>
</p></div>
<div>This is the page for the style guide.
</body>
</html>
Obviously, the index page is incomplete. We’ll get back to that. For
now, let’s see how it looks.
First, the index (Figure 1.5):
Wow. That looks terrible. At least the links are there. Click through
to each of those pages. (You will need to use the browser’s Back button to
14 • CREATING DATA-DRIVEN WEBSITES
return to the index page. What happens if you try to submit either form?)
It’s time to move on to the next chapter.
FURTHER READING
This section encompasses only a very small subset of the complete HTML spec.
It contains the most basic explanation of the most common HTML elements
and ideas.
The full HTML 5 specification can be found here:
https://www.w3.org/html/
The Mozilla Development Network has many tutorials and references for HTML:
https://developer.mozilla.org/en-US/docs/Web/HTML
Index
Objects, 80 session_start(), 95
Opening tag, 4 SET, 56
Operators Size properties, 28–29
arithmetic, 81 SMALLINT, 53
assignment, 82 Specificity, 22
comparison, 82 Strings
increment, 82 in MySQL, 54
logical, 83 in PHP, 81–82
string, 80 SUBSTRING(), 71
ORDER BY clause, 64–65, 72 Superglobal, 93–94
Ordered list, 6–7 switch (and case), 85
Overflow, 32 <table> element, 6–7
Momentum Press is one of the leading book publishers in the field of engineering,
mathematics, health, and applied sciences. Momentum Press offers over 30 collections,
including Aerospace, Biomedical, Civil, Environmental, Nanomaterials, Geotechnical,
and many others.
Momentum Press is actively seeking collection editors as well as authors. For more
information about becoming an MP author or collection editor, please visit
http://www.momentumpress.net/contact
The Momentum Press digital library is very affordable, with no obligation to buy in future years.