Presentation is loading. Please wait.

Presentation is loading. Please wait.

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.

Similar presentations


Presentation on theme: "_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the."— Presentation transcript:

1 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the book authors, 2002 PHP Bible Chapter 16: Displaying Queries In Tables

2 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition2  Wiley and the book authors, 2002 Summary Mapping database tables to HTML tables Reusable functions for table display using SELECT Displaying complex relational table data

3 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition3  Wiley and the book authors, 2002 Displaying queries in tables Much of the point of PHP is to help you translate between a back-end database and its front-end presentation on the Web Data can be viewed, added, removed, and tweaked as a result of your Web user's actions The 2 big productivity points from this chapter are:  Reuse functions in simple cases. The problem of database table display shows up over and over in database-enabled site design. If the display is not complicated, you should be able to throw the same simple function at the problem rather than reinventing the wheel with each PHP page you write  Choose between techniques in complex cases. You may find yourself wanting to pull out a complex combination of information from different tables (which, of course, is part of the point of using a relational database to begin with). You may not be able to map this into a simple reusable function, but there aren't that many novel solutions either – get to know the alternatives, and you can decide how to trade off efficiency, readability, and your own effort

4 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition4  Wiley and the book authors, 2002 HTML tables and database tables One-to-one mapping  HTML tables are really constructed out of rows ( ), and columns have no independent existence – each row has some number of table data items ( ) which will produce a nice rectangular array only if there are the same number of TDs for every TR within a TABLE  By contrast, fields (aka columns) in database tables are the more primary entity – defining a table means defining the fields, and then you can add as many rows as you like  The simplest case of display is where the structure of a database table or query does correspond to the structure of the HTML table we want to display – the database entity has m columns and n rows, and we'd like to display an m-by-n rectangular grid in the user's browser window, with all the cells filled in appropriately

5 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition5  Wiley and the book authors, 2002 HTML tables and database tables (cont.) Example: a single-table displayer  So let's write a simple translator that queries the database for the contents of a single table and displays the results on screen. Here's the top-down outline of how the code will get the job done: 1. Establish a database connection ( mysql_connect & mysql_select_db ) 2. Construct a query to send to the database (SELECT) 3. Send the query and hold on to the result id ( mysql_query ) 4. Start outputting an HTML table ( ) 5. Loop through the database result rows, printing an HTML table row tag set ( ) at the beginning and ending of each loop 6. On the first execution of the loop, if desired, print out the field names 7. In each row, retrieve the successive fields and display them wrapped in a table data cell ( ) 8. Close the HTML table ( )  We'd like to wrap all the preceding steps into a handy function that we can use whenever we want to

6 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition6  Wiley and the book authors, 2002 HTML tables and database tables (example) <?php include ('/home/php/db_vars.inc'); function display_db_table($table,$db_res,$criteria='',$field_headings=true) { $sql = 'SELECT * FROM '.$table.' '.$criteria; $result_id = mysql_query($sql,$db_res) or die ('ERROR executing query'); print(' '); if ($field_headings) $heading_printed = false; else $heading_printed = true; while ($row = mysql_fetch_assoc($result_id)) { if (!$heading_printed) { print (' '); foreach ($row as $field_name => $val) print (' '.$field_name.' '); print (' '); $heading_printed = true; } print (' '); foreach ($row as $val) print (' '.$val.' '); print (' '); } print (' '); }

7 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition7  Wiley and the book authors, 2002 HTML tables and database tables (example) $db_resource = mysql_connect($hostname,$username,$password); mysql_select_db($db_name,$db_resource); if (isset($_GET['category'])) { $category_num = $_GET['category']; $sql = 'SELECT description FROM categories WHERE (category_id = '.$category_num.')'; $result_id = mysql_query($sql,$db_resource); $row = mysql_fetch_row($result_id); $category = $row[0]; $criteria = 'WHERE (category='.$category_num.')'; } else { $category = 'All Products'; $criteria = ''; } ?> Available products in <?php print (' '.$category.' '); display_db_table('catalog',$db_resource,$criteria); ?>

8 _______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition8  Wiley and the book authors, 2002 HTML tables and database tables (cont.) Some things to notice about the preceding script:  Although the script refers to specific database tables, the display_db_table() function itself is general. You could put the function definition in an include file and use it anywhere you want  The first thing the script does is load an include file that contains the variable assignments for the database name, username, password, and server name  In the function, we use a while loop for printing rows, and a foreach loop to print the individual fields from each row  The main while loop reflects a very common idiom, which exploits the fact that the value of a PHP assignment statement is the condition. $row will either contain an array of the record returned from the SELECT statement or the value FALSE  The main body of the function shows a relationship between the catalog table and the categories table where the catalog.category field is a foreign key "pointing to" the categories.category_id field  The main body also looks for a category GET variable which could be entered into a URI in an HTML hyperlink ( ) or typed into the address line


Download ppt "_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the."

Similar presentations


Ads by Google