For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > October 2006 > Re: [SMARTY] DB_DataObject and Smarty









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [SMARTY] DB_DataObject and Smarty
Ken Snyder

2006-10-05, 7:04 pm

Yes, you probably do need to implement the iterator interface in the
users class. I believe that Smarty uses foreach loops for all iteration
so that objects with iterator interfaces can be used. You should also
get the same error/result if you were to add the following to sandbox.php:

foreach( $users as $user ) {
echo "Current User: ".$user['id']."<br>";
}


--Ken


Chris Boget wrote:
> Does anyone know of any tutorials out there that show how I can get
> smarty working with DBDO? I've searched but haven't been able to come
> up with anything beyond using DB::getAll() method. I'd rather stay
> away from using that if at all possible. I'd rather use DBDO::find().
> Here's one (among many) attempt I made to get it to work:
>
> sandbox.php:
> <?php
> $users = new Users();
> $users->country = 'USA';
> $users->limit( 10 );
> $users->find();
>
> $smarty = new Smarty();
> $smarty->assign( 'users', $users );
> $smarty->display( 'sandbox.tpl' );
>
> ?>
>
> sandbox.tpl
> <html><head><title></title></head><body>
> {section name=bar loop=$users}
> Current User: {$users[bar].uid}<br>
> {/section}
> </body></html>
>
> but doing that gives me an error saying that it cannot use object of
> type Users as array. Am I just doing it wrong? Do I need to
> implement the iterator interface in the Users class?
>
> If someone can offer any pointers as to what I'm doing wrong or if you
> can point me to a howto/tutorial on the web, that'd be awesome!
>
> thnx,
> Chris

Chris Boget

2006-10-05, 7:04 pm

> Yes, you probably do need to implement the iterator interface in the users
> class. I believe that Smarty uses foreach loops for all iteration so that
> objects with iterator interfaces can be used. You should also get the
> same error/result if you were to add the following to sandbox.php:
> foreach( $users as $user ) {
> echo "Current User: ".$user['id']."<br>";
> }


Well, I was able to use MDB2_Iterator as my iterator. After instantiating
and initializing it, I'm able to iterate through it fine using straight PHP
(similar to the above code), but I can't get it to work in Smarty. I no
longer get the error (using ->register_object() but I do get it
using ->assign()) but now it's not displaying anything; it doesn't appear
that Smarty is iterating through anything.

Is there something I'm missing?

thnx,
Chris
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com