cvs: peardoc /en/package/structures/structures-datagrid example-simple.xml
| From: | Olivier Guilyardi | Date: | Wed, 19 Apr 2006 18:56:04 +0000 |
| Subject: | cvs: peardoc /en/package/structures/structures-datagrid example-simple.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-7793@lists.php.net to get a copy of this message | ||
olivierg Wed Apr 19 18:56:04 2006 UTC
Modified files:
/peardoc/en/package/structures/structures-datagrid
example-simple.xml
Log:
Replaced obsolete examples with SQL-based ones
http://cvs.php.net/viewcvs.cgi/peardoc/en/package/structures/structures-datagrid/example-simple.xml?r1=1.10&r2=1.11&diff_format=u
Index: peardoc/en/package/structures/structures-datagrid/example-simple.xml
diff -u peardoc/en/package/structures/structures-datagrid/example-simple.xml:1.10
peardoc/en/package/structures/structures-datagrid/example-simple.xml:1.11
--- peardoc/en/package/structures/structures-datagrid/example-simple.xml:1.10 Fri Nov 18 22:47:11
2005
+++ peardoc/en/package/structures/structures-datagrid/example-simple.xml Wed Apr 19 18:56:04 2006
@@ -1,67 +1,76 @@
<?xml version="1.0" encoding="iso-8859-1" ?>
-<!-- $Revision: 1.10 $ -->
+<!-- $Revision: 1.11 $ -->
<refentry id="package.structures.structures-datagrid.example-simple">
<refnamediv>
<refname>Example - Quick</refname>
- <refpurpose>Quickly bind a database record set</refpurpose>
+ <refpurpose>Quickly retrieve data from a database table</refpurpose>
</refnamediv>
<refsect1 id="package.structures.structures-datagrid.example-simple.desc">
&title.desc;
<refsect2 id="package.structures.structures-datagrid.example-simple.desc.bind">
- <title>Binding a result set</title>
+ <title>Binding an SQL query</title>
<para>
- To begin binding an existing record set of data to a DataGrid you can
- easy use the
+ To retrieve the data that the datagrid will display you can begin
+ by passing a simple SQL statement to the
<link
linkend="package.structures.structures_datagrid.structures_datagrid.bind">
<function>bind</function>
</link> method.
</para>
<example>
- <title>Simple Database Binding Example</title>
+ <title>Using an SQL query as datasource</title>
<programlisting role="php">
<![CDATA[
<?php
require 'Structures/DataGrid.php';
-// Get my data
-$result = mysql_query('SELECT * FROM users');
-while ($record = mysql_fetch_assoc($result)) {
- $dataSet[] = $record;
-}
-
-// Print the DataGrid
-$dg =& new Structures_DataGrid();
-$dg->bind($dataSet);
-$dg->render();
+// Instantiate the DataGrid
+$datagrid =& new Structures_DataGrid();
+
+// Setup your database connection
+$options = array('dsn' => 'mysql://user:password@host/db_name');
+
+// Bind a basic SQL statement as datasource
+$datagrid->bind("SELECT * FROM my_table",$options);
+
+// Print the DataGrid with the default renderer (HTML Table)
+$datagrid->render();
?>
]]>
</programlisting>
+ </example>
<para>
- Additionaly you can use the bindDataSource for more complex datatypes such
- as a DB_Result object:
+ If you are familiar with the SQL language you'll certainly fine many ways
+ to adapt the above example to your needs, using more complex queries.
</para>
+ <para>
+ You can also page through your dataset with the automatic paging feature
+ as shown below. This feature transparently adds <acronym>LIMIT</acronym>
+ clauses to your SQL statement, providing optimized database access.
+ </para>
+ <example>
+ <title>Automatic paging</title>
<programlisting role="php">
<![CDATA[
<?php
require 'Structures/DataGrid.php';
-require 'Structures/DataGrid/DataSource.php';
-$dg =& new Structures_DataGrid();
+// 10 records per page
+$datagrid =& new Structures_DataGrid(10);
-// Get my data
-$result = $db->query('SELECT * FROM users');
+// Setup your datasource
+$options = array('dsn' => 'mysql://user:password@host/db_name');
+$datagrid->bind("SELECT * FROM my_table",$options);
-// Bind to DataGrid
-$data = Structures_DataGrid_DataSource::create($result);
-$dg->bindDataSource($data);
+// Print the DataGrid with the default renderer (HTML Table)
+$datagrid->render();
-// Print the DataGrid
-$dg->render();
+// Print the HTML paging links
+$datagrid->render(DATAGRID_RENDER_PAGER);
?>
]]>
</programlisting>