0

SmartyのMysqlデータベースからの結果を表示しています。Smartyに配列を割り当てました(PHPでprint_rを使用してこの配列をテストする前に)が、Smartyでforeachループを実行した後、1文字しか表示されません。

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>
4

1 に答える 1

2

$entries_product の代わりに $entry_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);
于 2012-05-21T04:49:49.607 に答える