簡體   English   中英

print_r檢查PHP結果后的Smarty / PHP顯示在Array中。 但是在Smarty中只顯示了第一個字母,在foreach循環中

[英]Smarty / PHP after print_r check in PHP result are shown in Array. But in Smarty shows only first letter, within foreach loop

我正在Smarty中顯示我的Mysql數據庫的結果。我為Smarty分配了一個數組(之前我用PHP在print_r中測試了這個數組)但是在Smarty中執行foreach循環之后只顯示了一個字母。

我的while / foreach循環可能有問題嗎? 我在外面做了分配給Smarty ......

謝謝,問候埃里克

我的PHP腳本:

        $query_main_category = "
        SELECT 
            webshop_products.wpID
            ,webshop_products.wpName
            ,webshop_products.wpDescription
            ,webshop_categories.wcName
        FROM
            webshop_products
        INNER JOIN
            webshop_product_category
        ON 
            webshop_products.wpID = webshop_product_category.wpcID
        INNER JOIN
            webshop_categories
        ON 
            webshop_product_category.wcID = webshop_categories.wcID  
        WHERE 
            webshop_categories.wcID = '1'
        ";
        $exec_main_category = mysql_query($query_main_category);
        if (($exec_main_category) and mysql_num_rows($exec_main_category))
        {
            while($list_products_category = mysql_fetch_assoc($exec_main_category))
            {
                $entries_product[] = $list_products_category; 
            }   
        }   

        $view_description = '';
        foreach($entries_product as $entry_product)
        {
            //If the description is more than 200 characters
            if (strlen($entry_product['wpDescription']) > 200) 
            {
                //Take the first 200 characters...
                $entry_product['wpDescription'] = substr($entry_product['wpDescription'], 0, 200);

                //Look for the last space in the description
                $temp = strrpos($entry_product['wpDescription'], ' ');

                //And cut everything after that point, and add three dots to show there's more
                $entry_product['wpDescription'] = substr($entry_product['wpDescription'], 0, $temp) . '...';
            }
            else
            {
                //If the description is <= 200 chars, show the whole description
                $entry_product['wpDescription'] = $entry_product['wpDescription'];
            }
        }
$this->view->assign('entry_product_smarty',$entry_product);

和Smarty:

<table>
    <tr>
        <td><strong>Titel</strong></td>     
        <td><strong>Omschrijving</strong></td>
    </tr>
    {foreach from=$entry_product_smarty item=entry_product}         
    <tr>
        <td>{$entry_product.wpName}</td>
        <td>{$entry_product.wpDescription}</td>
    </tr>
    {/foreach}

</table>

您分配$ entry_product而不是$ entries_product。 此外,您可能希望像下面的代碼一樣更改foreach循環,否則它將不會產生任何影響:

        foreach($entries_product as $key => $entry_product)
        {
            //If the description is more than 200 characters
            if (strlen($entry_product['wpDescription']) > 200) 
            {
                //Take the first 200 characters...
                $entries_product[$key]['wpDescription'] = substr($entry_product['wpDescription'], 0, 200);

                //Look for the last space in the description
                $temp = strrpos($entry_product['wpDescription'], ' ');

                //And cut everything after that point, and add three dots to show there's more
                $entries_product[$key]['wpDescription'] = substr($entry_product['wpDescription'], 0, $temp) . '...';
            }
            else
            {
                //If the description is <= 200 chars, show the whole description
                $entries_product[$key]['wpDescription'] = $entry_product['wpDescription'];
            }
        }
        $this->view->assign('entry_product_smarty',$entries_product);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM