我正在 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>