Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Using php: show articles with soon date
I have list of people where each article corellates to some man. I want to make something likeness birthday notification. Every man’s article has it’s own post date. It refers to month\date (not year) of birthday.
I set year to 2006 to be sure that article was ‘posted” :)
Then i call articles form sections pupils and teachers:
<txp:chh_article_custom section="pupils,teachers" listform="list_birthday" limit="99" />
I want to list only articles where month of “publish” is current or next. I think it should be php solution for this…
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline
Re: Using php: show articles with soon date
This should give the articles where the ‘publish’ month is the same as the current month (not tested):
<txp:php>
echo chh_article_custom(array(
'section' => 'pupils,teachers',
'listform' => 'list_birthday',
'limit' => '99',
'month' => strftime('%Y-%m')
));
</txp:php>
If you’re ordering by date, you could use two such constructs to get both months. The next month would be:
<txp:php>
echo chh_article_custom(array(
'section' => 'pupils,teachers',
'listform' => 'list_birthday',
'limit' => '99',
'month' => strftime('%Y-%m', strtotime('+1 month'))
));
</txp:php>
Offline
Re: Using php: show articles with soon date
With some corrections it works :)
Another question: can i call articles were post date (month\date – not year) current date plus 2 or 3 weeks?
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline
#4 2007-10-26 16:44:17
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Using php: show articles with soon date
chh_article_custom can do that.
Offline
Re: Using php: show articles with soon date
If chh_article_custom is unable to do this, then you could try something like this (not tested, but requires no plugins):
<txp:php>
$begin = strtotime('-5 days');
$end = strtotime('+2 weeks');
$rows = safe_rows(
'*, unix_timestamp(Posted) as uPosted',
'textpattern',
'Section IN('pupils','teachers') AND uPosted > '.$begin.' AND uPosted < '.$end.
' ORDER BY by Posted'
)
foreach($rows as $row)
{
populateArticleData($row);
echo parse_form('list_birthday');
}
unset($GLOBALS['thisarticle']);
</txp:php>
Offline
Re: Using php: show articles with soon date
Thanks ruud :) But the same time i discovered another (if your works – i’ll test it later) way:
Form call_birthday, wich is called from page template:
<txp:php>
$outPutArticle=chh_article_custom(array(
'section' => 'pupils,teachers',
'listform' => 'list_birthdays',
'limit' => '99',
'sortby' => 'Posted',
'sortdir' => 'asc'
));
if (strlen($outPutArticle)==0) {
echo "<div class=\"message\">";
echo "Now bithdays in 2 weeks.";
echo "</div>";
}
else {
echo "<div class=\"message\">";
echo "<h3>Birthdays in two weeks:</h3>";
echo "</div>";
echo $outPutArticle;
}
</txp:php>
And this is form list_birthdays:
<txp:php>
$gho_link= $GLOBALS['thisarticle']['permlink'];
$gho_title=$GLOBALS['thisarticle']['title'];
$articleTime=$GLOBALS['thisarticle']['posted']; // time of article in unix-time
$articleDaysFromNY=strftime("%j",$articleTime); // converting into days since 00:00 1st Jan - simply the current day number
$todayTime=time(); // time now in unix-time
$todayDaysFromNY=strftime("%j",$todayTime); // converting into days since 00:00 1st Jan - simply the current day number
$diff=$articleDaysFromNY-$todayDaysFromNY; // the differense between days of article and current date
$daysPast = 0; // if less then 0 - look for past birthdays
$daysFuture = 144; // look for some days in future if there will be birthday
// checking the differense. if it suites our settings - output suitable article
// $diff varies form $daysPast till $daysFuture ($daysPast < $diff < $daysFuture)
if ($diff <= $daysFuture && $diff >= daysPast) {
//echo $articleTime." ".$articleDaysFromNY." ".$todayDaysFromNY." ".$diff."<br>\n";
echo "<div class=\"birthday\">";
echo "<span class=\"persona\">\n<a href=\"".$gho_link."\">".$gho_title."</a>\n</span>";
echo " - <span class=\"date\">".strftime("%e %B",$articleTime)."</span>";
echo "</div>";
}
</txp:php>
It’s sounds strange but it works :) But there is one trouble: the permlink called by this string…
echo "<span class=\"persona\">\n<a href=\"".$gho_link."\">".$gho_title."</a>\n</span>";
…outputs the link of currently viewed page, not the url to article with birthday.
For example:
- This is the output on frontpage (http://school1118.ru/):
<div class="birthday">
<span class="persona">
<a href="">Ploeva Ella</a>
</span> - <span class="date"> 9 November</span>
As you can see – my code doesn’t outputs permlink… Why?
P.S. Site url is school1118.ru is protected by .htaccess. Login is admin
and pass is demo
;)
Last edited by the_ghost (2007-10-29 12:58:26)
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline
Re: Using php: show articles with soon date
the_ghost,
because $GLOBALS['thisarticle']['permlink'];
won’t work – it outputs nothing by itself, it requires friend and foe to work. Instead we need something else, like following. So try this at top of your *list_birthdays*-form’s code
global $thisarticle;
$gho_link = permlinkurl($thisarticle);
And voilá, it works like charm.
Cheers!
Last edited by Gocom (2007-10-29 13:19:31)
Offline
Re: Using php: show articles with soon date
that call_birthdays solution might work, but is not really efficient, because it pulls a lot of articles from the database and then throws most of them away because they have the wrong date.
Offline
Re: Using php: show articles with soon date
Gocom thanks! It works! But I don’t understand why $GLOBALS['thisarticle']['title']
works without
2Ruud, thanks for your opinion. But i’m glad to see that my method works instead of it’s scratching left ear by right leg :)
And as it’s my code I orient in it :)
But of course, you solution is much more effective. I’m thinking – may it’s worth to plugin in this base?
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline
Re: Using php: show articles with soon date
the_ghost wrote:
But I don’t understand why
$GLOBALS['thisarticle']['title']
works without
It just the way it have been done in the core. Firstly they work completely different way, as the permlink is container-tag and title is single tag. It have nothing to do with your code :)
Cheers!
Last edited by Gocom (2007-10-29 17:09:34)
Offline
Re: Using php: show articles with soon date
the_ghost wrote:
2Ruud, thanks for your opinion. But i’m glad to see that my method works instead of it’s scratching left ear by right leg :)
And as it’s my code I orient in it :)
Of course, it’s always better to use code that you can understand (and thus maintain) yourself.
To make writing code easier: instead of echo "<div class=\"birthday\">";
, you can do echo '<div class="birthday">';
. The double quotes are only needed if you use $variables inside strings, but since you don’t (which is good, I think), you can use single quotes instead so you don’t have to escape the double quotes that occur in the HTML.
$GLOBALS['thisarticle']['permlink'];
= permlink(array());
$GLOBALS['thisarticle']['title'];
= title(array());
<txp:php>
# converting article time into days since 00:00 1st Jan
$birthday = strftime("%j", $GLOBALS['thisarticle']['posted']);
# converting current time into days since 00:00 1st Jan - simply the current day number
$today = strftime("%j", time());
$daysPast = 0; # if more then 0 - look for past birthdays
$daysFuture = 144; # look for some days in future if there will be birthday
if (
# birthday in near future
($birthday - $today < $daysFuture) or
# birthday in near future (but next year)
($today + $daysFuture > 365 and 365 + $birthday - $today < $daysFuture) or
# birthday just passed
($today - $birthday < $daysPast) or
# birthday just passed (last year)
($today < $daysPast and 365 + $today - $birthday < $daysPast))
{
echo parse('
<div class="birthday">
<span class="persona">
<txp:permlink><txp:title></txp:permlink>
<span class="date"><txp:posted format="%e %B"></span>
</div>
');
}
</txp:php>
Offline
Re: Using php: show articles with soon date
ruud thanks for notice :)
I just was thinking over this. I think i’ll rewrite all code and all var will input to the output :) through 'text'.$var.'yet text'
Once another – is this worth to become plugin?
Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?
Offline