[CLUE-Tech] ASP alternative.

Dale K. Hawkins dhawkins at cdrgts.com
Wed Dec 3 15:49:05 MST 2003


David Anselmi <anselmi at americanisp.net> writes:

> Here's an example.  In ASP, a few lines of code run a database query
> and a few more hook it into a "datagrid control" (most of that can be
> done drag and drop but that isn't too important).

I thought I'd send a follow example of using smarty and
DB_DataContainer with php.  All very powerful and simple code.

Is this few enough lines of code?  :-)

========== people.php 8< ==========

require 'Smarty.class.php';

require_once 'DB.php';
require_once 'DB/sqlite.php';
require_once('DB/DataContainer.php');

class Person extends DB_DataContainer {

    var $firstname;
    var $lastname;
    var $mobile;

    function Person($dbh, $params) {
      overload('Person');

      if (is_array($params)) {
	$params[table] = $params[table] ? $params[table] : 'person';
      } else {
	$this->setTable("person");
      }

      $this->DB_DataContainer($dbh, $params);    
    }
}

// Define a DSN
// $dsn = "sqlite://dummy:@localhost///tmp/person.db?mode=0644";
$dsn = array (
 	      'phptype'   => "sqlite",
 	      'database'  => "/tmp/person.db",
 	      'mode'      => 0644
 	      );


$dbh = &new DB_sqlite();
$dbh->connect($dsn, array('persistent'=> true) );

$smarty = new Smarty;

$smarty->compile_check = true;
$smarty->debugging = true;

$params[classname] = 'person';
$params[limit]     = 10;
$people = Person::getObjects($dbh, $params);

$smarty->assign('people', $people);

$smarty->display('people.tpl');

========== people.tpl 8< ==========

{include file="header.tpl" title="Phone Book"}

<table class="select-list">
   <tr class="header">
      <th>Name</th>
      <th>Number</th>
   </tr>
{foreach from=$people item=person}
   <tr class="{cycle values="odd,even"}">
      <td class="item"><a href="edit-person.php?tid={$person->id}" {popup text="Edit Entry"}>{$person->lastname}, {$person->firstname}</a></td>
      <td class="item">{$person->mobile}</td>
   </tr>
{/foreach}
</table>

<p>

{include file="footer.tpl"}



More information about the clue-tech mailing list