This topic is locked

Adding tag cloud to PHPRunner application

4/19/2015 2:43:04 PM
PHPRunner Tips and Tricks
admin

Tag cloud is a nice visual metadata presentation tool. While it's more useful to display tags it can be applied to any free form text data. In this article we'll show how to add such tag cloud to your PHPRunner project. We'll be using awesomeCloud jQuery plugin for this purpose.
We'll be displaying tag cloud based on this forum posts database.


  1. Posts table, List page, BeforeProcess event:

$_SESSION["words"]=array();


2. Posts table, AfterRecordProcessed event:

$delimiters=array(" ",",",".","?","!","-","(",")",">","<","/","\"","[","]","$","&","#",";","=");

$ready = str_replace($delimiters, $delimiters[0], $data["post"]);

$arr = explode($delimiters[0],$ready);

$_SESSION["words"]=array_merge($_SESSION["words"], $arr);


Replace post with the actual name of the field that stores textual data.
In this event we split phrases into words, remove all kinds of delimiters and add all single words to $words array.
3. Put jquery.awesomeCloud-0.2.js file to 'source' folder of your project or just directly to the output folder.
4. On Posts table List page insert 'PHP code snippet' where you want your tag cloud to appear. Here is the code for this snippet.

echo '

<script src="jquery.awesomeCloud-0.2.js"></script>

<style type="text/css">

.wordcloud {

border: 1px solid #ccc;

height: 200px;

//margin: 0.5in auto;

padding: 0;

page-break-after: always;

page-break-inside: avoid;

width: 850px;

}
.wordcloud span {

display: none;

}

</style>
<div id="wordcloud1" class="wordcloud">';
//uncomment the following section if you have a separate tables with the list of words to exclude

//$exclude=array();

//$rs=CustomQuery("select excludeword from `tagcloud_exclude`");

//while ($data=db_fetch_array($rs))

// $exclude[]=$data["excludeword"];

//$_SESSION["words"]=array_diff($_SESSION["words"], $exclude);
$arr = array_count_values($_SESSION["words"]);
foreach($arr as $word=>$weight)

if ($weight>2 && strlen($word)>5)

echo '<span data-weight="'.$weight.'">'.$word.'</span>';
echo '</div>
<script>

$(document).ready(function(){

$("#wordcloud1").awesomeCloud({

"size" : {

"grid" : 18,

"normalize" : false

},

"options" : {

"color" : "random-dark",

"rotationRatio" : 0.35,

"printMultiplier" : 3,

"sort" : "random"

},

"font" : "\'Times New Roman\', Times, serif",

"shape" : "square"

});



});

</script>

';


Here are possible tweaks for this snippet:
4.1 width: 800px; height: 200px - width and height of tag cloud
4.2

if ($weight>2 && strlen($word)>5)


Here you can experiment with the minimum word count and word length. If you have lots of records on the page you do not want to display too many words, they gonna look like tiny dots. In this example I only display words that appears more than twice and and is longer than 5 characters.