Page 1 of 1

[Solved] Problem inserting a php script using 2 user tags.

Posted: Sun May 18, 2008 5:14 pm
by Coume
Hello,

I spent my Sunday afternoon playing around CMS made simple and I do find it pretty easy in comparison with my previous CMS (Typo3...) but I am stuck on something pretty simple.

I want to display the page loading time at its bottom, which is calculated through a basic PHP script.
I read in the forum that one should use a custom usertag to insert a php script but I must be missing something...

Here is my template:
(...)

{loading_time_init}

(....)


          {loading_time_display}
       

Usertags:
{loading_time_init}
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];

{loading_time_display}
putenv('TZ=GMT');
echo date("Y-m-d h:i:sa \G\M\T \:\: ", time());
$endtime = microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime - $starttime;
$totaltime = round($totaltime,5);
echo "Page loaded in $totaltime seconds.";
The date gets displayed properly but the loading time is completely wrong.
2008-05-18 05:11:45pm GMT :: Page loaded in 1211130705.1812 seconds.
From what I understand, it looks like the starttime variable defined by the first usertag is not taken into consideration by the second usertag.

Could someone let me know what I am doing wrong? Because I really cannot understand :(

Thanks in advance.
Ludo

Re: Problem inserting a php script using 2 user tags.

Posted: Sun May 18, 2008 5:23 pm
by alby
Coume wrote: Could someone let me know what I am doing wrong? Because I really cannot understand :(
UDT are same to functions.
Second UDT don't know nothing of first UDT.
Or $starttime is GLOBAL variable or you must set $starttime in a common environment (for example with smarty).
View here for examples.

Alby

Re: Problem inserting a php script using 2 user tags.

Posted: Sun May 18, 2008 5:50 pm
by Coume
Thank you for the quick reply Alby.

I did try to use a global variable in my init script as follow
global $starttime;
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
but it still does not work :(

I am not sure if I understand properly how to use a http://uk.php.net/global in CMS :/

Re: Problem inserting a php script using 2 user tags.

Posted: Sun May 18, 2008 10:06 pm
by blast2007
Coume wrote: I did try to use a global variable in my init script as follow
...
but it still does not work :(
mmm I'm not sure but I think you have to pass $starttime to a session variable in this way:
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
session_register("starttime");
$_SESSION["starttime"] = $starttime;
then read your $starttime value from the other UDT with something like:
echo $_SESSION["starttime"];

// When you want to reset $starttime use the following instructions:
unset($_SESSION[starttime]);
unset($starttime);
But I'm not so sure, I'm not a guru. ;)
Test and let me know

Regards
blast

Re: Problem inserting a php script using 2 user tags.

Posted: Sun May 18, 2008 10:19 pm
by alby
Coume wrote: I did try to use a global variable in my init script as follow
global $starttime;
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
but it still does not work :(
You must do GLOBAL also in second UDT (if not it cannot know this variable):

Code: Select all

global $starttime;
putenv('TZ=GMT');
echo date("Y-m-d h:i:sa \G\M\T \:\: ", time());
$endtime = microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime - $starttime;
$totaltime = round($totaltime,5);
echo "Page loaded in $totaltime seconds.";
Alby

Re: Problem inserting a php script using 2 user tags.

Posted: Sun May 18, 2008 11:26 pm
by Wiedmann
Just putting this at the end of your template should do the same (without any UDT):

Code: Select all

{assign var=endtime value=0|microtime}
Page loaded in {$gCms->variables.starttime|microtime_diff:$endtime} seconds.

Re: Problem inserting a php script using 2 user tags.

Posted: Sun May 18, 2008 11:31 pm
by Dr.CSS
Can't you just use the same thing that's used in the source to show render time?...

Re: Problem inserting a php script using 2 user tags.

Posted: Mon May 19, 2008 7:16 am
by Coume
Thanks a lot Alby and Blast!
I was not aware that I needed to use the global function in the second UDT. I still have lots to learn :)
Wiedmann wrote: Just putting this at the end of your template should do the same (without any UDT):

Code: Select all

{assign var=endtime value=0|microtime}
Page loaded in {$gCms->variables.starttime|microtime_diff:$endtime} seconds.
Thanks a lot for this neat litttle code. I had no idea that a variables.starttime already existed in CMS Made Simple. I will need to search around to find all these to avoid duplicating function with a PHP script when there is no need to do so...

Regards,
Ludo

Re: Problem inserting a php script using 2 user tags.

Posted: Mon May 19, 2008 8:26 am
by alby
Coume wrote: I will need to search around to find all these to avoid duplicating function with a PHP script when there is no need to do so...
And as Mark said, if you look in the page's source, time render is already calculated

Alby

Re: Problem inserting a php script using 2 user tags.

Posted: Mon May 19, 2008 8:57 am
by Coume
alby wrote:
Coume wrote: I will need to search around to find all these to avoid duplicating function with a PHP script when there is no need to do so...
And as Mark said, if you look in the page's source, time render is already calculated

Alby
Interesting...

Where can I find the variable to use in order to re-use the below data?


Thanks
Ludo

Re: Problem inserting a php script using 2 user tags.

Posted: Mon May 19, 2008 9:51 am
by alby
Coume wrote: Where can I find the variable to use in order to re-use the below data?
Look to index/include.php or view post of Wiedmann

Alby