Foreach cycle method:
Main. php
<? PHP
Include "Class/smarty. Class. php ";
Define ('_ site_root', 'd:/appserv/www/smarty_test ');
Require_once 'Primary des/DB. Class. php ';
$ TPL = new smarty ();
$ TPL-> template_dir = _ site_root. "/templates /";
$ TPL-> compile_dir = _ site_root. "/templates_c /";
$ TPL-> config_dir = _ site_root. "/configs /";
$ TPL-> cache_dir = _ site_root. "/Cache /";
$ TPL-> left_delimiter = '<{';
$ TPL-> right_delimiter = '}> ';
?>
Templates/test. php
<? PHP
Require 'main. php ';
$ Tg = new dB ('localhost', 'root', 'wangxiang ', 'fenghua', 'utf8 ');
$ SQL = "select title, content from news where id = 10 ";
$ Result = $ Tg-> getrow ($ SQL );
$ Array1 = array (1 => "apple", 2 => "pineapple", 3 => "banana", 4 => "happy", 5 => "watermelon ", 6 => "leather balls", 7 => "sneakers ");
$ TPL-> assign ("title", $ result [title]);
$ TPL-> assign ("content", $ result [content]);
$ TPL-> assign ("array1", $ array1 );
$ TPL-> display ("test.html ");
?>
Test.html
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> smarty </title>
</Head>
<Body>
<{$ Title}>
<Br>
<{$ Content}>
<Br>
<{Foreach item = Item1 from = $ array1}>
<{$ Item1}>
<{/Foreach}>
</Body>
</Html>
========================================================== ===
Section cycle method:
<? PHP
Require_once ("include/config_base.php ");
Require 'libs/smarty. Class. php ';
$ Db = new dedesql (false );
$ Smarty = new smarty;
$ Smarty-> compile_check = true;
$ Db-> setquery ("select ID, title from #@__ archives where iscommend = 11 order by id desc limit ");
$ Db-> execute ();
While ($ nrow = $ db-> GetObject ())
{
$ News_list [] = array ("ID" = >$ nrow-> ID, "title" => mb_substr ($ nrow-> title, 'gbk '));
}
$ STR = @ file_get_contents ("http://weather.265.com/weather.htm ");
$ STR = str_replace ("<SCR platinum Pt>", "</body>", $ Str );
$ Smarty-> assign ("news", $ news_list );
$ Smarty-> assign ("weatherstr", $ Str );
$ Smarty-> display('index.html ');
?>
Index.html
Loop {section name = loop = $ news}
<A href = "Plus/view. php? Aid = {$ news [loop]. ID} ">{$ news [loop]. Title} </a> <br/>
{/Section}
Weather {$ weatherstr}