Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [MOD] Keyword cloud by keyword usage  (Read 3079 times)

centered

  • Guest
[MOD] Keyword cloud by keyword usage
« on: June 14, 2007, 02:22:02 AM »

Inspired by this thread: http://www.solucija.com/forum/viewtopic.php?id=2974 and all the sites that fully utilize a tag cloud or weighed links. And this is what this mod is, a weighed list of keywords you use in your articles.

Huge thanks to byron3 at sitepoint

I am pretty sure this can be done for all versions of sNews

0. Backup!!
0a. If 1.4, you need this mod: http://www.solucija.com/forum/viewtopic.php?id=637
0b. Also will need parts 4 and 5 from this mod: http://www.solucija.com/forum/viewtopic.php?id=5011

1.  Place these functions somewhere:
Code: [Select]
// Following 3 functions for tag cloud
function EntryExist($out, $value) {
foreach ($out as $K => $v) {
         if ($K == $value) { return TRUE; }
     }
     return FALSE;
}

function keyword_array () {
     $out = array();
     connect_to_db();
     $db = new Database(s('dbname'));
     $query = "SELECT * FROM " .s('prefix'). "articles WHERE published = 1";
     $result = $db->query($query);
     while ($r = $result->fetch_array()) {
         $keywords = explode(', ', $r['keywords_meta']);
         foreach ($keywords as $value ) {
             if ( !EntryExist( $out, $value )) {
                 $out[$value] = 1;
             } else {
                 $out[$value] += 1;
             }
         }
     }
     return $out;  
}  

function keyword_cloud() {
$keywords = keyword_array();
foreach( $keywords as $K => $V ) {
   echo '<a class="t'.$V.'" href="index.php?search='.$K.'">'.$K.'</a> ';
}
}
1a. If MYSQL (untested, but I am sure this should work)

Remove:
Code: [Select]
    connect_to_db();
     $db = new Database(s('dbname'));
Replace:
Code: [Select]
    $result = $db->query($query);
     while ($r = $result->fetch_array()) {
With:
Code: [Select]
    $result = mysql_query($query);
      while ($r = mysql_fetch_array($result)) {
2. Add in index.php
Code: [Select]
<div id="tagsbox">
<h2>TagCloud</h2>
<?php keyword_cloud(); ?>
</div>
3. Add some style to it, of course add your own:
Taken from the original tag cloud thread:
Code: [Select]
#tagsbox {
    height: 100%;
    float: left;
    margin: 5px auto 20px;
    text-align: left;
    vertical-align: middle;
}
#tagsbox h2 { padding-bottom: 10px; }
#tagsbox a { padding: 5px;}
#tagsbox .t0 {font-size: x-small; color: #00E8E8; }
#tagsbox .t1 {font-weight: bold;font-size: small;color: #00D9AD;}
#tagsbox .t2 {font-size: medium;color: #00B7B7;}
#tagsbox .t3 {font-weight: bold;font-size: large;color: #009797;}
#tagsbox .t4 {font-weight: bold;font-size: x-large;color: #005959;}
Of course a screenshot:

Please notice "lorem" and "ipsum" are used in multiple articles resulting in a higher font size



Issues:

Well, from what it looks like, the keywords most used are placed first... though I am pretty sure someone can make it output to be alphabetical if they want to...
OR
You can just add
Code: [Select]
ksort($out);Before
Code: [Select]
return $out;
« Last Edit: August 25, 2007, 03:15:27 AM by Phil Martin (Philmoz) »
Logged

centered

  • Guest
[MOD] Keyword cloud by keyword usage
« Reply #1 on: June 15, 2007, 01:28:27 AM »

One slight issue...

Ok so $V gives out a count right. So the css should style it as such, but if you use a word 2x,3x,4x, it will move to the next font level.

So what happens after that...

umm help!
Logged