'; * $qp = qp(QueryPath::HTML_STUB, 'body') // Open a stub HTML doc and select * ->append('
') * ->dbInit($this->dsn) * ->queryInto('SELECT * FROM qpdb_test WHERE 1', array(), $template) * ->doneWithQuery() * ->writeHTML(); * ?> * @endcode * * The code above will take the results of a SQL query and insert them into a n * HTML table. * * If you are doing many database operations across multiple QueryPath objects, * it is better to avoid using {@link QPDB::dbInit()}. Instead, you should * call the static {@link QPDB::baseDB()} method to configure a single database * connection that can be shared by all {@link QueryPath} instances. * * Thus, we could rewrite the above to look like this: * @code * '; * $qp = qp(QueryPath::HTML_STUB, 'body') // Open a stub HTML doc and select * ->append('
') * ->queryInto('SELECT * FROM qpdb_test WHERE 1', array(), $template) * ->doneWithQuery() * ->writeHTML(); * ?> * @endcode * * Note that in this case, the QueryPath object doesn't need to call a method to * activate the database. There is no call to {@link dbInit()}. Instead, it checks * the base class to find the shared database. * * (Note that if you were to add a dbInit() call to the above, it would create * a new database connection.) * * The result of both of these examples will be identical. * The output looks something like this: * * @code * * * * * Untitled * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Title 0Body 0Footer 0
Title 1Body 1Footer 1
Title 2Body 2Footer 2
Title 3Body 3Footer 3
Title 4Body 4Footer 4
* * * @endcode * * Note how the CSS classes are used to correlate DB table names to template * locations. * * * @author M Butcher * @license http://opensource.org/licenses/lgpl-2.1.php LGPL or MIT-like license. * @see QueryPathExtension * @see QueryPathExtensionRegistry::extend() * @see QPDB */ /** * Provide DB access to a QueryPath object. * * This extension provides tools for communicating with a database using the * QueryPath library. It relies upon PDO for underlying database communiction. This * means that it supports all databases that PDO supports, including MySQL, * PostgreSQL, and SQLite. * * Here is an extended example taken from the unit tests for this library. * * Let's say we create a database with code like this: * @code *db = new PDO($this->dsn); * $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); * $this->db->exec('CREATE TABLE IF NOT EXISTS qpdb_test (colOne, colTwo, colThree)'); * * $stmt = $this->db->prepare( * 'INSERT INTO qpdb_test (colOne, colTwo, colThree) VALUES (:one, :two, :three)' * ); * * for ($i = 0; $i < 5; ++$i) { * $vals = array(':one' => 'Title ' . $i, ':two' => 'Body ' . $i, ':three' => 'Footer ' . $i); * $stmt->execute($vals); * $stmt->closeCursor(); * } * } * ?> * @endcode * * From QueryPath with QPDB, we can now do very elaborate DB chains like this: * * @code * * ->append('

') // Add

* ->children() // Select the

* ->dbInit($this->dsn) // Connect to the database * ->query($sql, $args) // Execute the SQL query * ->nextRow() // Select a row. By default, no row is selected. * ->appendColumn('colOne') // Append Row 1, Col 1 (Title 0) * ->parent() // Go back to the * ->append('

') // Append a

to the body * ->find('p') // Find the

we just created. * ->nextRow() // Advance to row 2 * ->prependColumn('colTwo') // Get row 2, col 2. (Body 1) * ->columnAfter('colThree') // Get row 2 col 3. (Footer 1) * ->doneWithQuery() // Let QueryPath clean up. YOU SHOULD ALWAYS DO THIS. * ->writeHTML(); // Write the output as HTML. * ?> * @endcode * * With the code above, we step through the document, selectively building elements * as we go, and then populating this elements with data from our initial query. * * When the last command, {@link QueryPath:::writeHTML()}, is run, we will get output * like this: * * @code * * * * * Untitled * * *

Title 0

*

Body 1

* Footer 1 * * @endcode * * Notice the body section in particular. This is where the data has been * inserted. * * Sometimes you want to do something a lot simpler, like give QueryPath a * template and have it navigate a query, inserting the data into a template, and * then inserting the template into the document. This can be done simply with * the {@link queryInto()} function. * * Here's an example from another unit test: * * @code *
  • '; * $sql = 'SELECT * FROM qpdb_test'; * $args = array(); * $qp = qp(QueryPath::HTML_STUB, 'body') * ->append('