RE: [SMARTY] Assigning mysql query to smarty template
| From: | Josh Johnson | Date: | Sun, 01 Sep 2002 16:43:00 +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-5903@lists.php.net to get a copy of this message | ||
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