2010/03/19

smarty dollar to cent

In a project we have a smarty variable presenting a dollar value like ‘1.23’, or ‘.06’. And we wanted to show cent value when the value less than one dollar, for exmaple,

  1. “.78” will be “78”
  2. “.06” will be “6”

Instead of crating a new smarty variable in php, here is a workaround I made in the template.

{*here $dollar_per is in dollar value*}
{if $dollar_per && $dollar_per < 1}
   {if $dollar_per < 0.1} {*ex. 0.06*}
   {assign var="cent_per" value=$dollar_per|substr:2} {*get 6*}
   {assign var="cent_per" value="&nbsp;"|cat:$cent_per} {*add space before 6 in my case*}
   {else} {*ex. 0.78*}
   {assign var="cent_per" value=$dollar_per|substr:1} {*get 78*}
   {/if}
{/if}

Note: substr and cat were used!

2010/03/12

name attribute of <a> tag

There are several attributes of <a> tag, and ‘name’ is one of them. I just found today, that you cannot have character, ‘%’, as a value of this attribute.

I used to use name attribute to hold some data, then pass it as variable in jQuery code as the following,

$(document).ready(function() {
  $(".s-img>a").hover(function(){
    var largePath = $(this).attr("name"); 
    // note: here I used 'name' attr 
    $("#limg").attr({ src: largePath});
    return false;
    },
    function() {
      ;
  });
});

It failed when the name has ‘%’ character in there. Fortunately, the “alt” attribute is happy with ‘%’as its value. So then, changed it ‘alt’, it works.

P.S. Actually, I see most of ‘alt’ other than ‘name’ to pass variable. Maybe, this is a reason.

2010/03/11

ajax setcookie failed with page reload

I have ajax to set cookie and then reload the page, the original code is some thing like the following:

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set"); //set cookie
  location.reload(true); //reload the page
});

But it failed. Looked around and figured out that the .get ajax called might not be successful before the page reload. ajax is asynchronous, so the reload function could be executing during the ajax called.

The solution is using ajax callback to make sure reload function runs after ajax call ended, see the revise code as the following,

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set", function(){
    location.reload(true);
  });
});