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
  #1  
Old 05-03-2010, 11:35 AM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Age Calculation

My client sells puppies and would like to include an age calculation for each dog.

I found a simple javascript that will do this, I just don't understand how to pull Variables out of the Xcart Database to make it work.

http://www.javascriptkit.com/script/script2/calculateage.shtml

I figured I'd have to create some extra fields, so I made

Birth Month = Birth_M
Birth Day = Birth_D
Birth Year = Birth_Y

Anyone know how to do this right so that it goes on the details and the product list page?

Thanks For your help....
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #2  
Old 05-03-2010, 01:56 PM
 
Shamun Shamun is offline
 

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

Default Re: Age Calculation

To pull variables from the XC Database, you must use something such as this:
{$config.Highslide.Highslide_contentId}

$config is the table
Highslide is the Category its in (its a column)
Highslide_ContentId is the name of the dataset/row.

This is in the config table of course.


For yours, you'd probably want to make a hidden extra field on each product and pull it from that.
__________________
- Shane Munroe
Reply With Quote
  #3  
Old 05-03-2010, 06:05 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Ok so I hid the extra fields, but what you said to do, doesn't help. I understand you're saying to pull the data from the DB, but I'm lost still.

Edit:
I was looking through the DBs and I see that xcart_extra_field_values contains the data as follows:
productid fieldid value
1 1 8
1 2 17
1 3 1970

I found a better script as well.
http://www.hscripts.com/scripts/JavaScript/age-calculator.php
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #4  
Old 05-03-2010, 06:37 PM
 
Shamun Shamun is offline
 

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

Default Re: Age Calculation

I just tested on 4.3.1 but it shouldn't be terribly different (if at all) for 4.2

In the admin area, setup 3 new fields. Birthmonth, Birthday, Birthyear. The default value doesn't matter since each product (dog) will have a unique value. Order value doesnt matter either, and make sure the value isnt shown to the customer unless you want it as so.

Once you get that setup, add the birthday values for one dog (just to test).

Open product.tpl (your product page) and find this line:
Code:
{include file="customer/main/product_details.tpl"}
If you include the javascript before it, it will show before the product description and such. Included after, it will be after the buy buttons.
If you want it in between those elements, open that file (product_details.tpl).
I don't know how your website is, so I don't know what to look for. Just play around with adding in text between </div><div> or even inside to get the right positioning.

Once you get the area you want it in, add the code into the template. You could just make a seperate .tpl altogether and include that to make the code nicer to look at and modify possibly.

Looking at the code, you will need to put {literal} at the start of the code and {/literal} at the end since smarty uses the {} brackets and so does javascript.

Once you add the code, you can pull the variables from the database using this:
{$extra_fields.0.field_value}

$extra_fields is infact the name of the array. Before I misinterpretted it as the table name.
0 is the array it looks in (0 is the first array, not 1). This is the value you would change depending on what array # your values are put into.
field_value is the value of the field.

I'm not 100% sure why you want a calculator for this though, as the one you provided is for humans and I think you would want a dog age calculator since their age compared to humans is done in a different way?




Edit:
I went ahead and made a new .tpl since you may not have known what to edit.
Look for the 3 queries on the array I mentioned earlier and change them as needed. Its in the HTML part near the bottom.

in dog.tpl I have this... Keep in mind, I've removed some stuff.
Code:
{literal} <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(); 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 calday = document.birthday.day.options[document.birthday.day.selectedIndex].value; var calmon = document.birthday.month.options[document.birthday.month.selectedIndex].value; var calyear = document.birthday.year.options[document.birthday.year.selectedIndex].value; if(curday == "" || curmon=="" || curyear=="" || calday=="" || calmon=="" || calyear=="") { alert("please fill all the values and click go -"); } else { 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); document.birthday.age.value=dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days"; var monleft = (dife[0]*12)+dife[1]; var secleft = diff/1000/60; var hrsleft = secleft/60; var daysleft = hrsleft/24; document.birthday.months.value=monleft+" Month since your birth"; document.birthday.daa.value=daysleft+" days since your birth"; document.birthday.hours.value=hrsleft+" hours since your birth"; document.birthday.min.value=secleft+" minutes since your birth"; var as = parseInt(calyear)+dife[0]+1; var diff = Date.UTC(as,calmon,calday,0,0,0) - Date.UTC(curyear,curmon,curday,0,0,0); var datee = diff/1000/60/60/24; document.birthday.nbday.value=datee+" days left for your next birthday"; } } </script> {/literal} <form name="birthday"> Date<select name="day" size="1"> <script type="text/javascript"> document.write("<option value={$extra_fields.1.field_value}>{$extra_fields.1.field_value}</option>"); </script></select> Month<select name="month" size="1"> <script type="text/javascript"> document.write("<option value={$extra_fields.0.field_value}>{$extra_fields.0.field_value}</option>"); </script></select> Year<select name="year" size="1"> <script type="text/javascript"> document.write("<option value={$extra_fields.2.field_value}>{$extra_fields.2.field_value}</option>"); </script></select> <input name="start" onclick="calage()" value="Calculate" type="button"><br> <input name="age" size="40" value="Result"><br> You have been living for:<br> <table style="border:solid green 1px"> <tr><td>In months:</td><td><input name="months" size="30"></td></tr> <tr><td>In days:</td><td><input name="daa" size="30"></td></tr> <tr><td>In hours:</td><td><input name="hours" size="30"></td></tr> <tr><td>In minutes:</td><td><input name="min" size="30"></td></tr> <tr><td colspan=2>Your next birthday will be in:</td></tr> <tr><td colspan=2><input name="nbday" size="40"><a href="http://www.hscripts.com" style="color:#3D366F;text-decoration:none;cursor:pointer;font-size:10px">hscripts.com</a></td></tr> </table> </form>


Then you can include it via
{include file="location/of/dog.tpl"}
__________________
- Shane Munroe
Reply With Quote
  #5  
Old 05-03-2010, 06:48 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Quote:
Originally Posted by Tal

I'm not 100% sure why you want a calculator for this though, as the one you provided is for humans and I think you would want a dog age calculator since their age compared to humans is done in a different way?
Yes you're right 7 years to 1 human year or whatever. But his clients like to know how old the puppies are. 12 weeks, 16 weeks, etc.

I'll play around with the javascript and see what I can come up with.
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #6  
Old 05-03-2010, 06:51 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
Yes you're right 7 years to 1 human year or whatever. But his clients like to know how old the puppies are. 12 weeks, 16 weeks, etc.

I'll play around with the javascript and see what I can come up with.


Re-read my post, I added in what you would need to use that calculator
__________________
- Shane Munroe
Reply With Quote
  #7  
Old 05-03-2010, 06:56 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Quote:
Originally Posted by Tal
Re-read my post, I added in what you would need to use that calculator
I'm building it right now.

Will what you did display the age in the results box based on the extra variables?
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #8  
Old 05-03-2010, 06:57 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
I'm building it right now.

Will what you did display the age in the results box based on the extra variables?

Yes. If you look at the HTML form near the bottom, it has the {$extra_fields.x.field_value} queries. It will pull the field value of each dataset. You just need to use the right number since I don't know what yours are.
That's basically all you need to change to get the calculator to pull the age correctly.


Edit:
Or do you mean will it auto-calculate? If so, then no. They still have to click the button.
Google "onload" to find info on running javascript when the page loads.
__________________
- Shane Munroe
Reply With Quote
  #9  
Old 05-03-2010, 07:01 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Yeah I meant automatically.

I think if I can get the java to build the variables based on the extra data then is should display "age" in the results window in the form.
__________________
X-Cart 4.3
Joomla
Among other things
Reply With Quote
  #10  
Old 05-03-2010, 07:46 PM
 
minfinger minfinger is offline
 

X-Adept
  
Join Date: Apr 2009
Posts: 678
 

Default Re: Age Calculation

Well I can't get it to work right and it's messing up the product details page.

http://littlepuppiesonline.msidesigns.com/product.php?productid=1&cat=10&page=1

I attached the age.tpl file I setup.

Thanks for your help!
Attached Files
File Type: tpl age.tpl (2.5 KB, 76 views)
__________________
X-Cart 4.3
Joomla
Among other things
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 03:13 AM.

   

 
X-Cart forums © 2001-2020