php - Smarty MYSQL 数组关联 ID

标签 php mysql arrays smarty

我的 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/

相关文章:

php - PHP-MySQL错误1040 “Too many connections”

php - 如何在 phpmyadmin 中导入 5 GB MySQL Dump 文件

c++ - 当前类成员函数的指针数组

javascript - KoJs : bind a dynamic number of text boxes to elements of an array

MySQL 按日期和优先级排序

javascript - 谷歌网络应用程序将单元素数组视为单个字符数组

php - MySQL数据库中的错误编码能否破坏AJAX请求(用PHP编写)?

php - 我可以在 mysql 查询中使用什么字符来表示所有结果?

php - 如何在没有内存开销的情况下在 PHP curl 中 POST 大量数据?

MySQL如何使用SET类型来选择一个SET包含另一个SET中的一个或多个项目的记录