Hello

September 10, 2010, 11:41:42 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?
Login with username, password and session length
What sNews Is: - sNews is a simple, basic, customizable CMS tool suitable for developers with beginner-to-advanced PHP skills. It is also useful to have a good working knowledge of how to work with, set up and manage MySQL databases. sNews is not - geared towards the end-user who knows little or nothing about building and developing PHP-MySQL based websites.
News: Try sNews 1.7 DEMO
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: [MOD] Related Articles (2 options)  (Read 8175 times)
bakercad
Administrator
ULTIMATE member
******

Karma: 105
Posts: 1654



WWW
« on: April 12, 2007, 08:11:19 PM »

See next post for Option 2
When viewing an article, this checks the database for other articles with matching keywords, it then lists links to these articles.

Credit - Idea and coding started here by avenir

OPTION 1 - see next post for Option 2
What it does:  Lists articles below info line and comments form.  If info line/comments are disabled lists below article text.
Files effected:  snews.php only
What it looks like:


STEP 1 - BACKUP!!

STEP 2 - add new functions (I added these after the retrieve() function )
Quote
function retrieve_related($column, $table, $field, $value, $n, $id) {
    $query = "SELECT $column FROM ".db('prefix')."$table WHERE ($value) AND id <> '$id' ORDER BY id DESC LIMIT $n ";
    $result = mysql_query($query) or die(mysql_error().'
'.$query);
    $retrieve = array();
    while ($r = mysql_fetch_array($result)) {$retrieve[] = $r[$column];}
    return $retrieve;
}

function rel_articles($id,$keywords,$n){
   $kws = explode(",", $keywords);
   $num_kw = count($kws);   
   if ($kws[0] != '') {
      for ($i = 0; $i < $num_kw; $i++) {
         $keywords = trim($kws[$i]);
         $value .= " keywords_meta LIKE '%$keywords%' ";
         if ($i < $num_kw-1) {
            $value .= " OR ";
         }
      }
      $get_art = retrieve_related('seftitle', 'articles', 'keywords_meta', $value,$n,$id);
      $num_art = count($get_art);
      if($num_art>0)echo "<h2>".l('related')."</h2><ul>";
      for ($i = 0; $i < $num_art; $i++) {
         $art_id = $get_art[$i];
         $query = "SELECT * FROM ".db('prefix')."articles WHERE seftitle = '$art_id'";
          $result = mysql_query($query);
          while ($r = mysql_fetch_array($result)) {
             $home = s('home_sef');
             $cat = $r['category'];
            $categorySEF = retrieve('seftitle','categories','id',$cat);
             $categorySEF = empty($categorySEF) ? $home : $categorySEF;
             echo '<li><a href="'.db('website').$categorySEF.'/'.$r['seftitle'].'/" title="'.$r['title'].'">'.$r['title'].'</a></li>';
         }
      }
       if($num_art>0)echo "</ul>";
   }
}
STEP 3 - in center() function
ADD the red
Quote
if (!$result || !$numrows) {echo '<h2>'.l('not_found').'</h2>';}
      else {
         while ($r = mysql_fetch_array($result)) {
            $id = $r['id'];
             $keywords = $r['keywords_meta'];

            $infoline = $r['displayinfo'] == 'YES' ? true : false;
THEN at very end of function find this
Quote
if (!empty($article) && empty($currentPage) && $infoline == true) {
            if ($commentable == 'YES') {comment('unfreezed');}
            else if ($commentable == 'FREEZ') {comment('freezed');}
}}}}
and ADD the red
Quote
if (!empty($article) && empty($currentPage) && $infoline == true) {
            if ($commentable == 'YES') {comment('unfreezed');}
            else if ($commentable == 'FREEZ') {comment('freezed');}
}rel_articles($id,$keywords,3); }}} //change the 3 to max number of articles to display
STEP 4 - new language variables
Quote
#related articles
   $l['related'] = 'Related articles';
« Last Edit: September 19, 2007, 12:49:58 PM by philmoz » Logged

bakercad
Administrator
ULTIMATE member
******

Karma: 105
Posts: 1654



WWW
« Reply #1 on: April 12, 2007, 08:12:03 PM »

See first post for Option 1

OPTION 2 - see first post for Option 1
What it does:  Lists articles on the sidebar (under "Categories", "New Comments", "New Articles")
Files effected:  snews.php AND index.php
What it looks like:


STEP 1 - BACKUP!!

STEP 2 - snews.php - add new functions (I added these after the retrieve() function ) THESE ARE NOT THE SAME AS OPTION 1
Quote
function retrieve_related($column, $table, $field, $value, $n, $id) {
    $query = "SELECT $column FROM ".db('prefix')."$table WHERE ($value) AND id <> '$id' ORDER BY id DESC LIMIT $n ";
    $result = mysql_query($query) or die(mysql_error().'
'.$query);
    $retrieve = array();
    while ($r = mysql_fetch_array($result)) {$retrieve[] = $r[$column];}
    return $retrieve;
}

function rel_articles($n){
   $article = get_id('article');
   if (!empty($article)) {
      echo '<div class="box"><h2>'.l('related').':</h2>';
      $id = retrieve('id','articles','seftitle',$article);
      $keywords = retrieve('keywords_meta','articles','seftitle',$article);
      $kws = explode(",", $keywords);
      $num_kw = count($kws);   
      if ($kws[0] != '') {
         for ($i = 0; $i < $num_kw; $i++) {
            $keywords = trim($kws[$i]);
            $value .= " keywords_meta LIKE '%$keywords%' ";
            if ($i < $num_kw-1) {
               $value .= " OR ";
            }
         }
         $get_art = retrieve_related('seftitle', 'articles', 'keywords_meta', $value,$n,$id);
         $num_art = count($get_art);
         echo "<ul>";
         if($num_art>0) {
            for ($i = 0; $i < $num_art; $i++) {
               $art_id = $get_art[$i];
               $query = "SELECT * FROM ".db('prefix')."articles WHERE seftitle = '$art_id'";
                $result = mysql_query($query);
                while ($r = mysql_fetch_array($result)) {
                   $home = s('home_sef');
                   $cat = $r['category'];
                  $categorySEF = retrieve('seftitle','categories','id',$cat);
                   $categorySEF = empty($categorySEF) ? $home : $categorySEF;
                   echo '<li><a href="'.db('website').$categorySEF.'/'.$r['seftitle'].'/" title="'.$r['title'].'">'.$r['title'].'</a></li>';
               }
            }
         }
         else {echo l('no_relation');}
          echo '</ul>';
      }
      else {echo l('no_relation');}
      echo '</div>';
   }
}
STEP 3 - snews.php - new language variables
Quote
#related articles
   $l['related'] = 'Related Articles';
   $l['no_relation'] = 'No related articles at the moment';
STEP 4 - index.php - call PHP function
find
Quote
<div class="box">
            <h2>New Articles:</h2>
            <ul>
               <? menu_articles(0,3); ?>
            </ul>
         </div>
         
         <? extra(); ?>
      </div>
and ADD the red (notice I change the php tags from <? to <?php)
Quote
<div class="box">
            <h2>New Articles:</h2>
            <ul>
               <?php menu_articles(0,3); ?>
            </ul>
         </div>

         <?php rel_articles(3); //change the 3 to max number of articles to display ?>
         
         <?php extra(); ?>
      </div>
« Last Edit: September 19, 2007, 12:50:34 PM by philmoz » Logged

David
sNews Dude
Hero Member
*****

Karma: 7
Posts: 580



« Reply #2 on: April 12, 2007, 08:18:40 PM »

I'm so glad because I wanted this mod for long time ago Smiley Thanks.


The code of the first option is on my site now: http://www.free-css-templates.com/snews-cms/related-articles-mod-for-snews/ Cheesy
Logged

bakercad
Administrator
ULTIMATE member
******

Karma: 105
Posts: 1654



WWW
« Reply #3 on: April 12, 2007, 08:20:07 PM »

You played no small part in this sir.
Logged

mattonik
Full Member
***

Karma: 6
Posts: 121



WWW
« Reply #4 on: April 12, 2007, 09:10:09 PM »

you seems to be a nice mod, very useful in my opinion. thanks for sharing
Logged
bakercad
Administrator
ULTIMATE member
******

Karma: 105
Posts: 1654



WWW
« Reply #5 on: April 12, 2007, 09:16:17 PM »

OOPS....forgot the language variables.

UPDATED each option.  Sorry.
Logged

gis
Jr. Member
**

Karma: 0
Posts: 79



WWW
« Reply #6 on: April 13, 2007, 12:15:37 AM »

OK, if I try option 1, I get the following error: "Parse error: parse error, unexpected $ in /var/www/vhosts/wortgefecht.net/httpdocs/php/snews.php on line 2023"

In that original line I have 5 "}" instead of 4. When I add the 5th, I don't get any error anymore, but there are no related articles.

I am using the following mods in the center() function: Read more, seftitles as links, social bookmarks, and print page.

Here's what my center() function looks like without the related articles mod:
Code:
// CENTER
function center() {
if (isset($_GET['category'])) {$id = $action = $_GET['category'];}
if (isset($_GET['articleid'])) {$articleid = $_GET['articleid'];}
if (isset($_POST['submit_text'])) {processing(); $processed = true;}
if (isset($_POST['contactform'])) {contact(); $processed = true;}
if (isset($_POST['search_query'])) {search(); $processed = true;}
if (isset($_POST['comment'])) {comment('comment_posted'); $processed = true;}
if (isset($_POST['Loginform']) == 'True') {administration(); $processed = true;}
else if (isset($_GET['action'])) {$action = $_GET['action'];}
if (isset($processed) and $processed == true) {unset($action);}
if (get_id('category')) {$action = get_id('category');}
switch ($action) {
case 'archive': archive(); break;
case 'sitemap': if (!isset($_POST['search_query'])) {sitemap();} break;
case 'contact': if (!isset($_POST['search_query'])) {contact();} break;
case 'login': if (!isset($_POST['search_query'])) {login();} break;
case 'administration': if (isset($_SESSION[db('website').'Logged_In']) && !isset($_POST['search_query'])) {administration();} break;
case 'settings': if (isset($_SESSION[db('website').'Logged_In'])) {settings();} break;
case 'categories': if (isset($_SESSION[db('website').'Logged_In'])) {admin_categories();} break;
case 'admin_category': if (isset($_SESSION[db('website').'Logged_In'])) {form_categories();} break;
case 'articles': if (isset($_SESSION[db('website').'Logged_In'])) {admin_articles('article_view');} break;
case 'extra_contents': if (isset($_SESSION[db('website').'Logged_In'])) {admin_articles('extra_view');} break;
case 'pages': if (isset($_SESSION[db('website').'Logged_In'])) {admin_articles('page_view');} break;
case 'admin_article': if (isset($_SESSION[db('website').'Logged_In'])) {form_articles('');} break;
case 'article_new': if (isset($_SESSION[db('website').'Logged_In'])) {form_articles('article_new');} break;
case 'extra_new': if (isset($_SESSION[db('website').'Logged_In'])) {form_articles('extra_new');} break;
case 'page_new': if (isset($_SESSION[db('website').'Logged_In'])) {form_articles('page_new');} break;
case 'editcomment': if (isset($_SESSION[db('website').'Logged_In'])) {edit_comment();} break;
case 'files': if (isset($_SESSION[db('website').'Logged_In'])) {files();} break;
case 'process': if (isset($_SESSION[db('website').'Logged_In'])) {processing();} break;
case 'logout': session_destroy();
echo '<meta http-equiv="refresh" content="1; url='.db('website').'">';
echo '<h2>'.l('log_out').'</h2>';
break;
default:
if ($processed == false) {
$article = get_id('article');
$currentPage = strpos($article, l('paginator')) === 0 ? str_replace(l('paginator'), '', $article) : '';
$home = s('home_sef'); $categorySEF = get_id('category');
$categorySEF = empty($categorySEF) ? $home : $categorySEF;
$use_cat_id = $categorySEF == $home ? 0 : retrieve('id', 'categories', 'seftitle', $categorySEF);
$query_articles = "SELECT * FROM ".db('prefix')."articles WHERE published = 1 AND SUBSTRING(position, 1, 1) != '2'";
   if (!empty($article) && empty($currentPage)) {$query_articles .= " AND seftitle = '$article'";}
else if ($categorySEF == $home && s('display_page') <> 0) {$id_page = s('display_page'); $query_articles .= " AND id = '$id_page'";}
   else {
if (s('display_new_on_home') == 'on') {$query_articles .= $use_cat_id != 0 ? " AND category = $use_cat_id" : '';}
else {$query_articles .= " AND category = $use_cat_id";}
$query_articles .= " AND position <> 3 ORDER BY date DESC";
$result_articles = mysql_query($query_articles);
if (!$result_articles) {echo '<h2>'.l('not_found').'</h2>'; break;}
$numrows_articles = mysql_num_rows($result_articles);
if ($result_articles && $numrows_articles) {
$articleCount = s('article_limit');
$article_limit = (empty($articleCount) || $articleCount < 1) ? 100 : $articleCount;
$totalPages = ceil($numrows_articles/$article_limit);
if (!isset($currentPage) || !is_numeric($currentPage) || $currentPage < 1) {$currentPage = 1;}
else if ($currentPage > $totalPages) {$currentPage = $totalPages;}
$query_articles .= " LIMIT ".($currentPage - 1) * $article_limit.", ".$article_limit;
}
}
$result = mysql_query($query_articles);
$numrows = mysql_num_rows($result);
if (!$result || !$numrows) {echo '<h2>'.l('not_found').'</h2>';}
else {
while ($r = mysql_fetch_array($result)) {
$infoline = $r['displayinfo'] == 'YES' ? true : false;
$text = stripslashes($r['text']);
if (!empty($currentPage)) {$short_display = strpos($text, '[break]'); $shorten = $short_display == 0 ? 9999000 : $short_display;}
else {$shorten = 9999000;}
$comments_query = "SELECT * FROM ".db('prefix')."comments WHERE articleid = $r[id] AND approved = 'True'";
$comments_result = mysql_query($comments_query);
$comments_num = mysql_num_rows($comments_result);
$a_date_format = date(s('date_format'), strtotime($r['date']));
       $position = $r['position'];
$category = $r['category'] == 0 ? $home : retrieve('seftitle', 'categories', 'id', $r['category']);
if ($r['displaytitle'] == 'YES') {
echo '<h2>';
if (strlen($text) > $shorten) {
echo '<a href="'.db('website').$category.'/'.$r['seftitle'].'/" title="'.$r['title'].'">'.$r['title'].'</a>';
}
// TITEL MIT LINK MOD
// ORIG: else {echo $r['title'];}

else {echo '<a href="'.db('website').$category.'/'.$r['seftitle'].'/" title="'.$r['title'].'">'.$r['title'].'</a>';}
echo '</h2>';
}

file_include(str_replace('[break]', '', $text), $shorten);

// MOD TO ADD ..READ MORE AFTER SHORTENED ARTICLE
if ($infoline == true && strlen($r['text']) > $shorten) {
                    echo '<p><a href="'.db('website').$category.'/'.$r['seftitle'].'/" title="'.$r['title'].'">'.l('read_more').'</a>';
echo '</p>';
                    }

$labels = explode(',', $r['keywords_meta']);
$first_label = false;
foreach ($labels as $labels) {
    if($labels == '') break;
    if($first_label == false){
        echo '<p class="tags">'.l('labels').': ';
        $first_label = true;
    }
    echo ' <a href="http://technorati.com/tag/'.$labels.'" rel="tag">'.$labels.'</a>';
}
if($first_label == true) echo '</p>';

echo '<p>'.l('soc_bookmark').': ';
soc_bookmark(db('website').$category.'/'.$r['seftitle'].'/', $r['title'], 'Y');
echo '</p>';

$commentable = $r['commentable'];
if (!empty($currentPage)) {
$tag = explode(',', tags('infoline'));
$link = '<a href="'.db('website');
$edit_link = $link.'index.php?action=admin_article&id='.$r['id'].'" title="'.l('edit').' '.$r['title'].'">'.l('edit').'</a>';
if ($infoline == true) {
foreach ($tag as $tag) {
switch (true) {
case ($tag == 'print'): echo "<a href=\"print.php?id=".$r['id']."\" target=\"_blank\">» Drucken</a> ";break;
case ($tag == 'date'): echo $a_date_format; break;
case ($tag == 'readmore' && strlen($r['text']) > $shorten):
echo $link.$category.'/'.$r['seftitle'].'/" title="'.l('read_more').'">'.l('read_more').'</a> '; break;
case ($tag == 'comments' && ($commentable == 'YES' || $commentable == 'FREEZ')):
echo $link.$category.'/'.$r['seftitle'].'/#'.l('comment').'1" title="'.l('comments').'">'.l('comments').' ('.$comments_num.')</a> '; break;
case ($tag == 'edit' && $_SESSION[db('website').'Logged_In'] == 'True'): echo ' '.$edit_link; break;
case ($tag != 'readmore' && $tag != 'comments' && $tag != 'edit'): echo $tag; break;
}
}
}
else if ($_SESSION[db('website').'Logged_In'] == 'True') {echo '<p>'.$edit_link.'</p>';}
}
else if (substr($position, 0, 1) != '2' && empty($currentPage)) {
$edit_link = '<a href="'.db('website').'index.php?action=admin_article&id='.$r['id'].'" title="'.l('edit').' '.$r['title'].'">'.l('edit').'</a>';
if ($infoline == true) {
$tag = explode(',', tags('infoline'));
foreach ($tag as $tag ) {
switch ($tag) {
case 'print' : echo "<a href=\"print.php?id=".$r['id']."\" target=\"_blank\">» Drucken</a> ";break;
case 'date': echo $a_date_format; break;
case 'readmore':
case 'comments': ; break;
case 'edit': if ($_SESSION[db('website').'Logged_In'] == 'True') {echo '  '.$edit_link;} break;
default: echo $tag;
}
}
}
else if ($_SESSION[db('website').'Logged_In'] == 'True') {echo '<p>'.$edit_link.'</p>';}
}
}
if (!empty($currentPage) && ($numrows_articles > $article_limit) && s('display_pagination') == 'on') {
paginator($categorySEF, $currentPage, $totalPages, '', l('paginator'));
}
if (!empty($article) && empty($currentPage) && $infoline == true) {
if ($commentable == 'YES') {comment('unfreezed');}
else if ($commentable == 'FREEZ') {comment('freezed');}
}}}}}
« Last Edit: September 19, 2007, 12:51:21 PM by philmoz » Logged

Nah, we anarchists don't believe in coercive orthography.
mosh
Sr. Member
****

Karma: 63
Posts: 415



WWW
« Reply #7 on: April 13, 2007, 12:33:01 AM »

installed and working perfecto  Cheesy
love it, thank you to developers

awsome day/night ( lol )  to all
mo
Logged

piXelatedEmpire
MIA
Thread Moderator
ULTIMATE member
*****

Karma: 37
Posts: 1456


currently MIA


« Reply #8 on: April 13, 2007, 01:56:56 AM »

looking good!  :cool:
Logged

my apologies to the sNews crew, but I will be MIA for the forseeable future
Patric Ahlqvist
Nobodys perfect, but Im pretty effing close
Administrator
ULTIMATE member
******

Karma: 65
Posts: 4917


“I'm a self-made man and worships my creator.”


WWW
« Reply #9 on: April 13, 2007, 10:53:26 AM »

And all this is possible to style ever whih way I like I assume... Great mod, again... Bobo. Thanks.
Logged

My sNews site
"It's only dead fish that goes with the flow... "
Updated, online again - Free designs
Sven
ULTIMATE member
******

Karma: 76
Posts: 1861


Chasing MY bugs!


WWW
« Reply #10 on: April 20, 2007, 12:12:58 PM »

Howdy!

It happens only to me Wink  : I failed to make 1st option working out.  Sad
No errors but I can't display related articles box.
I have 2 articles with same keywords.
I use Hidden comment form until clicked's Mod.

What informations should I give you to help me to fix that issue dear Sherlocks.
Logged

David
sNews Dude
Hero Member
*****

Karma: 7
Posts: 580



« Reply #11 on: April 20, 2007, 01:07:49 PM »

Sven I think Bob forgot to mention that you need to add in your center function this:
Quote
$result = mysql_query($query_articles);
    $numrows = mysql_num_rows($result);
    if (!$result || !$numrows) {echo '<h2>'.l('not_found').'</h2>';}
    else {
    while ($r = mysql_fetch_array($result))
    {
    $id = $r['id'];
    $keywords = $r['keywords_meta'];

    $infoline = $r['displayinfo'] == 'YES' ? true : false;
http://www.free-css-templates.com/snews-cms/related-articles-mod-for-snews/ Wink

edited by bakercad to show the correct lines as read
« Last Edit: September 19, 2007, 12:52:07 PM by philmoz » Logged

Sven
ULTIMATE member
******

Karma: 76
Posts: 1861


Chasing MY bugs!


WWW
« Reply #12 on: April 20, 2007, 02:25:58 PM »

Smiley
Oops !
I went on your website (and enjoyed my visit) yesterday and left 2 messages (did you get them btw) and didn't compare the codes.
Now it's working.
A big big thanks David !
Have a nice weekend.
Logged

bakercad
Administrator
ULTIMATE member
******

Karma: 105
Posts: 1654



WWW
« Reply #13 on: April 20, 2007, 02:36:31 PM »

thanks David for the catching that and Sven for pointing that out.  First post has been updated.
Logged

David
sNews Dude
Hero Member
*****

Karma: 7
Posts: 580



« Reply #14 on: April 20, 2007, 03:45:59 PM »

no problem. Sven, I didn't receive any message from you I think. Did you leave comments or did you send to me a message from the contact form?
Logged

Pages: [1] 2
  Print  
 
Jump to:  

English Steel 1.6 © Saxon North Technologies
Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!