HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [239]
Extracting fields from a row
Each time you go through the while loop described in the previous section, you’ll have a variable called $row. This will be an associative array containing all the field names and values from the current form. If you know a field name, you can access it directly. For example, I know the contact table has a field named company, so you can use an associative array lookup to see the current company name:
print $row[“company”];
This will print the company name of the current record.
More often, you will want to print out all the information in the row, so you can use the special form of foreach() loop used with associative arrays:
foreach ($row as $name => $value){
Here’s how it works:
1. Set up the foreach loop.
This form of for loop automatically loads variables with members of the array:
foreach ($row as $name => $value){
2. Analyze the $row array.
The $row variable contains an associative array, so it’s a perfect candidate for this type of loop:
foreach ($row as $name => $value){
3. Assign each key to $name.
On each pass of the loop, assign the current key of the array (which will contain the current field name) to the variable $name:
foreach ($row as $name => $value){
4. Indicate the relationship between $name and $value with the => operator.
This indicates that $name is the key and that $value is the value in this relationship:
foreach ($row as $name => $value){
5. Assign the value to $value.
The value of the current element will be placed in the $value variable:
foreach ($row as $name => $value){
When you use a foreach loop with an associative array, you assign each element to two variables because each element in an associative array has a name and a value. Check Chapter 4 of this minibook for more information about associative arrays and the foreach loop.
Inside this loop, you’ll have the name of the current field in a variable called $name and its value in a variable called $value. This loop will continue for each field in the current record.
Printing the data
For this simple example, I’m using the simplest way I can think of to print out the contents:
print “$name: $value
\n”;
This line simply prints out the current field name, followed by a colon and the current field value. Because this simple line is inside the complex nested loops, it ends up printing the name and value of every field in the query result. Here’s the whole chunk of code again:
while($row = mysql_fetch_assoc($result)){
foreach ($row as $name => $value){
print “$name: $value
\n”;
} // end foreach
print “
\n”;
} // end while
The result isn’t the most elegant formatting on the Internet, but it gets the job done, and it’s easy to understand. Note I added a
tag at the end of each line, so each field will appear on its own line of XHTML output. I also added a final
at the end of each for loop. This will cause a line break between each record so that the records are separated.
Improving the Output Format
Using
tags for output is a pretty crude expedient. It’s fine for a basic test, but
tags are usually a sign of sloppy XHTML coding. Take a look at this variation called contactDL.php in Figure 7-2.
Figure 7-2: Now, the output of the query is in a nice definition list.
Building definition lists
Definition lists are designed for name/value pairs, so they’re often a good choice for data in associative arrays. It’s not too difficult to convert the basic data result program (shown in the following code) into a form that uses definition lists:
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>