PHP + MySQL Tag Cloud

    Just inspired by the topic - habrahabr.ru/blog/php/48543.html#habracut

    Everything is simply impossible. This is an example of outputting a tag cloud for photos. The code is not perfect, the algorithm is also simple. But the result is pretty kosher.

    Tags are stored in the form as - id | text A is a communication table. This is the table where the photos themselves are sitting Well, actually the function that displays the cloud itself

    CREATE TABLE `users_tags` (
    `id` int(11) unsigned NOT NULL auto_increment,
    `text` varchar(255) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `txt` (`text`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;




    CREATE TABLE `users_fun_photos_tags_data` (
    `photo_id` int(11) unsigned NOT NULL,
    `tag_id` int(11) unsigned default NULL,
    KEY `tag_id` (`tag_id`),
    KEY `photo_id` (`photo_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;




    CREATE TABLE `users_fun_photos` (
    `id` int(11) unsigned NOT NULL auto_increment,
    `title` varchar(255) default NULL,
    `tags` varchar(255) default NULL, /* здесь сидят теги к фото в виде строки, где теги разделены запятой */
    `cat_id` int(11) unsigned NOT NULL default '1',
    `file` varchar(30) default NULL,
    `dir` varchar(3) default NULL,
    `user_id` int(11) unsigned default '1',
    `date` datetime NOT NULL default '0000-00-00 00:00:00',
    `date_last_comment` datetime default NULL,
    `comments` int(11) unsigned NOT NULL default '0',
    `views` int(11) NOT NULL default '0',
    `points` int(11) default '0',
    PRIMARY KEY (`id`),
    KEY `views` (`views`),
    KEY `user_id` (`user_id`),
    KEY `comments` (`comments`),
    KEY `cat_id` (`cat_id`),
    KEY `title` (`title`),
    KEY `tags` (`tags`),
    KEY `date` (`date`),
    KEY `date_last_comment` (`date_last_comment`),
    KEY `points` (`points`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;




    function GetAllTagsFromDB ()
    {
       # This is mine, yours may differ
       global $ prefix, $ dbi;

       $ data = NULL; # There will be output
       $ list_tags = array (); # There will be tag text
       $ list_ids = array (); # Here tag id

       # Indicate the maximum and minimum font size
       $ max_size = 300; # Maximum font size in percent
       $ min_size = 100; # Minimum font size, also in percent

       # Select data where quantity - how many times a tag occurs, tag_text - text and tag_id - tag id, respectively
       $ res = sql_query ("SELECT COUNT (a.photo_id) AS quantity, b.text, b.id from". $ prefix. "_ users_fun_photos_tags_data a,". $ prefix. "_ users_tags b where a.tag_id = b.id group by a.tag_id ", $ dbi);
       while (list ($ quantity, $ tag_text, $ tag_id) = sql_fetch_row ($ res))
       {
          $ list_ids [$ tag_id] = $ quantity; # Add tag id to array
          $ list_tags [$ tag_id] = $ tag_text; # Add tag text to the array
          # Note that the key to the array is the id of the tag
       }

       #
       Sort the array of tags asort ($ list_tags);

       # Check if the array is not empty
       if (! Empty ($ list_tags))
       {
          # Choose the maximum and minimum quantity
          $ max_qty = max (array_values ​​($ list_ids));
          $ min_qty = min (array_values ​​($ list_ids));

          # Calculate the difference between the maximum and minimum quantity
          $ difference = $ max_qty - $ min_qty;
          # You cannot divide by zero
          if (0 == $ difference)
          {
            $ difference = 1;
          }
          # We calculate the step of increasing the font size
          $ step = ($ max_size - $ min_size) / ($ difference);

          # We go through the array of tags
          foreach ($ list_tags as $ key => $ value)
          {
             # Calculate the future font size
            $ size = ceil ($ min_size + (($ list_ids [$ key] - $ min_qty) * $ step));

             $ data. = ''. $ list_tags [$ key].' ';

          }
       }
       return $ data;
    } * This source code was highlighted with Source Code Highlighter .

    Also popular now: