如何在smarty模板引擎中进行嵌套的foreach循环?

时间:2017-10-07 20:11:12

标签: php smarty templating smarty3 smarty2

我是smarty并且尝试实现嵌套的foreach循环。所以我有两个表子类别和成员,其中成员和子类别可以通过subcategory_id连接。例如,有5个类别,大约50个成员属于这5个类别中的任何一个。我想首先循环子类别,然后显示每个类别下的成员。但是我无法在聪明的事情上做第二次预告。在普通的PHP我能够做但不是聪明。请帮忙。

php code

$result = mysqli_query($conn,"SELECT * FROM subCategory ");
$list_subchannels = array();
while ( $row = mysqli_fetch_assoc($result) )
{

        $subcategory = $row['subcat_id'];
        $result1 = mysqli_query($conn,"SELECT * FROM member JOIN subCategory ON member.subcategory_id = subCategory.subcat_id WHERE member.subcategory_id = '$subcategory' ");
        $list_subchannels = array();
        while ( $row1 = mysqli_fetch_assoc($result1) )
        {
            $list_subchannels[] = $row1;
            $smarty->assign($row1['subcat_name'],$list_subchannels);

        }


        $list_subcategories[] = $row;
        $smarty->assign("subcategory",$list_subcategories);

}

.tpl代码是

{foreach from=$subcategory key=k item=b}
    Sub Category Names: {$b.subcat_name}

    <!-- Listing Item -->
    {foreach from=$b.subcat_name key=k item=a}
                    <h3>Member Name: {$a.channel_name}</h3>
                    <span>Member Id: {$a.member_id}</span>
    {/foreach}                
    <!-- Listing Item / End -->

{/foreach}

它正确显示所有子类别名称(即第一个foreach),但显示其下的成员。 TIA

2 个答案:

答案 0 :(得分:0)

PHP代码:

class TSSDocument: UIDocument {

    var userText: String? = ""

    override func contents(forType typeName: String) throws -> Any {

        if let content = userText {

            let length =
                content.lengthOfBytes(using: String.Encoding.utf8)
            return NSData(bytes:content, length: length)
        } else {
            return Data()
        }
    }

    override func load(fromContents contents: Any, ofType typeName: String?) throws {

        let n = contents as? Data

        let o = contents as AnyObject

        if let userContent = contents as? Data {
            if let bytes = (contents as AnyObject).bytes {
                userText = NSString(bytes: bytes,
                                    length: userContent.count,
                                    encoding: String.Encoding.utf8.rawValue) as String?
            }
            else
            {
                userText = ""
            }
        }
    }

}

TPL代码:

$result = mysqli_query($conn,"SELECT * FROM subCategory ");
$list_subchannels = array();
while ( $row = mysqli_fetch_assoc($result) )
{
    $subcategory = $row['subcat_id'];
    $result1 = mysqli_query($conn,"SELECT * FROM member JOIN subCategory ON member.subcategory_id = subCategory.subcat_id WHERE member.subcategory_id = '$subcategory' ");
    $list_subchannels = array();
    while ( $row1 = mysqli_fetch_assoc($result1) )
    {
        // $list_subchannels[] = $row1; // COMMENT
        // $smarty->assign($row1['subcat_name'],$list_subchannels); // COMMENT

        $list_subchannels[$subcategory] = $row1; // HERE
    }

    $list_subcategories[] = $row;
    $smarty->assign("subcategory",$list_subcategories);
    $smarty->assign("subchannels", $list_subchannels); // HERE
}

答案 1 :(得分:-1)

尝试使用{function} {/ function}。

例如:

{function name=subcategory_listing level=0}
    {foreach $subcategory as $subc}
        {if $level == 0}
            Sub Category Names: {$subc.subcat_name}
        {else}
            <h3>Member Name: {$subc.channel_name}</h3>
            <span>Member Id: {$subc.member_id}</span>
        {/if}

        {subcategory_listing cats=$subc.subcat_name level=$level+1}
    {/foreach}
 {/function}

 {subcategory_listing cats=$subcategory}