New Mod -- Display related stories

Register an Account
Pligg Chat Room
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-13-2007, 03:33 PM
New Pligger
 
Join Date: Mar 2007
Posts: 9
Hi All,

I wrote a quick mod for something I needed on a site, figured I would share it here.
This mod will display a list of related stories to the one you are currently viewing. The relevance is done based on tags (the more tags in common the more relevant) and is limited to 10 stories. It ain't pretty and it is not in a module but it works.

1. Add the template declarations to story.php in the root directory of you pligg install

PHP Code:
    // for show who voted
        
$main_smarty->assign('user_url'getmyurl('userblank'""));
        
$main_smarty->assign('voter'who_voted($id'small'));
// for show related stories
        
$main_smarty->assign('related_title_url'getmyurl('storytitle'""));
        
$main_smarty->assign('related_story'related_stories($id$link->tags$link->category)); 
The bottom three lines are the addition

2. Add the needed function in html1.php located in /libs

PHP Code:
function related_stories($storyid$related_tags$category){
    
// this returns similar stories based on tags in common and in the same category
    
    
global $db;
    
$related_tags="'".str_replace(", ","', '",$related_tags)."'"// This gives us the proper string structure for IN SQL statement
    // Select 10 stories that share tags with the current story and order them by number of tags they share
    
    
$sql "SELECT ".table_links.".link_title, ".table_links.".link_title_url, COUNT( ".table_tags.".tag_link_id ) AS relevance, ".table_tags.".tag_link_id FROM ".table_tags.", ".table_links."    WHERE ".table_tags.".tag_words IN (    ".$related_tags."    ) AND ".table_tags.".tag_link_id = ".table_links.".link_id    AND ".table_links.".link_status = 'published' AND NOT ".table_links.".link_id = ".$storyid;
    
//comment the following line out if you want this to work accross categories
    
$sql.= " AND ".table_links.".link_category = ".$category;
    
$sql.= " GROUP BY ".table_tags.".tag_link_id, ".table_links.".link_title, ".table_links.".link_title_url ORDER BY relevance DESC LIMIT 10";
    
$related_stories mysql_query($sql);
    
$related_story = array();
    while (
$rows mysql_fetch_array ($related_storiesMYSQL_ASSOC)) array_push ($related_story$rows);
    return 
$related_story;    

I put it just below who_voted()

3. Make it show up in your templates. The following is based on the future pligg template so it will need adaptation.

First of you need to add a new declaration to lang.conf located in /libs
Code:
PLIGG_Visual_Story_RelatedStory = "Related Stories"
Then you need to create template for this new section, create a new file in your futurepligg directory (/templates/futurepligg03 as of the current version of futurepligg) called story_related_stories.tpl and put the following code in it
Code:
{config_load file="/libs/lang.conf"}
<div id="comments">
    <h2>{#PLIGG_Visual_Story_RelatedStory#}</h2>
    <div class = "whovotedwrapper" id = "relatedstories">
        <ol
        {section name=nr loop=$related_story}
            ><li><a href = "{$related_title_url}{$related_story[nr].link_title_url}">{$related_story[nr].link_title}</a><br/></li
        {/section}
        ></ol><br/>
    </div>
</div>

{php}
?>
<style type="text/css">
  /* allow room for 3 columns */
  div#idwhovotedwrapper ol
  {
    width: 35em;
    list-style-type: none;
  }

  /* float & allow room for the widest item */
  div#idwhovotedwrapper ol li
  {
    float: left;
    width: 10em;
  }

  /* stop the float */
  div#idwhovotedwrapper br
  {
    clear: left;
  }

  /* separate the list from subsequent markup */
  div#idwhovotedwrapper div.whovotedwrapper
  {
    margin-bottom: 1em;
  }
</style>
<?php
{/php}
Last step, call the template from the main story template, story_center.tpl (/templates/futurepligg03 as of the current version of futurepligg) by adding
Code:
{include file=$the_template."/story_related_stories.tpl"}
at the very bottom

That's it, hope someone finds it useful.

Bert

Last edited by revenazb; 03-13-2007 at 10:11 PM.
Reply With Quote
  #2 (permalink)  
Old 03-13-2007, 03:39 PM
kbeeveer46's Avatar
Pligg Developer/Admin
Pligg Version: 0
Pligg Template: none
 
Join Date: Jun 2006
Location: Muncie, Indiana
Posts: 3,215
Very cool. I'll get this up and running and add it to Pligg if I can.

Last edited by kbeeveer46; 03-13-2007 at 03:44 PM.
Reply With Quote
  #3 (permalink)  
Old 03-13-2007, 04:04 PM
kbeeveer46's Avatar
Pligg Developer/Admin
Pligg Version: 0
Pligg Template: none
 
Join Date: Jun 2006
Location: Muncie, Indiana
Posts: 3,215
A few comments:

1. I think you left the echo $sql; in the code :P

2. I submitted 3 stories and all three of them had 1 or 2 similar tags but nothing seemed to be showing under related stories.
Reply With Quote
  #4 (permalink)  
Old 03-13-2007, 04:25 PM
New Pligger
 
Join Date: Mar 2007
Posts: 9
Quote:
Originally Posted by kbeeveer46 View Post
A few comments:

1. I think you left the echo $sql; in the code :P

2. I submitted 3 stories and all three of them had 1 or 2 similar tags but nothing seemed to be showing under related stories.
1. Sorry about that

2. A few things to check for the code check a few things when selecting the related stories, mainly:
  • Same category
  • Not the current story
  • Story status is published

If that is not source of the problem then I am not sure, it works for me

Bert

Last edited by revenazb; 03-13-2007 at 04:41 PM.
Reply With Quote
  #5 (permalink)  
Old 03-13-2007, 04:45 PM
kbeeveer46's Avatar
Pligg Developer/Admin
Pligg Version: 0
Pligg Template: none
 
Join Date: Jun 2006
Location: Muncie, Indiana
Posts: 3,215
Thanks Works like a charm now. All of the stories were on the upcoming page before.
Reply With Quote
  #6 (permalink)  
Old 03-13-2007, 08:53 PM
Banned
Pligg Version: 9.8.
Pligg Template: Custom
 
Join Date: Feb 2007
Location: Canada
Posts: 796
Please post the solution so others will know what to do.

Also, what is html1.php? What version are you using?

Geoserv

Last edited by Geoserv; 03-13-2007 at 08:59 PM.
Reply With Quote
  #7 (permalink)  
Old 03-13-2007, 09:18 PM
Banned
Pligg Version: 9.8.
Pligg Template: Custom
 
Join Date: Feb 2007
Location: Canada
Posts: 796
After much searching, html1.php is located in libs, can you update the mod to show this?

So is lang.conf

Geoserv

Last edited by Geoserv; 03-13-2007 at 09:31 PM.
Reply With Quote
  #8 (permalink)  
Old 03-13-2007, 09:35 PM
Banned
Pligg Version: 9.8.
Pligg Template: Custom
 
Join Date: Feb 2007
Location: Canada
Posts: 796
I am having an issue with this mod, its a great feature, but it doesn't seem to work. Also, it appears at the bottom of the screen.

http://www.newsdots.com/story.php?title=CSS-Superdouche

Geoserv
Reply With Quote
  #9 (permalink)  
Old 03-13-2007, 10:07 PM
New Pligger
 
Join Date: Mar 2007
Posts: 9
Quote:
Originally Posted by Geoserv View Post
Please post the solution so others will know what to do.

Also, what is html1.php? What version are you using?

Geoserv
I am using the latest version 0.91 I think it is.
The solution referred too is simply that the mod filters out unpublished stories, that is unpublished/upcoming stories do not show up as related stories. You can easily change that by editing the SQL query.

I have update the mod to explain where the files are.
Reply With Quote
  #10 (permalink)  
Old 03-13-2007, 10:09 PM
kbeeveer46's Avatar
Pligg Developer/Admin
Pligg Version: 0
Pligg Template: none
 
Join Date: Jun 2006
Location: Muncie, Indiana
Posts: 3,215
Yes, the SQL query only pulls published stories but you can easily change that to just say link_status != discard to get published and unpublished.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Related Stories from Digg, Twitter, YouTube alpapa23 Questions and Comments 0 08-04-2009 03:50 PM
Related Stories on Story Page canadianguy33 Questions and Comments 8 03-22-2008 02:39 PM
Related Stories Geoserv Questions and Comments 15 07-08-2007 11:21 AM
Related stories empty when tags separated by space aaronpais Questions and Comments 5 06-18-2007 10:32 PM
How can you make "Queued Stories" display promoted stories as well? gragland Questions and Comments 0 06-18-2006 12:24 AM


Pligg Modules and Pligg Templates from Pligg Pro Find support on the Pligg CMS Forum - 24 hours a day! Make a donation to support Pligg CMS development