Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [240]

By Root 1387 0
{

float: left;

}

dl {

float: left;

clear: left;

}

$conn = mysql_connect(“localhost”,”user”,”password”) or die(mysql_error());

mysql_select_db(“xfd”);

$sql = “SELECT * FROM contact”;

$result = mysql_query($sql, $conn) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){

print “

\n”;

foreach ($row as $name => $value){

print “

$name
\n”;

print “

$value
\n”;

} // end foreach

print “

\n”;

} // end while

?>

The general design is copied from contact.html, with the following changes:

1. Add CSS styling for the definition list.

Definition lists are great for this kind of data, but the default style is pretty boring. I added some float styles to make the data display better. (See Book III, Chapter 1 for how to use floating styles.)

2. Put each record in its own definition list.

The while loop executes once per record, so begin the definition list at the beginning of the while loop. The tag goes at the end of the while loop. Each pass of the while loop creates a new definition list.

3. Display the field names as

elements.

The field name maps pretty well to the concept of a definition term, so put each $name value in a

pair.

4. Place the values inside

tags.

The values will be displayed as definition data. Now, you have the contents of the data set up in a form that can be easily modified with CSS.


Using XHTML tables for output

The basic unit of structure in SQL is called a table because it’s usually displayed in a tabular format. XHTML also has a table structure, which is ideal for outputting SQL data. Figure 7-3 shows contactTable.php, which displays the contact information inside an XHTML table.

Figure 7-3: The contact information displayed in an XHTML table.

Tables are a very common way to output SQL results. There’s one big difference between table output and the techniques that have been shown elsewhere in this chapter. In a table, you have a separate row containing field names. Here’s the code:

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

contactTable.php

My Contacts

$conn = mysql_connect(“localhost“, “user“, “password“);

mysql_select_db(“xfd“);

$sql = “SELECT * FROM contact“;

$result = mysql_query($sql, $conn);

print “ \n“;

//get field names first

print “

\n“;

while ($field = mysql_fetch_field($result)){

print “

\n“;

} // end while

print “

\n“;

while ($row = mysql_fetch_assoc($result)){

print “

\n“;

foreach ($row as $name => $value){

print “

\n“;

} // end foreach

print “

\n“;

} // end while loop

print “

$field->name
$value
\n“;

?>

You might be confused that I’m using a table here, seeing as how I argue pretty strongly against use of tables for page layout in the HTML and CSS minibooks. Tables aren’t evil: they just aren’t designed to be a page layout mechanism. Tables, however, are designed to display tabular data, and the result of a data query is pretty much the definition of tabular data. You can (and should) still use CSS for specific layout details of the table. Tables are fine when used to present data, which is what I’m doing here.

This code is still very similar to the basic contact.php program. It extracts data from the database exactly the same way. The main difference is how field names are treated. The field names will go in table headings, and only the values are printed from each row. To make this work, follow these steps:

1. Build a normal MySQL connection.

Begin with the standard connection.

Return Main Page Previous Page Next Page

®Online Book Reader