0

我正在 Smarty 中显示 Mysql 数据库的结果。我为 Smarty 分配了一个数组(在我使用 print_r 在 PHP 中测试这个数组之前),但是在 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);

和聪明的:

<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

您分配 $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);
于 2012-05-21T04:49:49.607 回答