Michael Bommarito ([info]g0thm0g) wrote in [info]php,
  • Mood: wow

The Holy Grail of PHP

Thought I'd share this with the community. I'll be writing a real walkthrough soon, as this combo is too good to keep to myself.
Would love to hear any comments, criticisms, or alternatives. If anyone is interested in helping write a few pages on the topic, send me an email or something too.


What do you get when you combine an automated database object abstraction layer, an embedded templating engine, and an AJAX library?

Unparalled rapid modular development fusing the web's hottest technologies.
And it's easy.

PEAR DB_DataObject
PHP Smarty
xajax

What's it look like? Here's a teenie example.

index.php
<?php
require_once 'ajax.php';
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<?php $xajax->printJavascript(); ?>
		<title>Test</title>
	</head>
	<body>
		<form id="frmNote">
			<div><b>ID</b>: <input type="text" name="id" id="id" /></div>
			<div><input type="button" 
        onclick="xajax_getNote(xajax.getFormValues('frmNote'))"
        value="Get Note" /></div>
		</form>
		<div id="content"></div>
	</body>
</html>



ajax.php
<?php
require_once 'xajax/xajax.inc.php';
require_once 'template.php';

function getNote($form) {
	$id = $form['id'];
	$v = new View();

	$objResponse = new xajaxResponse();
	$objResponse->addAssign("content","innerHTML", $v->getNote($id));

	return $objResponse->getXML();
}

$xajax = new xajax();

$xajax->registerFunction("getNote");
$xajax->processRequests();
?>



template.php
<?php
require_once 'Smarty/Smarty.class.php';
require_once 'data.php';

class View extends Smarty {
	function View() {
		parent::Smarty();
		$this->template_dir = 'tpl/';
		$this->compile_dir = 'tpl/comp';
		$this->config_dir = 'tpl/conf';
		$this->cache_dir = 'tpl/cache';
	}

	function getNote($id = null) {
		$note = DB_DataObject::Factory('notes');
		$note->get($id);
		$this->assign('note', $note);
		return $this->fetch('note.tpl');
	}
}
?>



tpl/note.tpl
<table>
	<tr>
		<td>{$note->name}</td>
	</tr>
	<tr>
		<td>{$note->body}</td>
	</tr>
</table>



data.php
<?php
require_once 'PEAR.php';
require_once 'DB/DataObject.php';

//Initialize configuration
$options = &PEAR::getStaticProperty('DB_DataObject','options');
$config = parse_ini_file('do.ini',TRUE);
$options = $config['DB_DataObject'];
?>



schema.sql
CREATE DATABASE organizer;
USE organizer;

CREATE TABLE notes (
	id INT UNSIGNED NOT NULL AUTO_INCREMENT,
	name VARCHAR(32),
	body TEXT,
	PRIMARY KEY(id)
);





  • Post a new comment

    Error

    Your IP address will be recorded 

  • 23 comments

[info]wisn

August 9 2005, 14:51:19 UTC 6 years ago

I appreciate the post, but can you put all the code samples behind the cutline? It's whacking the hell out of my friend's list.

[info]g0thm0g

August 9 2005, 14:58:29 UTC 6 years ago

How's that? Your friends page looks fine to me now.

What problem were you having? The whole point of the first code block was that you could get a feel for how simple the code ends up looking before committing to reading it all.

[info]wisn

August 9 2005, 15:08:23 UTC 6 years ago

I didn't have a problem with that, but one of the unwrapped lines was running off the window and making all the other text on the page bleed off too.

Back on topic, though: Thanks. I'll take a look at this tonight.

[info]sueposey

August 11 2008, 18:53:55 UTC 3 years ago

I didn`t have one then and I still do not but I do think there are new ways of helping with symptom management.

Anonymous

February 4 2006, 09:32:39 UTC 6 years ago

I could definantly see a use for this in Pagination.

[info]rickieroder

August 11 2008, 16:53:52 UTC 3 years ago

melissa // Jun at am You can put me down as uneducated, if I can put you down as a sexist jerk.

[info]jaybigum

August 11 2008, 17:15:13 UTC 3 years ago

melissa // Jun at am You can put me down as uneducated, if I can put you down as a sexist jerk.

[info]xinu

August 9 2005, 14:59:42 UTC 6 years ago

Thanks for sharing! I'll read through it more carefully when I get to work.

[info]coreb

August 9 2005, 15:13:03 UTC 6 years ago

So how does this compare to the 1 million+ Model-View-Controller Frameworks that have already been developed?

[info]g0thm0g

August 9 2005, 15:31:21 UTC 6 years ago

Essentially, it's just another implementation of an MVC framework. Others might have more maturity in isolating components, but they're missing out on the whole AJAX revolution.

Besides, AJAX would allow the MVC model to be implemented on the web in a manner that simplifies the user's experience, just as it should've first been.

Ruby-on-Rails is exploding because of its native support for this sort of development, and it seems therefore an appropriate time to discuss and test implementations of similar frameworks for PHP.

[info]coreb

August 9 2005, 15:56:12 UTC 6 years ago

Yea, without ajax support an MVC framework has no chance at competing with ruby on rails, from what I am seeing.

What may be an interesting way to support your claim to this combination would be for you to implement a webapp your way and also in another framework as a comparison of ease of use.

A couple of the newer ones that I've found.
PHP on Trax
Biscuit
Jaws
Cake

[info]bobalien

August 10 2005, 22:39:26 UTC 6 years ago

i never could get Ruby-on-Rails to work on my system and i'm just starting to get into working with AJAX and Object Relational DB stuff (I use ADOdb, never could get PEAR working right either - looks like i'll have to give it another whirl)

excellent post, you've always got the goods, i gotta get my hands dirty w/some of this stuff

[info]mileschantler

August 11 2008, 12:15:52 UTC 3 years ago

Yes, if you get to use rails, I get to use my object tool kit ; ) At the end of the day, writing maintainable code and making your users happy trumps any technological "advantage" a framework or new language has to offer.

[info]bobalien

August 12 2005, 00:23:56 UTC 6 years ago

what's your opinion on ADOdb vs. PEAR::DB? I'd like to start playing around with DB_DataObject, and I got PEAR::DB working; from what i've read ADOdb is faster and less buggy than PEAR; is it worth it for me to take a look at using PEAR's DB or should i try to find something comparable to DataObject that works with ADOdb (or something different entirely)

[info]g0thm0g

August 12 2005, 01:03:07 UTC 6 years ago

ADOdb is more mature and has a C extension.

PEAR::DB has better design and a more active community.

In all honesty, I think PHP 5.5/6.0 is going to introduce a real contender in PDO. Keep your eyes on that, and if you've got a development machine handy, I'd suggest grabbing a build from snaps.php.net and getting your hands wet with PDO.

Anonymous

September 1 2005, 23:13:22 UTC 6 years ago

ADODB Vs Pear::DB Vs PDO

I use ADODB extensivly and find its great. Especially you can npw use it as a PHP extension... but, its clunky and not very lightweight. Pear::DB is slightly slower and not as nice in some areas, but great in others (Select meu generation is a great feature in ADODB, but also limited). To be honest I can't wait to try PDO, its looking really good and should be very fast. Just my useless 2c worth :) Great post by the way.

Anonymous

November 22 2005, 03:49:46 UTC 6 years ago

xajax

Hi Michael. This is the xajax project owner. Thanks so much for your interesting article. We're glad that you have found xajax to be useful. I thought I would let you know that we have been working hard on a new release of xajax. We plan to release a beta of the upcoming release later this week. Some of the new features include:

* New error handling: Non-fatal PHP errors can now be easily trapped and written to a log file or displayed in an alert dialog in the client.

* OOP: You can now register instance and static class methods to be callable through xajax. This is essential to make it easy to add xajax to many OOP CMS solutions.

* Internationalization: xajax defaults to use UTF-8, but we've added the ability to set the charset to whatever you need to.

* External Function Registration: you can now register functions in external .php files and xajax will dynamically include the file and call the function only if it is requested.

We will soon be setting up at xajaxproject.org, but for now you can visit our forums at:

http://www.intuitivefuture.com/xajaxforums/

I am interested in your feedback and your needs as you work with xajax. Feel free to ask questions and make feature requests. Thanks,

J. Max Wilson
xajax project owner

Anonymous

January 28 2006, 14:25:55 UTC 6 years ago

Mutreta das grossas...

Cara, isso ai é engembração, mutreta das grossa. hahaha

Anonymous

June 12 2006, 16:25:19 UTC 5 years ago

what should be in do.ini?

what should be write in do.ini? I got following message:

Warning: parse_ini_file(): Cannot open 'do.ini' for reading in d:\appserv\www\html\live\data.php on

Anonymous

June 13 2006, 08:23:58 UTC 5 years ago

Re: what should be in do.ini?

a quite stupid question I asked... since I never used dataobject @@

for someone who has the same question as me.

[DB_DataObject]
database = mysql://xxx:xxxx@localhost/xxxx
schema_location = classes
class_location = classes
require_prefix = /dataobjects/
class_prefix = DataObjects_

Anonymous

July 6 2006, 02:22:45 UTC 5 years ago

undefined function: get()

Thank you for your nice combination of scripts.

I am currently got a problem when I click GetNote button. It shows an error msg as below,

------------------------------------
Error: the XML response that was returned from the server is invalid.
Received:


Fatal error: Call to undefined function: get() in d:\appserv\www\live\template.php on line
16

You have whitespace in your response.
------------------------------------

line 16 is $note->get($id);
Any idea and suggestion are appreciated.

Anonymous

October 27 2006, 14:02:34 UTC 5 years ago

Alternative to DB:DataObject

Personally I live by phpobjectgenerator.com -- look at their tutorials for how to use it!

Anonymous

December 9 2010, 08:22:34 UTC 1 year ago

Patty looks like black and white dress, but also a http://www.party-dresses.biz (http://www.party-dresses.biz) black lace evening dress sexy dresses (http://www.sexy-dresses.biz) Tee. A shaped skirt more copies of her cute little taste of a woman, sexy lace is full of hollow. Added models dressed in the skirt of black tulle look more enchanting charm, coupled with ornate gold looked very mysterious.</p>

Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…