Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2009-08-03 19:27:40
- b-monkey
- New Member
- Registered: 2009-08-03
- Posts: 5
txp tags in php
Sorry if this has been asked before, I searched but didn’t find anything…
I’m trying to put a php code into my website but need to also include a txp tag within that php code. Is it possible?
For example, there’s a countdown script where I’d like to have <txp:expires format=”%Y,%m,%d,%H,%M” /> tag inside.
This is the countdown script:
<txp:php>
// countdown function
// parameters: (year, month, day, hour, minute)
countdown(*<txp:expires format="%Y,%m,%d,%H,%M" />*);
//--------------------------
// author: Louai Munajim
// website: www.elouai.com
//
// Note:
// Unix timestamp limitations
// Date range is from
// the year 1970 to 2038
//--------------------------
function countdown($year, $month, $day, $hour, $minute)
{
// make a unix timestamp for the given date
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
// get current unix timestamp
$today = time();
$difference = $the_countdown_date - $today;
if ($difference < 0) $difference = 0;
$days_left = floor($difference/60/60/24);
$hours_left = floor(($difference - $days_left*60*60*24)/60/60);
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
// OUTPUT
echo "Today's date ".date("F j, Y, g:i a")."<br/>";
echo "Countdown date ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
echo "Countdown ".$days_left." days ".$hours_left." hours ".$minutes_left." minutes left";
}
</txp:php>
Any ideas?
Thanks!
EDIT: added bc..
for code display purposes
Last edited by Bloke (2009-08-03 20:11:56)
Offline
Re: txp tags in php
b-monkey wrote:
I’m trying to put a php code into my website but need to also include a txp tag within that php code. Is it possible?
Yes. Each tag has an equivalently-named function. You pass an array of name-value pairs as the arguments to the function. So in this case:
countdown( expires(array("format" => "%Y,%m,%d,%H,%M")) );
Use an underscore to join multi-word tags, e.g. recent_comments()
.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
Re: txp tags in php
I think if you use $thisarticle['expires']
you already get a unix timestamp.
Shoving is the answer – pusher robot
Offline
#4 2009-08-03 20:08:14
- b-monkey
- New Member
- Registered: 2009-08-03
- Posts: 5
Re: txp tags in php
Thanks Bloke and hakjoon for your quick responses! Unfortunately, I get an error with both solutions:
Parse error: syntax error, unexpected $end in /home/website/public_html/textpattern/publish/taghandlers.php(3082) : eval()’d code on line 17
Offline
Re: txp tags in php
The following works for me.
<txp:article_custom id="33">
<h2><txp:title/></h2>
<txp:php>
// countdown function
// parameters: (year, month, day, hour, minute)
countdown(expires(array("format" => "%Y,%m,%d,%H,%M")));
//--------------------------
// author: Louai Munajim
// website: www.elouai.com
//
// Note:
// Unix timestamp limitations
// Date range is from
// the year 1970 to 2038
//--------------------------
function countdown($year, $month, $day, $hour, $minute)
{
// make a unix timestamp for the given date
$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);
// get current unix timestamp
$today = time();
$difference = $the_countdown_date - $today;
if ($difference < 0) $difference = 0;
$days_left = floor($difference/60/60/24);
$hours_left = floor(($difference - $days_left*60*60*24)/60/60);
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
// OUTPUT
echo "Today's date ".date("F j, Y, g:i a")."<br/>";
echo "Countdown date ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
echo "Countdown ".$days_left." days ".$hours_left." hours ".$minutes_left." minutes left";
}
</txp:php>
</txp:article_custom>
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: txp tags in php
Couple of random ideas that may or may not be the problem:
- are you putting this code in an article? If so, consider turning off Textile processing in the article or surrounding your
==<txp:php>... </txp:php>==
tags with double-equals signs to stop Textile from trying to mark up your code (assuming you’re not using==
within your code itself! If you are, you need to usenotextile..
or put your code in a Form, or write it as a plugin) - is the expires() tag actually returning a list of items that you are passing to your function? I suspect it’s passing, for example, the string
2009,08,03,15,43
as the first parameter to your functionm, and leaving the others empty… might be wrong here - hakjoon’s solution requires
global $thisarticle;
to be in your function or you won’t be able to get at the variable
EDIT: MattD was quicker :-)
Last edited by Bloke (2009-08-03 20:29:24)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
Re: txp tags in php
I think Bloke may be right on #2. My example only appears to work but the date it outputs as Countdown date isn’t the correct expiration date.
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
#8 2009-08-03 20:49:10
- b-monkey
- New Member
- Registered: 2009-08-03
- Posts: 5
Re: txp tags in php
Bloke, you’re right, it’s probably passing the date properly, must be something else that is causing the error..
Anyway, I and now I’m getting this error:
Fatal error: Cannot redeclare countdown() (previously declared in /home/website/public_html/textpattern/publish/taghandlers.php(3082) : eval()’d code:15) in /home/website/public_html/textpattern/publish/taghandlers.php(3082) : eval()’d code on line 34
I’m using the code in a form that is a listform for an article_custom tag.
Offline
Re: txp tags in php
This seems to work but it’s messy.
countdown(expires(array("format" => "%Y")),expires(array("format" => "%m")),expires(array("format" => "%d")),expires(array("format" => "%H")),expires(array("format" => "%M")));
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: txp tags in php
b-monkey wrote:
Fatal error: Cannot redeclare countdown()
If you put it in a form it will have to be a form you only call once via <txp:output_form form="my_code_form" />
. If you use it as a listform, it will be called once for each article in your article list and thus will try to recreate the countdown function each time, giving you the error.
If you need any resource material, have a look at Textbook particularly the sections on Orientation and the Tag Reference for ideas on how stuff hangs together.
Last edited by Bloke (2009-08-03 20:57:33)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
Re: txp tags in php
b-monkey wrote:
Fatal error: Cannot redeclare countdown() (previously declared
The function can’t be declared more than once. If you have it in a form that is being included for each article in a list you’d get this error.
Bloke beat me to it.
If you seperate the function declaration into one form and output it at the top of your page you can then use something like
<txp:php>countdown(expires(array("format" => "%Y")),expires(array("format" => "%m")),expires(array("format" => "%d")),expires(array("format" => "%H")),expires(array("format" => "%M")));
</txp:php>
in your article form.
Last edited by MattD (2009-08-03 20:59:56)
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: txp tags in php
Of course the best thing would be to use the awesomeness that is ied_plugin_composer and just make countdown a standard tag.
Shoving is the answer – pusher robot
Offline