![]() |
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.... |
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. |
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 |
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 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} Then you can include it via {include file="location/of/dog.tpl"} |
Re: Age Calculation
Quote:
I'll play around with the javascript and see what I can come up with. |
Re: Age Calculation
Quote:
Re-read my post, I added in what you would need to use that calculator :) |
Re: Age Calculation
Quote:
Will what you did display the age in the results box based on the extra variables? |
Re: Age Calculation
Quote:
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. |
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. |
Re: Age Calculation
1 Attachment(s)
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! |
Re: Age Calculation
Try using this:
Code:
{* It auto-calculates on page load, but it requires jquery (no worry, x-cart comes with it and will not have any issues :P) so importing into another program won't work later unless it uses jquery. |
Re: Age Calculation
Not working for me, just showing the "Results".
http://littlepuppiesonline.msidesigns.com/product.php?productid=1&cat=28&page=1 I also tried scaling it down since we don't need some of the other calculations there were in the original form. Code:
{* |
Re: Age Calculation
Do you think we'd better off storing the calculation results as a variable and displaying it like the rest of the site and not using the <form>?
|
Re: Age Calculation
Thankgod firebug exists.
Enter numbers for their birthdays without 0's preceding such as 1 2 3 4 5 6 7 8 9 10 11 12 13 14 etc You also stripped out too many {} brackets. Trying to find where you're missing one... Edit: Find this: Code:
$(document).ready(function(){ On the line before it, add a } |
Re: Age Calculation
Ok so I tried it this way. I compared the modified one against the original and made sure all of the { } were correct.
It's still not displaying the info. Code:
{* |
Re: Age Calculation
I see what you did.
You moved stuff around as well. Notice my code: Code:
{/literal} I ended the opening literal statement right before using smarty, and then started again after it. This is due, as I said before, smarty using {} and javascript as well... They don't play nice together. Your code keeps the smarty calls in literal Code:
var calday = {$extra_fields.0.field_value}; if its taken literally, it will be. The actual values are those. Another thing you did was change the form. Code:
<form name="birthday"> Yours: Code:
<form name="age"> Thing is, the script only outputs the variable to a form named birthday. You changed it to age, thus another issue. In any case... The code... Already stripped out some code, removed extra spaces, changed it so the form it outputs to is called age, changed form name to age and fixed literal placing. Tested and works... Code:
{* |
Re: Age Calculation
Thanks for all your help on this. I'm still only getting "Result" in the form box.
I did check the fields in the product. I still had 08 as the month, but that didn't make any difference when I changed it to 8 |
Re: Age Calculation
Don't use 08 or 09. Javascript won't accept it as a number.
Edit: Checked your site, going thru the javascript now to see what changed... Edit2: Found it. Your site: Code:
<input name="age" size="40" value="Result"><br> Proper: <form name="age"> <input name="age" size="40" value="Result"><br> </form> |
Re: Age Calculation
Quote:
Yeah that's what I had on there. I even check the source code. hmmm Maybe it's because of Firefox? I did see in the code that the var's are being set to the correct day,month,year. Edit: Nope IE blows too. :P OMG this is so stupid, why is it not working. I was asking before, is there a different way to store the calc and display it not using the form? Really what I'd like is the details page to look like is: Puppy Name Junior is 4 months, 2 days old. Other Sites Sell For: $699.00 Our price: $0.01 |
Re: Age Calculation
Doing this in the php file would seem to be easier and more efficient - if you are comfortable modifying the php files. You can continue to use the extra fields to capture the birthdate. In php, you can use the time function to get the current date. You can use strtotime to turn your string date in the extra fields to a timestamp so you can do the the math. Then, you use regular math to format the difference in seconds into years/months/days. While it is not doing exactly what you want, if you understand the concepts in this thread - it helps to explain how the extra fields are accessible in the php, and how to pass the result to the templates.
|
Re: Age Calculation
gb,
I struggled with java vs php. I understand just enough to be dangerous with both. However Java is easier and code is more widely available. I've looked into PHP Age caculators and I've found a few. I'm just drawing a blank on how to make it work in my X-Cart. |
Re: Age Calculation
I tried messing around with PHP code this week and I can get it to pull the extra field variables, but It's not displaying correctly on the site.
|
Re: Age Calculation
Quote:
That's a blanket problem, like "My car doesn't work". Nothing can help unless you actually describe what doesn't work :P Is it displaying at all? If you have it and it's own div in an {if} section, does the div show up and the variable is just missing? |
Re: Age Calculation
Here's the code in the tpl.
Code:
{* Here is the error. As you can see it's pull in the variables. Error: Smarty error: [in modules/Age_Calculation/age.tpl line 12]: syntax error: unrecognized tag: $day=(empty($day))?'1':$day; list($this_year, $this_month, $this_day) = explode(' ',date('Y n j')); $age = $this_year - $year; if ($month>$this_month||($this_month==$month&&$this_d ay<$day)) { $age--; (Smarty_Compiler.class.php, line 446) in /home/minfinge/public_html/littlepuppiesonline.com/include/lib/smarty/Smarty.class.php on line 1093 var day = 8; var month = 17; var year = 1977; function checkCurAge($year,$month,$day='') return $age; } |
Re: Age Calculation
php code does not go in the tpl files - it goes in the php files.
Perhaps you are closer to your desired result with the javascript as Tal has already provided code to you that works and you are more comfortable with javascript. If you are having trouble with the submit form, try taking that out - there is no reason for it as you are getting the data from extra fields, not a submission by the user. |
Re: Age Calculation
GB2...I have not gotten the Javascript to display the age calculation yet.
Look below "Our Price" It says Results. http://littlepuppiesonline.msidesigns.com/product.php?productid=1&cat=10&page=1 |
Re: Age Calculation
One possible issue - you have a form inside a form - which is not valid html.
I've not looked at the javascript - but maybe Tal can comment about where to put this code? If you want it inside the main form - maybe drop the form submission and just use the javascript date functions you have to output the data without a form submission |
Re: Age Calculation
Quote:
Actually that's what I'd prefer to do is just display the calc answer without the form. The form is only there because that's what came in the code I found online. |
Re: Age Calculation
Just noticed that when Tal told you he does not see the <form> in firebug, you suggested that maybe it was firefox. Firebug often does not display incorrect code. You'll see it in the page source, but firebug does not know what to do with it since it is wrong.
Seems to me that this is even more evidence that your problem is that your age form is being incorrectly placed inside another form. If so - your solution is to move it to a valid location or revise the javascript you have copied to not use a form submission. You could verify that this is indeed the problem by trying to move that form to a valid location. |
Re: Age Calculation
Quote:
I'd rather have the script not use the form and then I'd be able to place the tpl call where ever I wanted. Only problem is I don't understand how to display the age calc answer using java. Any help would be great. |
Re: Age Calculation
Code:
{* 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. |
Re: Age Calculation
THANKS TAL!!! That's awesome!!! I'm extremely grateful!!!
|
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 |
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 { |
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){ |
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() Tested and works. |
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 |
Re: Age Calculation
Quote:
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. |
All times are GMT -8. The time now is 10:47 PM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.