RE: [SMARTY] Assigning mysql query to smarty template
| From: | Josh Johnson | Date: | Sun, 01 Sep 2002 22:15:02 +0000 |
| Subject: | RE: [SMARTY] Assigning mysql query to smarty template | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-5907@lists.php.net to get a copy of this message | ||
The only thing I noticed that made it "better" is that it's easy to work
with an array of objects instead of a multi-demensional array with
smarty, especially if you totally ignored the part of the manual that
says "The syntax for foreach is much easier than section, but as a
tradeoff it can only be used for a single array" :)
IMHO, it's definitely easier to read, and understand object syntax than
multi-demenstional array syntax, especially smarty's associative array
syntax (coming from a perl/c background, $array.index is cumbersome for
me, but $object->property comes naturally). As far as efficiency goes,
if I remember correctly, php treats objects as associative arrays anyway
(I remember seeing something in the docs about either accessing a
property using the $object[prop_name] syntax or accessing a member of an
assoc array using the -> operator: $array->key).
-- Josh
-----Original Message-----
From: Ray A. Akey [mailto:ray@codemain.com]
Sent: Sunday, September 01, 2002 4:29 PM
To: smarty-general@lists.php.net
Subject: Re: [SMARTY] Assigning mysql query to smarty template
Hmm.. I've been using a similar block but instead of
mysql_fetch_object(),
I've been using mysql_fetch_array(). Now I am curious which is more
efficient; creating an object from the records using the former or
creating
an array from them using the latter method?
Any ideas on which is more efficient? I suppose I'll end up
benchmarking it
myself but a quick answer would be better. ;)
-Ray
----- Original Message -----
From: "Josh Johnson" <mailing_lists@allusiveimage.com>
To: "'gawan'" <mail@gawan.net>; <smarty-general@lists.php.net>
Sent: Sunday, September 01, 2002 12:43 PM
Subject: RE: [SMARTY] Assigning mysql query to smarty template
> In the past, I've assigned an entire array to a template variable, and
> so far it's worked great (the templated part of the site has gotten
over
> 30,000 unique accesses in the past week). I made an array of
> mysql_fetch_object generated objects, which I think made the data more
> accessable in the template.
>
> I'd do something like this (inside of a plugin function, but you could
> do it inside of the php part of your code, or in a {php} tag):
>
> ...
> mysql query code that assigns the result id to $mysql_result_id
> ...
>
> $result = Array(); //keeps the foreach below from breaking
> while($row = mysql_fetch_object($mysql_result_id)){
> $result[$row->id] = $row; // I used the id for
> // the key of the array,
> // you could use whatever
> // fielf you want
> }
>
> $smarty->assign("database_result", $result); // replace $smarty with
> // $this if you're using
a
> // {php} block, otherwise
> // this is the name of
your
> // smarty object.
>
> ....
>
> Then, in the template, I can loop through the returned objects like
so:
> {foreach from=$database_result item=current key=$id}
> id: {$current->id} or {$id}<br>
> name: {$current->fname} {$current->lname}
> etc...
> {/foreach}
>
> It might be more efficient though to use mysql_data_seek to pull an
> array or object from the database as needed, instead of all at once,
> especially if you have a really large amount of data that's being
> displayed. Depending on how your query is structured, and when you get
> the array, if a large amount of data is being put into an array, it
can
> delay the display of the information, even if some of it is disgarded
> (I've had this problem in the past). With mysql_data_seek, you could
out
> spit out the data for the row as you receive it. I haven't tried it
yet,
> but if I ever get around to writing a plugin that uses
mysql_data_seek,
> I'll post it.
>
> Hope that helps.
>
> -- Josh
>
> -----Original Message-----
> From: gawan [mailto:mail@gawan.net]
> Sent: Sunday, September 01, 2002 12:13 PM
> To: smarty-general@lists.php.net
> Subject: [SMARTY] Assigning mysql query to smarty template
>
> Hi
>
> I am a beginner in using Smarty. Where could I find a basic example on
> how
> to assign a mysql query to a smarty template variable.
>
> Should I try to assign every result row separately or would assigning
> the
> whole query at once be more elegant (as an array of arrays). I am
still
> struggling with getting the basics right.
>
> thank you
>
> P.S. I wonder why smarty.php.net is so often unreachable?
>
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php