Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Age Calculation

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #31  
Old 05-08-2010, 04:24 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

Code:
{* For http://forum.x-cart.com/showthread.php?t=53578 Call extra fields via this: {$extra_fields.0.field_value} {$extra_fields.1.field_value} {$extra_fields.2.field_value} *} <script type="text/javascript"> var startyear = "1950"; var endyear = "2010"; var dat = new Date(); var curday = dat.getDate(); var curmon = dat.getMonth()+1; var curyear = dat.getFullYear(); var calday = {$extra_fields.1.field_value}; var calmon = {$extra_fields.0.field_value}; var calyear = {$extra_fields.2.field_value}; {literal} function checkleapyear(datea) { if(datea.getYear()%4 == 0) { if(datea.getYear()% 10 != 0) { return true; } else { if(datea.getYear()% 400 == 0) return true; else return false; } } return false; } function DaysInMonth(Y, M) { with (new Date(Y, M, 1, 12)) { setDate(0); return getDate(); } } function datediff(date1, date2) { var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(), y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate(); if (d1 < d2) { m1--; d1 += DaysInMonth(y2, m2); } if (m1 < m2) { y1--; m1 += 12; } return [y1 - y2, m1 - m2, d1 - d2]; } function calage() { var curd = new Date(curyear,curmon-1,curday); var cald = new Date(calyear,calmon-1,calday); var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); var dife = datediff(curd,cald); {/literal} document.getElementById('dAge').innerHTML = dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; {literal} } $(document).ready(function(){ calage(); }); {/literal} </script> He is <span id="dAge">5 months</span> old



There you go. There is no forms in that and it's text based.
You can customize what it says at the bottom. Anything between the <span></span> tags are from the script so you can change anything outside of that. You'll probably need to change the order for which the age is though. At the top with the smarty variables, I have it as 1 0 2.

If that works, you'll want to put the script in an actual external .js file so the browser can cache it. Faster load times and less bandwith. The " He is <span id="dAge">5 months</span> old" shouldn't be in a .js file though.
__________________
- Shane Munroe
Reply With Quote

The following 2 users thank Shamun for this useful post:
gb2world (05-08-2010), minfinger (05-08-2010)
  #32  
Old 05-08-2010, 05:53 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

THANKS TAL!!! That's awesome!!! I'm extremely grateful!!!
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #33  
Old 05-08-2010, 07:10 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

TAL,

I was wondering about making it say "Sku" is <span id="dAge">5 months</span> old"

Here's what I was trying, but I got nothing.

<span id="product.productid"></span> is <span id="dAge">5 months</span> old
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #34  
Old 05-08-2010, 07:50 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

the spanId is simply used to let the script know where to replace text.

Code:
document.getElementById('dAge').innerHTML = dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days";

As you can see, it has dAge in there. So everything in the dAge ID will be replaced by the output of the script, which happens to be the age. If it can't get a value, it'll just say as "5 months".

If you want to show something like "Junior (SKU name) is X old" then it would be this as the HTML/smarty part:
Code:
{$product.productid} is <span id="dAge">5 months</span> old

If you wish to style the name of the puppy you can add its own class/id such as this:
Code:
<span class="puppyCust">{$product.productid}</span> is <span id="dAge">5 months</span> old

Then you just need to add something like this in your css:

Code:
.puppyCust { color: #ea55f3; text-decoration: super flashing underline and overlines and everything else; font-size: 400000px; }
__________________
- Shane Munroe
Reply With Quote
  #35  
Old 05-09-2010, 08:15 AM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Thanks Tal.

I was trying to input some logic in the final calc. His puppies are often after less than a year old, so it's not necessary to include the year calc.

I was trying the following, but it's not working right

Code:
if(dife[0]>0){ {/literal} document.getElementById('dAge').innerHTML = dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days";} if(dife[1]>0){ {/literal} document.getElementById('dAge').innerHTML = dife[1]+" months, and "+dife[2]+" days";}
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #36  
Old 05-09-2010, 01:36 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

That's a bit confusing.

Your first statement is checking the years, then your second is checking the months. It's also not an ifelse statement which would be more logical.

In any case...
Code:
function calage() { var curd = new Date(curyear,curmon-1,curday); var cald = new Date(calyear,calmon-1,calday); var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0); var dife = datediff(curd,cald); if (dife[0]<1) { document.getElementById('dAge').innerHTML = dife[1]+" months, and "+dife[2]+" days"; } else { document.getElementById('dAge').innerHTML = dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; } }



Tested and works.
__________________
- Shane Munroe
Reply With Quote
  #37  
Old 05-09-2010, 02:38 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Awesome Tal!!

So now I'm trying to put the age on the products_list.tpl

I just inserted:
Code:
{include file="modules/Age_Calculation/age.tpl"}

Below:
Code:
<div class="descr">{$product.descr}</div>

However it only shows " Junior is 5 months old" And does the same for all dogs, except the name is different.

http://littlepuppiesonline.msidesigns.com/home.php?cat=10
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #38  
Old 05-09-2010, 02:53 PM
 
Shamun Shamun is offline
 

X-Adept
  
Join Date: Jun 2009
Location: North Carolina
Posts: 841
 

Default Re: Age Calculation

Quote:
Originally Posted by minfinger
Awesome Tal!!

So now I'm trying to put the age on the products_list.tpl

I just inserted:
Code:
{include file="modules/Age_Calculation/age.tpl"}

Below:
Code:
<div class="descr">{$product.descr}</div>

However it only shows " Junior is 5 months old" And does the same for all dogs, except the name is different.

http://littlepuppiesonline.msidesigns.com/home.php?cat=10


That's because the extra field variables are not available in the category display.

You'll have to have someone else help you with that.
__________________
- Shane Munroe
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 12:34 PM.

   

 
X-Cart forums © 2001-2020