我的 php 代码是这样的:
$products = mysql_query("SELECT p.* FROM products AS p "
."INNER JOIN products_cat_link AS cpl ON cpl.Product_ID = p.id "
."WHERE cpl.Category_ID = '$forcat' "
."ORDER BY p.id");
$products_row = array();
$products_images = array();
while($prow = mysql_fetch_assoc($products)) {
if (isset($prow['id'])) {
$product['info'] = $prow;
$images = mysql_query("SELECT * FROM products_images WHERE `Product_id` = '$prow[id]' ORDER BY id");
while($irow = mysql_fetch_assoc($images)) {
$images_result[] = $irow;
}
$product['images'] = $images_result;
$products_images[] = $product;
}
$product = '';
$images_result = '';
}
$smarty->assign('products_images',$products_images);
这使得一个数组像这样
Array (
[info] => array (
[id] = 1
[name] = abc
)
[images] => array (
[Products_Image_Thumb] = 1a.jpg
[Products_Image_Big] = 1b.jpg
)
)
但是我无法用smarty获取图片信息:
{foreach from=$products_images key=cols item=image name=images}
{if $cols % 4 == 0}<div>{/if}
<a href="images/products/big/{$image.images.Products_Image_Big}">
<img src="images/products/thumb/{$image.images.Products_Image_Thumb}" alt="Click to Enlarge" title="Click to Enlarge" width="87" height="65" style="margin:3px;" /></a>
{if $cols % 4 == 3}</div>{/if}
{if $smarty.foreach.images.last}</div>{/if}
{/foreach}
感谢您的帮助!
最佳答案
根据对我之前回答的评论,这应该有效。然而,它未经测试,我有一段时间没有使用 Smarty。
{foreach from=$products_images key=cols item=product name=products}
{if $cols % 4 == 0}<div>{/if}
{foreach from=$product.images item=image name=images}
<a href="images/products/big/{$image.Products_Image_Big}">
<img src="images/products/thumb/{$image.Products_Image_Thumb}" alt="Click to Enlarge" title="Click to Enlarge" width="87" height="65" style="margin:3px;" /></a>
{/foreach}
{if $cols % 4 == 3}</div>{/if}
{if $smarty.foreach.products.last}</div>{/if}
{/foreach}
关于php - Smarty MYSQL 数组关联 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6796517/