views:

144

answers:

2

I have this code here,

{foreach from=$cart.cartItems item="item" name="cart"}    

<div id="cart2Produkt">


 <p>{if $item.Product.ID}
                    <a href="{productUrl product=$item.Product}" data-tooltip="sticky1">{$item.Product.name_lang|truncate:20}</a>
                {else}
                    <span>{$item.Product.name_lang|truncate:20}</span>
                </a>
            {/if}
                <div id="mystickytooltip" class="stickytooltip">

<div style="padding:5px;">

<div id="sticky1" class="atip" style="width:200px;">

<img src="{$item.Product.DefaultImage.paths.3}" alt="{$item.Product.name_lang|escape}"><br>
{$item.Product.name_lang}

</div>

</div>
<div class="stickystatus"></div>

</div>

</p>
 <p>
 {include file="order/itemVariations.tpl"}
            {include file="order/block/itemOptions.tpl"}

            {if $multi}
                {include file="order/selectItemAddress.tpl" item=$item}
            {/if}
            </p>
</div>

{/foreach}

I want to change the "1"in "sticky1" to a "2" everytime this loops (it's part of a foreach).

But no luck.. Plus I am kinda new to Javascript and don't know how to document write it where I want it.

Please help!

A: 

It's going to be much more effective to change it while you are generating the code in your foreach loop than to fix it later with javascript. Note that if someone has javascript disabled, your code won't run and the changes won't be made. If you make the change server-side, then it gets generated correctly in the first place.

Example:

{foreach from=$cart.cartItems item="item" name="cart"}    

<div id="cart2Produkt">


 <p>{if $item.Product.ID}
                    <a href="{productUrl product=$item.Product}" data-tooltip="sticky{$item.Product.ID}">{$item.Product.name_lang|truncate:20}</a>
                {else}
                    <span>{$item.Product.name_lang|truncate:20}</span>
                </a>
            {/if}
                <div id="mystickytooltip" class="stickytooltip">

<div style="padding:5px;">

<div id="sticky{$item.Product.ID}" class="atip" style="width:200px;">

...

Using javascript (and jQuery to achieve the same thing later).

   $(function() {
         $('[data-tooltip=sticky1]').each( function(i) {
              var tipNo = i + 1;
              $(this).attr('data-tooltip','sticky' + i);
         });
   });

FWIW, using document.write() will only append things to the document. You need to navigate the DOM, either using jQuery (or another framework) or plain javascript to find the element you want to change and update it's property. Using a framework, IMO, is usually much easier than writing the same code with plain javascript.

tvanfosson
Thanks, I should have said I'm using php and smarty code.I'll try the Javascript solution first :)
Kyle Sevenoaks
Yeah I couldn't get that to work, sorry but I am a total noob with JS.
Kyle Sevenoaks
It uses jQuery -- did you include jQuery before attempting it?
tvanfosson
Or post your PHP code -- it shouldn't be that hard to modify.
tvanfosson
<div id="cart2Produkt"> <p>{if $item.Product.ID} <a href="{productUrl product=$item.Product}" data-tooltip="sticky1">{$item.Product.name_lang|truncate:20}</a> {else} <span>{$item.Product.name_lang|truncate:20}</span> </a> {/if} <div id="mystickytooltip" class="stickytooltip"> <div style="padding:5px;"><div id="sticky1" class="atip" style="width:200px;"><img src="{$item.Product.DefaultImage.paths.3}" alt="{$item.Product.name_lang|escape}"><br>{$item.Product.name_lang}</div></div><div class="stickystatus"></div></div>
Kyle Sevenoaks
@Kyle - in your question -- that's almost impossible to read, and include the foreach part of the loop as well.
tvanfosson
Sorry, hope that's better!
Kyle Sevenoaks
@Kyle -- are your product ids unique -- if so, you could use it and append it to "sticky" to define the id. I'll update my example.
tvanfosson
Yes, I see what you mean and that would be good but for this I am using a JS hoverbox image things and the sticky1 just needs sticky2 in the next loop.. But I am thankful for what you showed me here for other reasons :) So just to make the 1 turn iutno 2 hen 3 on the next loop :)
Kyle Sevenoaks
I would have thought that the hoverbox just needed to have the same, unique id in the tooltip and the DIV id. If my suggestion doesn't work, then replace the product id with the iteration counter as @Douglas suggests.
tvanfosson
Yes, Doglass' suggestion worked to what I wanted it to do, but unfortunately now no pictures display. The problem is that when it reloops for different products, it displays the same picture for item 2, 3, 4etc as for item 1. I thought this would fix it. But not. Now I am truly stumped.
Kyle Sevenoaks
A: 

Looks like you want to use the smarty iteration counter to set the id. Have a look at Example 7-11 here: http://www.smarty.net/manual/en/language.function.foreach.php

{* this will output sticky1, sticky2, sticky3, ... etc *}

{foreach from=$cart.cartItems item="item" name="cart"}
    sticky{$smarty.foreach.cart.iteration},
{/foreach}
Douglas
Thanks, that did it, just great to find out that it didn't fix my bigger problem..
Kyle Sevenoaks
I have found out what's going wrong. The {$item.Product.DefaultImage.paths.3} only reads from the first item in the cart.. I think it's because they're not part of the IF loop. But trying to add them in just breaks the layout and the function.. You can see this @euroworker.no Put something in the cart (use the "kjøp" button then click "handlevogn"). Then mouseover the product name, put more items in to see what I'm going for..
Kyle Sevenoaks