HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [241]
2. Print the table tag before extracting any results.
All the query data will be displayed inside the table, so print the table tag before you start printing anything that should go inside the table.
3. Print the table header row first.
The table headers must be printed before you can worry about the other data:
//get field names first
print “
while ($field = mysql_fetch_field($result)){
print “
} // end while
print “
4. Extract metadata from the result set with mysql_fetch_field().
After you get a result from a data query, you can learn a lot about the data by using the mysql_fetch_field() function:
while ($field = mysql_fetch_field($result)){
This function (mysql_fetch_field()) extracts the next field object from the result and passes it to a variable called $field. It returns false if there are no more fields in the result, so it can be used in a while loop, like mysql_fetch_assoc().
5. Print the field’s name.
The field is an object, so you can extract various elements from it easily. In this case, I’m interested in the field name. $field->name yields the name of the current field. (See the nearby sidebar, “More about metadata,” for more information about information you can extract from field objects.)
print “
6. Print each row’s data as a table row.
Each row of the data result maps to a table row. Use the preceding variation of nested loops to build your table rows.
7. Finish off the table.
The table tag must be completed. Don’t forget to print when you’re done printing out all the table information.
8. Clean up your XHTML.
Check your code in a browser. Make sure it looks right, but don’t stop there. Check with a validator to make sure that your program produces valid XHTML code. View the source and ensure that the indentation and white space are adequate. Even though a program produced this code, it needs to be XHTML code you can be proud of.
More about metadata
You can find out all sorts of information about the table you’re querying with the mysql_fetch_field() function.
This function returns an object that has the following properties:
table: The name of the table the field (column) belongs to.
name: The field’s name.
type: The field’s datatype.
primary_key: If the field is a primary key, will return a 1.
unique_key: If the field is a unique key, will return a 1.
max_length: The field’s maximum length.
def: The field’s default value (if any) .
not_null: If the field can’t be NULL, will return a 1.
multiple_key: If the field is a non-unique key, will return a 1.
numeric: If the field is numeric, will return a 1.
blob: If the field is a blob, will return a 1.
unsigned: If the field is unsigned, will return a 1.
zerofill: If the field is zero-filled, will return a 1.
You’ll probably end up using table, name, type, primary_key, and max_length the most. Refer to any of these values using object-oriented syntax, so if you have a field named $field, get its name by using $field->name.
Allowing User Interaction
If you have a large database, you probably want to allow users to search the database. For example, the form in Figure 7-4 allows the user to search the My Contacts database.
Figure 7-4: The user can check for any value in any field.
Here are a couple of interesting things about the form in Figure 7-4:
♦ The search value can be anything. The first field is an ordinary text field. The user can type absolutely anything here, so you should expect some surprises.
♦ The user selects a field with a drop-down menu. You don’t expect the user to know exactly what field names you are using in your database. Whenever possible, supply this type of information in a format that’s easier for the user and less prone to error.
♦ This form is built to fill in a query. The back-end program (search.php) will be constructing a query from data