Welcome Guest, Not a member yet? Register   Sign In
Tag Cloud Library
#1

[eluser]Leggy[/eluser]
This is my first library so is probably not perfect:

/system/application/libraries/cloud.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
    Created: Saturday 22nd December
    Author: Henry Legge
    Copyright: Henry Legge
    
    This code may be used on any website, providing credit is given
*/

class Cloud
{
    
    var $param = array();
    
    /*
        Constuctor Function
        
        Opens Library and prepares parameters
    */
    function Cloud( $config = array() )
    {
        $this->param = $config;
    }
    
    /*
        Create Function
        
        Prepare and echo the tag cloud
    */
    function create()
    {
        if( count( $this->param ) != 6 ) die( 'Sorry there has been an error. The required data needed is not provided' );
        else
        {
            $types = array();
            
            $query = $this->param[0];
            $separator = $this->param[1];
            $x = $this->param[2]; // max px
            $y = $this->param[3]; // min px
            $url = $this->param[4];
            $column = $this->param[5];
            
            if( $query->num_rows() > 0 )
            {
                foreach( $query->result() as $ftypes )
                {
                    if( $ftypes->$column != null ) $types = array_merge( $types, explode( $separator, trim( $ftypes->$column ) ) );
                }

                $typecount = array_count_values( $types );
                $types = $this->array_unique_compact( $types );

                $max = max($typecount);
                $min = min($typecount);

                $stepvalue = ($max - $min) / ($x - $y);
                if( $stepvalue == 0 ) $stepvalue = 1;

                for( $i = 0; $i < count( $types ); $i++ )
                {
                    $link = str_replace( '{TAG}', $types[$i], $url );
                    echo '<a href="'.$link.'" style="font-size:'. ( $y + round( ($typecount[$types[$i]]-$min) / $stepvalue ) ).'px;">'. $types[$i].'</a>';
                    if( $i < count( $types ) - 1 ) echo ",\n";
                }
            }
            else
            {
                echo 'No Tags';
            }
        }
    }
    
    /*
        Array Unique Compact Function
        
        Creates a unique and compact array
    */
    function array_unique_compact( $a )
    {
        $tmparr = array_unique( $a );
        $i = 0;
        foreach( $tmparr as $v )
        {
            $newarr[$i] = $v;
            $i++;
        }
        return $newarr;
    }

}
?&gt;

How to use it
Code:
&lt;?php
$query = $this->db->get( 'blog' );

$prefs = array(
    $query, // query to get the tags
    ', ', // tag seprator ( e.g. ', ', '. ', etc )
    '18', // max size of tag
    '11', // min size of tag
    'http://www.url.com/tags/{TAG}/', // url of tags {TAG} is the tag
    'tags' //column of the table containing the tags
);

$this->load->library( 'cloud', $prefs );

$this->cloud->create();
?&gt;

If must contain all 6 of the things in the $prefs array.

Enjoy Wink




Theme © iAndrew 2016 - Forum software by © MyBB