Php smarty looping array implementation code

Source: Internet
Author: User

When a loop is used, empty data is added to the two arrays. How can we filter out empty data in a loop?

If it is determined in the loop body, if it is null, the loop after this loop does not show data continues

For example:

Foreach ($ array as $ value ){

If (empty ($ value ))

Continue;

}

 

The PHP code is as follows:
/**
@ Document category includes Level 2
@ Param int $ rootnum -- number of hash classes
@ Param int $ childnum -- number of second-level classes
@ Return value array
@ Date 2011.2.24
*/
Function temp_articletreecate ($ rootnum, $ childnum ){
If (! Isnumber ($ rootnum )){
$ Rootnum = 10;
}
If (! Isnumber ($ childnum )){
$ Childnum = 10;
}
$ Category = array ();
$ Parent_ SQL = "SELECT cateid, catename FROM". TABLE_PREFIX. "articlecate WHERE

Parentid = 0 AND depth = 0 AND flag = 1 order by orders ASC ";
If (intval ($ rootnum)> 0 ){
$ Parent_ SQL. = "LIMIT $ rootnum ";
}
$ Parent_cate = $ GLOBALS ['DB']-> getall ($ parent_ SQL );
Foreach ($ parent_cate as $ parent_key => $ parent_value ){
// The subclass array name is childcategory, which can be customized as needed
$ Category [] = array ('cateid' => $ parent_value ['cateid'], 'catename' => $ parent_value

['Catename'], 'childcategory '=> array ());


// Read subclass
$ Child_ SQL = "SELECT cateid, catename FROM". TABLE_PREFIX. "articlecate WHERE

Parentid = ". $ parent_value ['cateid']." AND flag = 1 order by orders ASC ";
If (intval ($ childnum)> 0 ){
$ Child_ SQL. = "LIMIT $ childnum ";
}
$ Child_cate = $ GLOBALS ['DB']-> getall ($ child_ SQL );
Foreach ($ child_cate as $ child_key => $ child_value ){
$ Category [count ($ category)-1] ['childcategory '] [] = array ('cateid' => $ child_value

['Cateid'], 'catename' => $ child_value ['catename']);
}
}
Return $ category;
}

Php Tutorial: smarty foreach function **/


$ Tpl = new Smarty ();
$ Tpl-> template_dir = $ cfg ['path'] ['template'];
$ Tpl-> compile_dir = $ cfg ['path'] ['root']. 'Www .111cn.net/111cn /';
 
$ Tpl-> compile_check = $ cfg ['debug'];
$ Tpl-> debugging = false;
$ Tpl-> caching = 0;
$ Tpl-> cache_lifetime = 6000;
 
$ Tpl-> left_delimiter = '<! --{';
$ Tpl-> right_delimiter = '} --> ';


 
// Configure smarty

 

$ Db = new Db ();
Try {
$ Query = $ Db-> query ($ SQL );
If ($ Db-> rows ($ query ))
   {
$ Array = $ Db-> fetch ($ query, 0 );
Foreach ($ array as $ v =>$ _ v)
    {   
$ List [$ v] ['id'] = $ _ v [0];
$ List [$ v] ['cntitle'] = $ _ v [2];
$ List [$ v] ['I'] = $ I ++;
    }   
   
   }
   
} Catch (Execption $ e ){
MessAge ('Call parameter failed! ');
  }


// Php processing and programs

Tpl-> assign ('jsmenu _ Deng', $ List );


 
// The following process foreach for The smarty Template

<! -- {Foreach from = $ jsMenu_Deng item = item key = key} -->
Subcat [<! -- {$ Item. I} -->] = new Array ("<! -- {$ Item. cntitle} --> "," <! -- {$ Item. upid}

--> "," <! -- {$ Item. id} --> ");
<! -- {/Foreach} -->
Var onecount = <! -- {$ Item. I} -->;


 
// In fact, it is very easy to save the data in php and then implement it by smarty foreach.

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.