I am having showing my results from Mysql database in Smarty.I assigned an array to Smarty (before i tested this array in PHP with print_r) but after doing foreach loop in Smarty only one letter is shown.
Is there maybe something wrong in my while / foreach loop?
I did the assign to Smarty outside of it…
Thanks and greetz Eric
my PHP script:
$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);
And 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>
You assign the $entry_product instead of the $entries_product. In addition, you might want to change your foreach loop like the code below, otherwise it won’t have any effect: