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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
Coume
Forum Members
Forum Members
Posts: 14
Joined: Tue Oct 16, 2007 12:24 pm

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

Post 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
Last edited by Coume on Mon May 19, 2008 7:16 am, edited 1 time in total.
alby

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

Post 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
Coume
Forum Members
Forum Members
Posts: 14
Joined: Tue Oct 16, 2007 12:24 pm

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

Post 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 :/
User avatar
blast2007
Power Poster
Power Poster
Posts: 508
Joined: Wed Aug 01, 2007 5:36 pm

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

Post 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
Last edited by blast2007 on Sun May 18, 2008 10:22 pm, edited 1 time in total.
alby

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

Post 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
Wiedmann
Forum Members
Forum Members
Posts: 233
Joined: Wed Mar 26, 2008 1:49 am

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

Post 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.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

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

Post by Dr.CSS »

Can't you just use the same thing that's used in the source to show render time?...
Coume
Forum Members
Forum Members
Posts: 14
Joined: Tue Oct 16, 2007 12:24 pm

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

Post 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
alby

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

Post 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
Coume
Forum Members
Forum Members
Posts: 14
Joined: Tue Oct 16, 2007 12:24 pm

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

Post 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
alby

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

Post 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
Post Reply

Return to “CMSMS Core”