PHP's Smarty foreach and section loop methods!

Source: Internet
Author: User
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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.