473,657 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Usort Feeds By Relevance

59 New Member
Hi
I Am using Simplepie to display some rss feeds, currently it displays them in date and time order but i want it to display them in relevance order by using a usort function.
here is the code for the page

[PHP]<?php
/*************** *************** *************** *************** ********
MULTIFEEDS TEST PAGE

Nothing too exciting here. Just a sample page that demos integrated
Multifeeds support as well as cached favicons and perhaps a few other
things.

Lots of this code is commented to help explain some of the new stuff.
Code was tested in PHP 5.2.2, but *should* also work with earlier
versions of PHP, as supported by SimplePie (PHP 4.1).

*************** *************** *************** *************** ********/

// Include the SimplePie library, and the one that handles internationaliz ed domain names.
require_once('. ./simplepie.inc') ;
require_once('. ./idn/idna_convert.cl ass.php');

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(a rray(
'http://rss.news.yahoo. com/rss/topstories',
'http://news.google.com/?output=atom',
'http://rss.cnn.com/rss/cnn_topstories. rss'
));

// When we set these, we need to make sure that the handler_image.p hp file is also trying to read from the same cache directory that we are.
$feed->set_favicon_ha ndler('./handler_image.p hp');
$feed->set_image_hand ler('./handler_image.p hp');

// Initialize the feed.
$feed->init();



// Make sure the page is being served with the UTF-8 headers.
$feed->handle_content _type();



// Begin the (X)HTML page.
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Multifee ds Test page</title>
<link rel="stylesheet " href="../demo/for_the_demo/simplepie.css" type="text/css" media="screen" title="SimplePi e Styles" charset="utf-8" />
<style type="text/css">
div#site {
width:600px;
}
span.footnote {
white-space:nowrap;
}
h1 {
line-height:1.4em;
}
h4 {
padding-left:20px;
background-color:transpare nt;
background-repeat:no-repeat;
background-position:0 1px;
}
.clearBoth {
clear:both;
}
</style>
</head>
<body>
<div id="site">

<?php if ($feed->error): ?>
<p><?=$feed->error()?></p>
<?php endif ?>

<div class="chunk">
<h1>Quick-n-Dirty Multifeeds Demo</a></h1>
</div>

<?php
// Let's loop through each item in the feed.
foreach($feed->get_items() as $item):

// Let's give ourselves a reference to the parent $feed object for this particular item.
$feed = $item->get_feed();
?>

<div class="chunk">
<h4 style="backgrou nd-image:url(<?php echo $feed->get_favicon( ); ?>);"><a href="<?php echo $item->get_permalink( ); ?>"><?php echo html_entity_dec ode($item->get_title(), ENT_QUOTES, 'UTF-8'); ?></a></h4>

<!-- get_content() prefers full content over summaries -->
<?php echo $item->get_content( ); ?>

<?php if ($enclosure = $item->get_enclosure( )): ?>
<div>
<?php echo $enclosure->native_embed(a rray(
// New 'mediaplayer' attribute shows off Flash-based MP3 and FLV playback.
'mediaplayer' => '../demo/for_the_demo/mediaplayer.swf '
)); ?>
</div>
<?php endif; ?>

<p class="footnote ">Source: <a href="<?php echo $feed->get_permalink( ); ?>"><?php echo $feed->get_title(); ?></a> | <?php echo $item->get_date('j M Y | g:i a'); ?></p>
</div>

<?php endforeach ?>

<p class="footnote ">This is a test of the emergency broadcast system. This is only a test&hellip; beeeeeeeeeeeeee eeeeeeeeeeeep!</p>

</div>
</body>
</html>[/PHP]
This is the sort of usort i will use

[PHP]usort($feed,'fu nnySort');

function funnySort($a,$b )
{
// assign numerical value so we can sort on
// whether "term" is in title or content or neither

$term = 'funny';

$x = 0;
if (stripos($a->get_title(),$t erm))
$x = 2;
elseif (stripos($a->get_content(), $term))
$x = 1;

$y = 0;
if (stripos($b->get_title(),$t erm))
$y = 2;
elseif (stripos($b->get_content(), $term))
$y = 1;

return $x <= $y
} [/PHP]
How can i make this work together so that it displays the most relevant posts first?

Thanks
Mar 2 '08 #1
1 1591
ronverdonk
4,258 Recognized Expert Specialist
Do not start a second thread when the first one does not run!

This is a duplicate of thread http://www.thescripts.com/forum/thread771947.html

moderator
Mar 3 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1628
by: Spidah | last post by:
I am having trouble using the usort function inside a class. Is this possible and if so what is the correct way to pass the compare function to it? I assume this function must also be inside the class in which usort is called. Hamilton
5
2963
by: Alan Little | last post by:
I wrote a sorting function to use with usort(). It worked fine, and I went on with the development of my code. At one point I changed the name of the sorting function, but forgot to update it in the usort() call. Yet, it still was working. I removed the usort() call to make sure my data didn't just happen to be arriving in sorted order, and it wasn't. When I removed the usort() call, the code broke; when I put it back, it worked. I...
1
2444
by: leegold | last post by:
Show full header Is there any way to make relevance when using boolean mode more useful? If not, are there plans in the Fulltext development "todo" for making it useful? I'm thinking of just counting the keyword occurences in records with a PHP script somehow and using that to order the records somehow. Or doing a non boolean fulltext search w/relevance first and then melding that in somehow with a boolean search. See my results...
1
1321
by: ojsimon | last post by:
Hi does anyone know how to combine several rss feeds and sort by relevance to a few specified keywords? Thanks
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.