X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Age Calculation (https://forum.x-cart.com/showthread.php?t=53578)

minfinger 05-03-2010 11:35 AM

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....

Shamun 05-03-2010 01:56 PM

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.

minfinger 05-03-2010 06:05 PM

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

Shamun 05-03-2010 06:37 PM

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"}

minfinger 05-03-2010 06:48 PM

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.

Shamun 05-03-2010 06:51 PM

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 :)

minfinger 05-03-2010 06:56 PM

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?

Shamun 05-03-2010 06:57 PM

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.

minfinger 05-03-2010 07:01 PM

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.

minfinger 05-03-2010 07:46 PM

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!

Shamun 05-03-2010 07:51 PM

Re: Age Calculation
 
Try using this:

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}
       

*}


{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()

{
{/literal}
var calday = {$extra_fields.0.field_value};

var calmon = {$extra_fields.1.field_value};

var calyear = {$extra_fields.2.field_value};
{literal}
    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";   





    }

}

$(document).ready(function(){

calage();
});


</script>
{/literal}



<form name="birthday">


<input name="age" size="40" value="Result"><br>
 </form>






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.

minfinger 05-04-2010 05:49 AM

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:

{*
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}
       

*}


{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()

{
{/literal}

var calday = {$extra_fields.0.field_value};
var calmon = {$extra_fields.1.field_value};
var calyear = {$extra_fields.2.field_value};

{literal}
    {

        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).ready(function(){

calage();
});


</script>
{/literal}



<form name="age">
<input name="age" size="40" value="Result"><br>
 </form>


minfinger 05-04-2010 05:51 AM

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>?

Shamun 05-04-2010 02:34 PM

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(){
    calage();
});


On the line before it, add a }

minfinger 05-04-2010 06:05 PM

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:

{*
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}

*}

{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();
var calday = {$extra_fields.0.field_value};
var calmon = {$extra_fields.1.field_value};
var calyear = {$extra_fields.2.field_value};

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);

        document.birthday.age.value=dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days";

}

$(document).ready(function(){

calage();
});

</script>
{/literal}

<form name="age">
<input name="age" size="40" value="Result"><br>
 </form>


Shamun 05-04-2010 08:28 PM

Re: Age Calculation
 
I see what you did.
You moved stuff around as well.

Notice my code:
Code:

{/literal}
var calday = {$extra_fields.1.field_value};

var calmon = {$extra_fields.0.field_value};

var calyear = {$extra_fields.2.field_value};
{literal}
    if(curday == "" || curmon=="" || curyear=="" || calday=="" || calmon=="" || calyear=="")


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};
var calmon = {$extra_fields.1.field_value};
var calyear = {$extra_fields.2.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">
<input name="age" size="40" value="Result"><br>
 </form>

That was the original.

Yours:
Code:

<form name="age">
<input name="age" size="40" value="Result"><br>
 </form>


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:

{*
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);
        document.age.age.value=dife[0]+" years, "+dife[1]+" months, and "+dife[2]+" days";
    }

$(document).ready(function(){
calage();
});
{/literal}
</script>


<form name="age">
<input name="age" size="40" value="Result"><br>
 </form>


minfinger 05-04-2010 08:56 PM

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

Shamun 05-04-2010 09:55 PM

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>

minfinger 05-05-2010 12:44 PM

Re: Age Calculation
 
Quote:

Originally Posted by Tal
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>


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

gb2world 05-05-2010 04:06 PM

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.

minfinger 05-06-2010 05:23 AM

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.

minfinger 05-08-2010 10:27 AM

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.

Shamun 05-08-2010 02:44 PM

Re: Age Calculation
 
Quote:

Originally Posted by minfinger
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.


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?

minfinger 05-08-2010 02:48 PM

Re: Age Calculation
 
Here's the code in the tpl.

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}
       
*}
var day = {$extra_fields.0.field_value};
var month = {$extra_fields.1.field_value};
var year = {$extra_fields.2.field_value};
function checkCurAge($year,$month,$day='') {
$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_day<$day)) {
    $age--;
}
return $age;
}


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; }

gb2world 05-08-2010 03:19 PM

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.

minfinger 05-08-2010 03:23 PM

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

gb2world 05-08-2010 03:41 PM

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

minfinger 05-08-2010 03:43 PM

Re: Age Calculation
 
Quote:

Originally Posted by gb2world
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


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.

gb2world 05-08-2010 04:01 PM

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.

minfinger 05-08-2010 04:07 PM

Re: Age Calculation
 
Quote:

Originally Posted by gb2world
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.


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.

Shamun 05-08-2010 04:24 PM

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.

minfinger 05-08-2010 05:53 PM

Re: Age Calculation
 
THANKS TAL!!! That's awesome!!! I'm extremely grateful!!!

minfinger 05-08-2010 07:10 PM

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

Shamun 05-08-2010 07:50 PM

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;
}


minfinger 05-09-2010 08:15 AM

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";}


Shamun 05-09-2010 01:36 PM

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.

minfinger 05-09-2010 02:38 PM

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

Shamun 05-09-2010 02:53 PM

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.


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.