Smarty MYSQL 数组关联 ID

发布于 2024-11-26 10:38:53 字数 1730 浏览 2 评论 0原文

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

感谢您的帮助!

I have my php code like this:

$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);

That makes an array like this

Array ( 
    [info] => array (
        [id] = 1
        [name] = abc
    ) 
    [images] => array (
        [Products_Image_Thumb] = 1a.jpg
        [Products_Image_Big] = 1b.jpg
    )
)

But i cannot get the images information with 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}  

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-12-03 10:38:53

根据我之前的回答的评论,这应该可行。然而它还没有经过测试,而且我已经有一段时间没有使用 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}

Based off the comment on my previous answer, this should work. However it is untested and I haven't used Smarty in a while.

{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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文