SimplePortal

Customization => Custom Coding => Topic started by: fotografo74 on April 10, 2009, 05:35:00 AM

Title: [TIPS]Tags cloud in PHP block
Post by: fotografo74 on April 10, 2009, 05:35:00 AM
Hi :-)
I have Simple Tagging Mod, useful and nice: http://custom.simplemachines.org/mods/index.php?mod=1411
In http://www.macroforum.org/index.php?action=tags;cloud i have the tags clouds.
How can i manage this cloud for display it in PHP block ?
This could be nice addition in portal home page! :-)

In tagging.php i have :
Code: [Select]
function ViewCloud(){
global $context, $smcFunc, $modSettings, $txt;

$context['tags'] = array();

// A big thank you to Prism Perfect for this code!
// http://prism-perfect.net/archive/php-tag-cloud-tutorial/

$result = $smcFunc['db_query']('', '
SELECT t.tag_name, COUNT(tt.id_tag) AS amount
FROM {db_prefix}tags AS t
INNER JOIN {db_prefix}tag_topics AS tt ON (tt.id_tag = t.id_tag)
GROUP BY tt.id_tag
LIMIT {int:limit}',
array(
'limit' => $modSettings['tags_to_show']
)
);

while ($row = $smcFunc['db_fetch_assoc']($result)) {
$tags[$row['tag_name']] = $row['amount'];
}

$smcFunc['db_free_result']($result);

// Let's shuffle these puppies up, to make it more fun
$keys = array_keys($tags);
shuffle($keys);

foreach ($keys as $key)
$context['tags'][$key] = $tags[$key];

$context['max_size'] = (int) $modSettings['max_tag_percentage'];
$context['min_size'] = (int) $modSettings['min_tag_percentage'];
$context['max_qty'] = (int) max(array_values($context['tags']));
$context['min_qty'] = (int) min(array_values($context['tags']));

$spread = $context['max_qty'] - $context['min_qty'];
$spread = ($spread > 0) ? $spread : 1;

$context['step'] = ($context['max_size'] - $context['min_size']) / $spread;

// Load the main Tags template
loadtemplate('Tagging');

// Setup the title and template.
$context['page_title'] = $txt['tags'] . ' - ' . $txt['tags_cloud'];
$context['sub_template'] = 'cloud';

}

and in Tagging.template.php i have:
Code: [Select]
function template_cloud()
{
global $txt, $context, $scripturl;

// I'm settings this here so that you can change it on a per-theme basis
$context['tag_colors'] = array(
'#0AC92B', // Green
'#CC00FF', // Purple
'#FF0033', // Red
'#4D4DFF', // Blue
'#000000', // Black
);

$num_colors = count($context['tag_colors']) - 1;

echo '
<table border="0" cellspacing="0" cellpadding="8" align="center" class="tborder" style="width: 60%;">
<tr class="catbg3">
<td align="center"><b>', $txt['tags_tag_cloud'], '</b></td>
</tr>
<tr>
<td class="windowbg" style="padding: 1em 2em 1em 2em; text-align: center;">';

if(!empty($context['tags']))
foreach ($context['tags'] as $key => $value){
$color = $context['tag_colors'][mt_rand(0, $num_colors)];
$size = ceil($context['min_size'] + (($value - $context['min_qty']) * $context['step']));
// The index is so we can put smaller elements in front, so they won't get swallowed by the big ones.
$index = abs($size - $context['max_size']);

echo '<a href="', $scripturl, '?action=tags;name=', urlencode($key), '" style="z-index: ', $index, '; position: relative; font-size: ', $size, '%; color: ', $color, ';" title="', $value, ' ', $txt['tags_tagged_with'], ' ', ucwords($key), '">', str_replace(' ', '&nbsp;', ucwords($key)), '</a>&nbsp;<span class="smalltext">(', $value, ')</span> ';
}
else
echo $txt['tags_none_in_cloud'];

echo '
</td>
</tr>
</table>';
}

Bye !
Antonio
Title: Re: Tags cloud in PHP block
Post by: fotografo74 on April 10, 2009, 03:46:40 PM
Thanks. This is nice trick.
Hi :-)
This is help request...not my trick!  :D
I hope that anyone help me :-)
Antonio
Title: Re: Tags cloud in PHP block
Post by: Yağız... on April 10, 2009, 04:38:55 PM
Code: [Select]
   global $context, $smcFunc, $modSettings, $txt, $scripturl;

   $context['tags'] = array();

   // A big thank you to Prism Perfect for this code!
   // http://prism-perfect.net/archive/php-tag-cloud-tutorial/

   $result = $smcFunc['db_query']('', '
      SELECT t.tag_name, COUNT(tt.id_tag) AS amount
      FROM {db_prefix}tags AS t
         INNER JOIN {db_prefix}tag_topics AS tt ON (tt.id_tag = t.id_tag)
      GROUP BY tt.id_tag
      LIMIT {int:limit}',
      array(
         'limit' => $modSettings['tags_to_show']
      )
   );

   while ($row = $smcFunc['db_fetch_assoc']($result)) {
      $tags[$row['tag_name']] = $row['amount'];
   }

   $smcFunc['db_free_result']($result);

   // Let's shuffle these puppies up, to make it more fun
   $keys = array_keys($tags);
   shuffle($keys);

   foreach ($keys as $key)
      $context['tags'][$key] = $tags[$key];

   $context['max_size'] = (int) $modSettings['max_tag_percentage'];
   $context['min_size'] = (int) $modSettings['min_tag_percentage'];
   $context['max_qty'] = (int) max(array_values($context['tags']));
   $context['min_qty'] = (int) min(array_values($context['tags']));

   $spread = $context['max_qty'] - $context['min_qty'];
   $spread = ($spread > 0) ? $spread : 1;

   $context['step'] = ($context['max_size'] - $context['min_size']) / $spread;

   // Load the main Tags template
   loadtemplate('Tagging');

  // I'm settings this here so that you can change it on a per-theme basis
   $context['tag_colors'] = array(
      '#0AC92B', // Green
      '#CC00FF', // Purple
      '#FF0033', // Red
      '#4D4DFF', // Blue
      '#000000', // Black
   );

   $num_colors = count($context['tag_colors']) - 1;

   echo '
      <table border="0" cellspacing="0" cellpadding="8" align="center" class="tborder" style="width: 60%;">
         <tr class="catbg3">
            <td align="center"><b>', $txt['tags_tag_cloud'], '</b></td>
         </tr>
         <tr>
            <td class="windowbg" style="padding: 1em 2em 1em 2em; text-align: center;">';

   if(!empty($context['tags']))
      foreach ($context['tags'] as $key => $value){
         $color = $context['tag_colors'][mt_rand(0, $num_colors)];
         $size = ceil($context['min_size'] + (($value - $context['min_qty']) * $context['step']));
         // The index is so we can put smaller elements in front, so they won't get swallowed by the big ones.
         $index = abs($size - $context['max_size']);

         echo '<a href="', $scripturl, '?action=tags;name=', urlencode($key), '" style="z-index: ', $index, '; position: relative; font-size: ', $size, '%; color: ', $color, ';" title="', $value, ' ', $txt['tags_tagged_with'], ' ', ucwords($key), '">', str_replace(' ', '&nbsp;', ucwords($key)), '</a>&nbsp;<span class="smalltext">(', $value, ')</span> ';
      }
   else
      echo $txt['tags_none_in_cloud'];

   echo '
            </td>
         </tr>
      </table>';
Not sure but you can try this.
Title: Re: Tags cloud in PHP block
Post by: fotografo74 on April 11, 2009, 12:49:00 AM
Code: [Select]
   global $context, $smcFunc, $modSettings, $txt, $scripturl;

   $context['tags'] = array();

   // A big thank you to Prism Perfect for this code!
   // http://prism-perfect.net/archive/php-tag-cloud-tutorial/

   $result = $smcFunc['db_query']('', '
      SELECT t.tag_name, COUNT(tt.id_tag) AS amount
      FROM {db_prefix}tags AS t
         INNER JOIN {db_prefix}tag_topics AS tt ON (tt.id_tag = t.id_tag)
      GROUP BY tt.id_tag
      LIMIT {int:limit}',
      array(
         'limit' => $modSettings['tags_to_show']
      )
   );

   while ($row = $smcFunc['db_fetch_assoc']($result)) {
      $tags[$row['tag_name']] = $row['amount'];
   }

   $smcFunc['db_free_result']($result);

   // Let's shuffle these puppies up, to make it more fun
   $keys = array_keys($tags);
   shuffle($keys);

   foreach ($keys as $key)
      $context['tags'][$key] = $tags[$key];

   $context['max_size'] = (int) $modSettings['max_tag_percentage'];
   $context['min_size'] = (int) $modSettings['min_tag_percentage'];
   $context['max_qty'] = (int) max(array_values($context['tags']));
   $context['min_qty'] = (int) min(array_values($context['tags']));

   $spread = $context['max_qty'] - $context['min_qty'];
   $spread = ($spread > 0) ? $spread : 1;

   $context['step'] = ($context['max_size'] - $context['min_size']) / $spread;

   // Load the main Tags template
   loadtemplate('Tagging');

  // I'm settings this here so that you can change it on a per-theme basis
   $context['tag_colors'] = array(
      '#0AC92B', // Green
      '#CC00FF', // Purple
      '#FF0033', // Red
      '#4D4DFF', // Blue
      '#000000', // Black
   );

   $num_colors = count($context['tag_colors']) - 1;

   echo '
      <table border="0" cellspacing="0" cellpadding="8" align="center" class="tborder" style="width: 60%;">
         <tr class="catbg3">
            <td align="center"><b>', $txt['tags_tag_cloud'], '</b></td>
         </tr>
         <tr>
            <td class="windowbg" style="padding: 1em 2em 1em 2em; text-align: center;">';

   if(!empty($context['tags']))
      foreach ($context['tags'] as $key => $value){
         $color = $context['tag_colors'][mt_rand(0, $num_colors)];
         $size = ceil($context['min_size'] + (($value - $context['min_qty']) * $context['step']));
         // The index is so we can put smaller elements in front, so they won't get swallowed by the big ones.
         $index = abs($size - $context['max_size']);

         echo '<a href="', $scripturl, '?action=tags;name=', urlencode($key), '" style="z-index: ', $index, '; position: relative; font-size: ', $size, '%; color: ', $color, ';" title="', $value, ' ', $txt['tags_tagged_with'], ' ', ucwords($key), '">', str_replace(' ', '&nbsp;', ucwords($key)), '</a>&nbsp;<span class="smalltext">(', $value, ')</span> ';
      }
   else
      echo $txt['tags_none_in_cloud'];

   echo '
            </td>
         </tr>
      </table>';
Not sure but you can try this.
Thanks!
I don't understand....in preview mode the block works perfect, but in normal mode i have this error:
Fatal error: Cannot redeclare template_main() (previously declared in /home/macrofor/public_html/Sources/Load.php(2015) : eval()'d code:6) in /home/macrofor/public_html/Sources/Load.php(2015) : eval()'d code on line 69

Thanks for helps!
Antonio
Title: Re: Tags cloud in PHP block
Post by: Yağız... on April 11, 2009, 05:13:58 AM
Remove that:
Code: [Select]
   // Load the main Tags template
   loadtemplate('Tagging');
Title: Re: Tags cloud in PHP block
Post by: Daydreamer on April 11, 2009, 12:27:30 PM
Sorry. I was write wrong topic.
Title: Re: Tags cloud in PHP block
Post by: fotografo74 on April 12, 2009, 03:08:12 PM
Remove that:
Code: [Select]
   // Load the main Tags template
   loadtemplate('Tagging');
Thanks,it works perfects! thanks!
i put the code in php block,no body,no title,and i modified width to 100%.
The results:www.macroforum.org,top block.
thanks!
antonio
Title: Re: [TIPS]Tags cloud in PHP block
Post by: marcozambi on April 29, 2009, 05:25:07 AM
Hi!
I modified code to just fit into a standard custom PHP block for SimplePortal 2.2
In this way we have better lines separation, centered text, and you can use normal block's Title property.
Code: [Select]
global $context, $smcFunc, $modSettings, $txt, $scripturl;

   $context['tags'] = array();

   // A big thank you to Prism Perfect for this code!
   // http://prism-perfect.net/archive/php-tag-cloud-tutorial/

   $result = $smcFunc['db_query']('', '
      SELECT t.tag_name, COUNT(tt.id_tag) AS amount
      FROM {db_prefix}tags AS t
         INNER JOIN {db_prefix}tag_topics AS tt ON (tt.id_tag = t.id_tag)
      GROUP BY tt.id_tag
      LIMIT {int:limit}',
      array(
         'limit' => $modSettings['tags_to_show']
      )
   );

   while ($row = $smcFunc['db_fetch_assoc']($result)) {
      $tags[$row['tag_name']] = $row['amount'];
   }

   $smcFunc['db_free_result']($result);

   // Let's shuffle these puppies up, to make it more fun
   $keys = array_keys($tags);
   shuffle($keys);

   foreach ($keys as $key)
      $context['tags'][$key] = $tags[$key];

   $context['max_size'] = (int) $modSettings['max_tag_percentage'];
   $context['min_size'] = (int) $modSettings['min_tag_percentage'];
   $context['max_qty'] = (int) max(array_values($context['tags']));
   $context['min_qty'] = (int) min(array_values($context['tags']));

   $spread = $context['max_qty'] - $context['min_qty'];
   $spread = ($spread > 0) ? $spread : 1;

   $context['step'] = ($context['max_size'] - $context['min_size']) / $spread;

  // I'm settings this here so that you can change it on a per-theme basis
   $context['tag_colors'] = array(
      '#0AC92B', // Green
      '#CC00FF', // Purple
      '#FF0033', // Red
      '#4D4DFF', // Blue
      '#000000', // Black
   );

   echo "<div style=\"text-align:center;\">";
   $num_colors = count($context['tag_colors']) - 1;
   if(!empty($context['tags']))
      foreach ($context['tags'] as $key => $value){
         $color = $context['tag_colors'][mt_rand(0, $num_colors)];
         $size = ceil($context['min_size'] + (($value - $context['min_qty']) * $context['step']));
         // The index is so we can put smaller elements in front, so they won't get swallowed by the big ones.
         $index = abs($size - $context['max_size']);

         echo '<a href="', $scripturl, '?action=tags;name=', urlencode($key), '" style="line-height:1.2; z-index: ', $index, '; position: relative; font-size: ', $size, '%; color: ', $color, ';" title="', $value, ' ', $txt['tags_tagged_with'], ' ', ucwords($key), '">', str_replace(' ', ' ', ucwords($key)), '</a> <span class="smalltext">(', $value, ')</span> ';
      }
   else
      echo $txt['tags_none_in_cloud'];
echo "</div>";
Title: Re: [TIPS]Tags cloud in PHP block
Post by: Blue on April 29, 2009, 04:22:20 PM
Hi,

I'm using Tagging System (by vgamer) and I wanted a block with the cloud system too.

So, I messed up here and there and IT'S WORKING ;D

If someone wants it, just add a php block with this code: (for SMF 2)
Code: [Select]
global $context, $txt, $mbname,$scripturl, $user_info, $smcFunc,  $modSettings;

  // Tag cloud from http://www.prism-perfect.net/archive/php-tag-cloud-tutorial/
$result = $smcFunc['db_query']('', "
SELECT
t.tag AS tag, l.ID_TAG, COUNT(l.ID_TAG) AS quantity
FROM {db_prefix}tags as t, {db_prefix}tags_log as l WHERE t.ID_TAG = l.ID_TAG
  GROUP BY l.ID_TAG
  ORDER BY l.ID DESC LIMIT " .  $modSettings['smftags_set_cloud_tags_to_show']);

// here we loop through the results and put them into a simple array:
// $tag['thing1'] = 12;
// $tag['thing2'] = 25;
// etc. so we can use all the nifty array functions
// to calculate the font-size of each tag
$tags = array();

$tags2 = array();

while ($row = $smcFunc['db_fetch_assoc']($result))
{
    $tags[$row['tag']] = $row['quantity'];
    $tags2[$row['tag']] = $row['ID_TAG'];
}

if (count($tags2) > 0)
{
// change these font sizes if you will
$max_size = $modSettings['smftags_set_cloud_max_font_size_precent']; // max font size in %
$min_size = $modSettings['smftags_set_cloud_min_font_size_precent']; // min font size in %

// get the largest and smallest array values
$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));

// find the range of values
$spread = $max_qty - $min_qty;
if (0 == $spread)
{ // we don't want to divide by zero
    $spread = 1;
}

// determine the font-size increment
// this is the increase per tag quantity (times used)
$step = ($max_size - $min_size)/($spread);

// loop through our tag array
$context['poptags'] = '';
$row_count = 0;
foreach ($tags as $key => $value)
{
$row_count++;
    // calculate CSS font-size
    // find the $value in excess of $min_qty
    // multiply by the font-size increment ($size)
    // and add the $min_size set above
    $size = $min_size + (($value - $min_qty) * $step);
    // uncomment if you want sizes in whole %:
    // $size = ceil($size);

    // you'll need to put the link destination in place of the #
    // (assuming your tag links to some sort of details page)
    $context['poptags'] .= '<a href="' . $scripturl . '?action=tags;tagid=' . $tags2[$key] . '" style="font-size: '.$size.'%"';
    // perhaps adjust this title attribute for the things that are tagged
   $context['poptags'] .= ' title="'.$value.' things tagged with '.$key.'"';
   $context['poptags'] .= '>'.$key.'</a> ';
   if ($row_count > ($modSettings['smftags_set_cloud_tags_per_row']-1))
   {
   $context['poptags'] .= '<br />';
   $row_count =0;
   }
    // notice the space at the end of the link
}
}


// Find Results
$dbresult = $smcFunc['db_query']('', "
SELECT DISTINCT l.ID_TOPIC, t.num_replies,t.num_views,m.id_member,m.poster_name,m.subject,m.id_topic,m.poster_time, m.thank_you_post_counter, t.id_board
FROM ({db_prefix}tags_log as l,{db_prefix}boards AS b, {db_prefix}topics as t, {db_prefix}messages as m)
WHERE b.ID_BOARD = t.id_board AND l.ID_TOPIC = t.id_topic AND t.approved = 1 AND t.id_first_msg = m.id_msg AND " . $user_info['query_see_board'] . " ORDER BY l.ID DESC LIMIT 20");

$context['tags_topics'] = array();
while ($row = $smcFunc['db_fetch_assoc']($dbresult))
{
$context['tags_topics'][] = array(
'id_member' => $row['id_member'],
'poster_name' => $row['poster_name'],
'subject' => $row['subject'],
'id_topic' => $row['id_topic'],
'poster_time' => $row['poster_time'],
'num_views' => $row['num_views'],
'num_replies' => $row['num_replies'],
'thank_you_post_counter' => $row['thank_you_post_counter'],
);
}
$smcFunc['db_free_result']($dbresult);
echo'
<center>';
if (isset($context['poptags']))
  echo $context['poptags'];
echo'
</center>';

Don't thank me, thank vgamer for this great mod ;)
Title: Re: [TIPS]Tags cloud in PHP block
Post by: fotografo74 on May 15, 2009, 03:01:47 AM
Hi!
I modified code to just fit into a standard custom PHP block for SimplePortal 2.2
In this way we have better lines separation, centered text, and you can use normal block's Title property.
Code: [Select]
global $context, $smcFunc, $modSettings, $txt, $scripturl;

   $context['tags'] = array();

   // A big thank you to Prism Perfect for this code!
   // http://prism-perfect.net/archive/php-tag-cloud-tutorial/

   $result = $smcFunc['db_query']('', '
      SELECT t.tag_name, COUNT(tt.id_tag) AS amount
      FROM {db_prefix}tags AS t
         INNER JOIN {db_prefix}tag_topics AS tt ON (tt.id_tag = t.id_tag)
      GROUP BY tt.id_tag
      LIMIT {int:limit}',
      array(
         'limit' => $modSettings['tags_to_show']
      )
   );

   while ($row = $smcFunc['db_fetch_assoc']($result)) {
      $tags[$row['tag_name']] = $row['amount'];
   }

   $smcFunc['db_free_result']($result);

   // Let's shuffle these puppies up, to make it more fun
   $keys = array_keys($tags);
   shuffle($keys);

   foreach ($keys as $key)
      $context['tags'][$key] = $tags[$key];

   $context['max_size'] = (int) $modSettings['max_tag_percentage'];
   $context['min_size'] = (int) $modSettings['min_tag_percentage'];
   $context['max_qty'] = (int) max(array_values($context['tags']));
   $context['min_qty'] = (int) min(array_values($context['tags']));

   $spread = $context['max_qty'] - $context['min_qty'];
   $spread = ($spread > 0) ? $spread : 1;

   $context['step'] = ($context['max_size'] - $context['min_size']) / $spread;

  // I'm settings this here so that you can change it on a per-theme basis
   $context['tag_colors'] = array(
      '#0AC92B', // Green
      '#CC00FF', // Purple
      '#FF0033', // Red
      '#4D4DFF', // Blue
      '#000000', // Black
   );

   echo "<div style=\"text-align:center;\">";
   $num_colors = count($context['tag_colors']) - 1;
   if(!empty($context['tags']))
      foreach ($context['tags'] as $key => $value){
         $color = $context['tag_colors'][mt_rand(0, $num_colors)];
         $size = ceil($context['min_size'] + (($value - $context['min_qty']) * $context['step']));
         // The index is so we can put smaller elements in front, so they won't get swallowed by the big ones.
         $index = abs($size - $context['max_size']);

         echo '<a href="', $scripturl, '?action=tags;name=', urlencode($key), '" style="line-height:1.2; z-index: ', $index, '; position: relative; font-size: ', $size, '%; color: ', $color, ';" title="', $value, ' ', $txt['tags_tagged_with'], ' ', ucwords($key), '">', str_replace(' ', ' ', ucwords($key)), '</a> <span class="smalltext">(', $value, ')</span> ';
      }
   else
      echo $txt['tags_none_in_cloud'];
echo "</div>";

Thanks Marco! It's better!
Grazie Marco, questo è migliore!
Antonio

SimplePortal 2.3.8 © 2008-2024, SimplePortal