Exercise : Results Table

Take your query from one of the previous problems and put it into a PHP page that displays the results using a table. (See next slides.)

LanguagePercentage
English86.2
Spanish7.5
French0.7

Recall: Querying a Database in PHP with PDO

$name = new PDO("dbprogram:dbname=database;host=server", username, password);
$name->query("SQL query");
# connect to world database on local server
$db = new PDO("mysql:dbname=world;host=localhost", "traveler", "packmybags");
$db->query("SELECT * FROM countries WHERE population > 100000000;");

Recall: Result rows with query

$db = new PDO("dbprogram:dbname=database;host=server", username, password);
$rows = $db->query("SQL query");
foreach ($rows as $row) {
	do something with $row;
}

HTML tables: <table>, <tr>, <td>

A 2D table of rows and columns of data (block element)

<table>
	<tr><th>Col 1</th><th>Col 2</th></tr>
	<tr><td>r1c1</td><td>r1c2 hello :-)</td></tr>
	<tr><td>r2c1 is very wide!</td><td>r2c2</td></tr>
</table>

Styling tables

table { border: 4px dotted blue; text-align: right; }
td    { border: 3px dashed red; width: 30%; padding: 10pt; }
Column 1Column 2
1,11,2 okay
2,1 real wide2,2