Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function: tagCloud
#1
Simple function to extract all the meta tags from pages and display a Tag Cloud.

You'll need to change the link tags depending on what you want to do with the link, either through onlick or normal links.

Might be of use to someone....

Mike.

This is revision 1.1 of the code.

Code:
function tagCloud(){
    $minFont=14;            // change to min font size
    $maxFont=20;            // change to max font size
    $tags  = array();
    $cloud = array();
    // Grab the tags from the database
    
    $path = "data/pages";
    $dir_handle = @opendir($path) or die("Unable to open $path");
    $filenames = array();
    while ($filename = readdir($dir_handle)) {
        $filenames[] = $filename;
    }
    
    $count=0;
    $value="";
    if (count($filenames) != 0) {
        foreach ($filenames as $file) {
            if ($file == "." || $file == ".." || is_dir("data/pages/".$file) || $file == ".htaccess"  ) {
                // not a page data file
            } else {
                $thisfile = @file_get_contents('data/pages/'.$file);
                $data = simplexml_load_string($thisfile);
                if ($data->private != 'Y') {
                    $metaData = $data->meta;
                    $db = explode(' ', $metaData);    
                    while(list($key, $value) = each($db)){
                           @$tags[(string)$value] += 1;
                    }
                      $count++;
                }
            }
        }
    }
        
    // Get totals to use for font size    
    $min = min(array_values($tags));
    $max = max(array_values($tags));
    $fix = ($max - $min == 0) ? 1 : $max - $min;
    // Display the tags
    foreach ($tags as $tag => $count) {
        $size = $minFont + ($count - $min) * ($maxFont - $minFont) / $fix;
        $jsLink=" onclick=\"searchTag('".$tag."');\" ";  // change this if
        $normalLink='#';                    // change if you want to use normal links
        $cloud[] = '<a  style="font-size:'. floor($size) .'px" class="cloud" href="'.$normalLink.'" title=" '. ucfirst($tag) .' ('. $count .')"  '.$jsLink.' >'. htmlspecialchars(stripslashes(ucfirst($tag))) . '</a>';
    }
    $shown = join("\n", $cloud) . "\n";
    echo  $shown;
}
My Github Repos: Github
Website: DigiMute
Reply
#2
Looking good, I was actualliy thinking of writing a tagcloud function myself but it seems you beat me to it!

I have a few things I want to say though:
Code:
\\ example   $jsLink=" onclick=\"searchTag('".$tag."');\" ";
Slashes the wrong way, wouldn't this give problems if I were to copy your script into my functions.php?
Code:
$query=$myPages->item;
You define a variable that you're not going to be using anywhere?
Code:
$components = subval_sort($myPages->xpath("//*"),'slug');
You seem to be using subval_sort() but I thought we established the fact that it doesn't work on an array of SimpleXMLElement objects (as are returned by your XPath call)?
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#3
Thanks Zegnåt,

Made those changes, should be sorted now.

Sort didn't make a difference but your right its not needed...

Mike....
My Github Repos: Github
Website: DigiMute
Reply
#4
I just saw this now - very cool Mike... That's exactly why i called it "Keywords & Tags" Wink
- Chris
Thanks for using GetSimple! - Download

Please do not email me directly for help regarding GetSimple. Please post all your questions/problems in the forum!
Reply
#5
Mike, you dont happen to have a preview of this running on GS do you?
Reply
#6
I've enabled it on my site: http://www.digimute.com

Not many tags there yet as I still haven't got around to updating my site.
and the links don't do anything at the moment, but you get the idea.

Mike.
My Github Repos: Github
Website: DigiMute
Reply
#7
Jamz Wrote:Mike, you dont happen to have a preview of this running on GS do you?
I like to believe the “Tags” box on Mike’s site is an example.

The problem is not so much showing you what this outputs, the problem is letting it do something. GetSimple has no search system or ordering by tags implemented, meaning that even though we can display this tag cloud we can’t link the tags to specific pages

Edit: never mind, Mike answered while I was writing this.
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#8
How much extra time does this add in processing the page?

My concern is that your opening every file on every load. If you have 100 people loading the page at the same time you could be processing thousands of requests. I have learned that using the XML structure is a great thing, but also it's a burden. HOWEVER, there are ways around burdens. I believe the best way would be to include a file called TAGS.xml and then have that updated whenever you save or delete a page. That way when you load index.php and include the tags, it's all in one file instead of many more.

On top of that, when you open the file isn't it cached to the browser for quick access?
Clients always want to be able to change the content of their pages, but they are unwilling to do so.

Have you ever coded in your underwear before?
Reply
#9
internet54 Wrote:My concern is that your opening every file on every load.
There is a large overheat, yes. This due to the multiple uses of menu_data(). The best way would be to cache the contents of that function somewhere. If you were to use this function here you are calling menu_data() for your menu (where it will walk through every single XML file) and afterwards you call it again just to generate this tag cloud (where it will, again, walk through all files).

Other than that GetSimple has minimal overheat. It takes the overall configurations file to get stuff like the site name, it gets the XML file for the current page and when requested by the theme it will read the components file. The amount of files read for this could only be made less by porting the site name and overall settings like that to every single page file. This will raise the amount of space taken on the server though.

So maybe a file containing the XML often given by menu_data() will save us most of the overheat, and with that most of the requests. This should not be limited to just tags.

If you care to fix it for your website already you could write your own menu function, or use mine, to call menu_data() to some variable. Then this same variable can be used by your menu and by this tag cloud function. That way you will only be reading through those files once instead of multiple times.

Might be interesting to hear what Chris thinks about this? As he often said menu_data() was imperfect.

internet54 Wrote:when you open the file isn't it cached to the browser for quick access?
What file? The XML files? No. The browser never actually sees those, everything gets sorted by PHP and I don’t think the server caches the XML files that are already stored by it. That would be redundant.
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#10
I did suggest something like this way back at the start.

http://get-simple.info/forum/viewtopic.php?id=87

I agree its not the best way to do do things.

Mike..
My Github Repos: Github
Website: DigiMute
Reply
#11
How is this component called?

Thanks.

homershines
Reply
#12
homershines Wrote:How is this component called?
A matter of calling the tagCloud() function in your template file.
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#13
OK. Thanks.

Using <?php tagCloud(); ?>

I'm getting a message:

Notice: Undefined index: in /Users/hj/work/GS/theme/hp/functions.php on line 41

Did I fail to make a proper selection of a feature?

Thanks.

homershines
Reply
#14
What’s on line 41 of your functions.php?

And did you make sure that the function Mike shared with us in the first post is between <?php and ?> within functions.php as well?
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply
#15
Code:
function tagCloud(){
        $myPages=simplexml_load_string(menu_data('',true));
        $minFont=14;            // change to min font size
        $maxFont=20;            // change to max font size
        $tags  = array();
        $cloud = array();
        // Grab the tags from the database
        $components = $myPages->xpath("//*");
            foreach ($components as $item){      
            $db = explode(' ', $item->meta);    
            while(list($key, $value) = each($db)){
                   $tags[$value] += 1;
...}

Last line is line 41

Im getting error message like Homershines..

Im added function tagCloud in theme_functions.php and call it from template <?php tagCloud(); ?>
Reply
#16
Thanks folks.

Line 41 contains the following...

$tags[$value] += 1;


The function tagCloud() is below the component_master function in functions.php.
There is a <?php and ?> opening and closing functions.php.

Many thanks.

homershines
Reply
#17
Smile

THis was never going to work...
I have a slightly modified menudata() function which returns the "meta" data as well as the rest.
the function relies on it being there.

I've posted revised code above which should work perfectly...

Mike...
My Github Repos: Github
Website: DigiMute
Reply
#18
Thanks folks.

It works now nOOdles.
Because of my lack of experience, I don't always feel confident making a comment about things.
I always think I'm at fault for not doing something correctly or not understanding directions.
I'm sure other people feel the same way.

This little situation should teach us that it doesn't really matter.

Everyone can contribute something.

Even their confusion about something.

peace and thanks.

homershines
Reply
#19
What do I do with the code? Is this a plugin? And do the tags have links now? Sorry for all the questions, I have tried to understand the whole thread but I don't!
Reply
#20
At the moment it doesn't do anything except display a Tag Cloud , check my site
http://www.digimute.com

Each entry on my site if filed under "php, portfolio, getsimple etc..."
these are entered in the tags/keywords field on each page, and can have as many as you like seperated by a space or comma.

The tagcloud function will collate all the tags and displaysall the keywords increasing the font size depending on the frequency of the word. the links created can then be used for searching. (which you will have to write, doesn't do anything on my site... )

hope it explains a little...

Mike.
My Github Repos: Github
Website: DigiMute
Reply
#21
Thanks.

Where in the script do you write the info for the generated links?

Thanks.

homershines
Reply




Users browsing this thread: 1 Guest(s)