Camen Design Forum

How to get latest posts

append delete Jos

First thanks for this great and wonderfull forum software that really empowers users. Its simplicity makes it a joy to use.

I have a forum with multiple subforums and need a list of latest posts across all the forums. I should be the x latest forums no matter what forum. I cannot find a way to do it with the forumsoftware.

Has anyone solved the problem and is willing to share the script? Othewise i'll have to do it myself :)

/Jos

Reply RSS

Replies

append delete #1. Martijn

They are split up between index files for the different subfora and I do not know of any existing scripts that pull it together. So I am afraid you will have to do it yourself (or find someone to do it for you).

append delete #2. Jos

I have a quick and dirty script running allready . Will post it later. It just loads the index.xml files in the forums and sorts them according to pubDate. Nothing fancy but it does what i need.

Thanks

#3. Jod

This post was deleted by its owner

append delete #4. Jos

With the help from Stackoverflow and Google :)
Quick and dirty small script that grabs the entrys and sorts them per date. The interesting part is between //====.
Defining feeds this way suits me well. You prabably need to do something recursive. The last part i just use to test and display.
This works fine for me right now but probably not for others.

%PHP
<?
//Define feeds
foreach (glob("*/index.xml") as $filename) {
$feeds[] = $filename;
}
//================================
// Get all feed entries
$entries = array();
foreach ($feeds as $feed) {
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, $xml->xpath('/rss//item'));
}

// Sort feed entries by pubDate (descending)
usort($entries, function ($x, $y) {
return strtotime($y->pubDate) - strtotime($x->pubDate);
});

//================================
foreach ($entries as $entry){

echo "$entry->author." : ".$entry->title."<p>".$entry->pubDate."</p><p>".strip_tags(mb_substr($entry->description, 0, 100, 'UTF-8')) . '...';
?> 
%
append delete #5. Martijn

This skips the main forum and only scans subfora, right? Other than that, it looks like the shortest and quickest solution to the problem. Thanks for sharing.

append delete #6. Jos

Yes. This only scans subfora and only one level down. It you have nested fora, probably a good idea with many fora, then you probably need to use http://php.net/manual/en/class.recursivedirectoryiterator.php.
For me the above is very simple and works wonderful in my setup.

I've cleaned the code a bit this morning and put it in a function.

%
<?
function latestFeed($n) {
	$latest[]=NULL;
	foreach (glob("forum/*/index.xml") as $filename) {
		$feeds[] = $filename;
		}
// Get all feed entries
	$entries = array();
	foreach ($feeds as $feed) {
    	$xml = simplexml_load_file($feed);
    	$entries = array_merge($entries, $xml->xpath('/rss//item'));
	}

// Sort feed entries by pubDate (descending)
	usort($entries, function ($x, $y) {
    	return strtotime($y->pubDate) - strtotime($x->pubDate);
	});

	foreach (array_slice($entries,0,$n) as $entry){
		$entry->description=strip_tags(mb_substr($entry->description, 0, 100, 'UTF-8')) . '...';
		$latest[]=$entry;
	}
return $latest;
}
?>
%

I call the above with the following:

%
    require_once 'forum/joinfeeds.php';
    $latestfeeds=latestFeed(5);
%

to get the latest 5 posts.

append delete #7. hank1

Do you have a demo of this?

append delete #8. Jos

Only in an internal site.

Just try it. Its very simple and works nicely. I use it exactly as posted here. Nothing changed.

append delete #9. hank1

Can you attach a screenshot? Does this fetch ALL boards or is this just the index?

append delete #10. Martijn

This only scans subfora and only one level down.

It does not look at the index at all.

append delete #11. Jos

There is no point in attaching a screenshot. Your implementation will look different.
My templating system just prints out entryes from an array. I'm on fatfreeframework (f3) and use bootstrap 3
My code looks like . You should get the idea

%HTML
<repeat group="{{ @latestfeeds}}" value="{{ @item }}" counter="{{ @ctr }}">

    <small>
     <p><a href="{{@item->link}}">{{@item->title}}</a></p>
     <p>{{@item->description}}</p>
     <p><i>Af {{@item->author}}</i></p>
          <HR class="feeds">
   </small>
   
  </repeat>
%
append delete #12. hank1

How do I get this to work? Tried creating a test.php and runing it.

append delete #13. jos

Its very simple.
Just save the joinfeeds.php file and then have your test.php be like this

%
<?php
    require_once 'forum/joinfeeds.php';
    $latestfeeds=latestFeed(5);

//Print some stuff from entries
foreach($latestfeeds as $feed) {

echo $feed->link," ",$feed->title," ",$feed->description
}

?>

%

I havent tested the above so experiment and use php.net for help. You have to take a look at the index.xml files to see what is available

:: @Jos added on 25 Jul ’16 · 18:27

Beware of path of require_once 'forum/joinfeeds.php';

:: @Jos added on 25 Jul ’16 · 18:29

And remember this is something that is perfect for me. It does not necessarely something for your setup, but its simple and easy to adapt to your needs. No need to a complex tools when something as simple does the job.

append delete #14. tmyusuf

Actually, what do you want from this forum

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn