Re: Newbie Question - Looping Over mySQL Result Sets
| From: | Peter Bowyer | Date: | Sun, 23 Dec 2001 09:08:05 +0000 |
| Subject: | Re: Newbie Question - Looping Over mySQL Result Sets | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-2377@lists.php.net to get a copy of this message | ||
Hi Roy,
Welcome to Smarty :-)
At 12:56 AM 12/23/01 -0800, Roy wrote:
Am I correct in assuming that I can no longer just loop over the result set, and instead I'll need to store the results in a multi-dimensional array?I believe so. Leastwise, that's how I've always done it.
Here's the typical existing code I have after doing a database query...
while ($row = mysql_fetch_array($sql_result)) {
$KeyID = $row["KeyID"];
$PostDate = $row["PostDate"];
$TextBlurb = $row["TextBlurb"];
echo "$KeyID - <b>$PostDate</b><br>$TextBlurb</font><p>";
}
So, I'll need to store this into some sort of array now? Can someone show me some code (both the PHP and Template)?
-- PHP code --
/* Initialise MySQL and Smarty. Query MySQL... */
while ($row = mysql_fetch_array($sql_result)) {
$data[] = $row;} $smarty->assign('data', $data); $smarty->display('template.tpl); -- /PHP code -- -- template code -- {* This is an extract from a template - add more junk if you wish *} {section name=i loop=$data}
{$data[i].$KeyID} - <b>{$data[i].PostDate}</b><br>{$data[i].TextBlurb}</font><p>";
{/section}
-- /template code --
Happy Christmas to all!
Peter.
--
Maple Design - http://www.mapledesign.co.uk