Jump to content

Tag Cloud


benji87

Recommended Posts

Can someone please help me? Im trying to modify this code to include a url defined as $link into the array but im unsure of how to do it. Also this code is supposed to add 1 to a count coulmn in the database everytime a link is clicked but it doesnt seem to do this either. Could someone take a look and help me out? Thanks

 

<?php

function get_tag_data() { 
  include "db.php";
  $result = mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC"); 
  while($row = mysql_fetch_array($result)) { 
    $arr[$row['tag']] = $row['count'];
  } 
  ksort($arr); 
  return $arr; 
}


function get_tag_cloud() {

// Default font sizes
$min_font_size = 12;
$max_font_size = 30;

// Pull in tag data
$tags = get_tag_data();

$minimum_count = min(array_values($tags));
$maximum_count = max(array_values($tags));
$spread = $maximum_count - $minimum_count;

if($spread == 0) {
    $spread = 1;
}

$cloud_html = '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $count) {
$size = $min_font_size + ($count - $minimum_count) 
	* ($max_font_size - $min_font_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
	. '" class="tag_cloud" href="' . $link 
	. '" title="\'' . $tag  . '\' returned a count of ' . $count . '">' 
	. htmlspecialchars(stripslashes($tag)) . '</a>';
}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;
};

?>
<h3>Sample Tag Cloud results</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

Link to comment
Share on other sites

Where does the value for $link get set?  You use it in the foreach loop but you never set it anywhere.

 

 

Just curious here. Why do you sort twice?  You sort by most popular link "ORDER BY count DESC" in your query then by the tag name before you return it.

 

 

Link to comment
Share on other sites

Gotcha!

 

Is this the only place get_tag_data() is used?  That will have a big effect on my guess for a fix.

 

ASSUMPTION There is a column in the table named link that has the url.

 

Lets take a close look at this query first.  How you describe the data and this query don't quite match up.  If the tag value is unique in the  table with an incremented count then the GROUP BY clause is not at all necessary.  If the tag value is not unique and there are many entries for each tag then the count value is misleading.

 

If there is one and only one entry per tag with an incremented count field then the query should look something like

'SELECT tag, link, count FROM tags'

 

If there are many entries per tag and no count field then the query needs to look something like

'SELECT tag, link, COUNT(*) as count FROM tags GROUP BY tag, link'

 

If there are many entries per tag with a count field (I don't know how your code would update this count) then the query should look something like

'SELECT tag, link, SUM(count) as count FROM tags GROUP BY tag, link'

 

 

Link to comment
Share on other sites

Now that is an interesting nut to crack.  Since you are sending the browser to another location you are looking at a client side piece of information.  Long story short send an AJAX (or like) request using the onclick of the <a> to send the tag back to you.

 

Your <a> would look something like:

 

<a href="http:some.valid.link.com" onclick="increment('Tag Name');">Tag Name</a>

 

In an included JavaScript file or script block in the head of your document:

function increment(tag) {
  //most of this garbage is just to accommodate IE
  var request = null;
  try {request = new XMLHttpRequest(); } 
  catch (trymicrosoft) {
    try {request = new ActiveXObject("msxml12.XMLHTTP");} 
    catch (othermicrosoft) {
      try {request = new ActiveXObject("Microsoft.XMLHTTP");} 
      catch (failed) {
        //do nothing, the browser can't submit the request
        return;
      }
    }
  }

  request.open("POST", 'some_script.php', true);
  request.onreadystatechange = doSomething;
  request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  request.send('action=increment&tag=' + escape(tag));  
}
function doSomething(){
  //deal with the response if you want to
}

 

In a script on the server side (some_script.php in this case) the update portion would look like:

  $tag = $_POST['tag'];
  // do any cleanup or validation you see as appropriate to $tag then...
  include "db.php";
  $result = mysql_query("UPDATE tags SET count = count + 1 where tag = '{$tag}'"); 

Link to comment
Share on other sites

Ok i figured out how to increment the count by parsing the url to a php file like you explained. But in the url i need to parse the link variable but i cant get the link to variable to register from $link = $row['link'] inside the cloud tag array. If i try and echo it before ksort() then it works no problem but i need it inside the cloud tag array!

 

here is my code, i just to sort this and its done!

 

<?php
function get_tag_data() { 
  include "db.php";
  $result = mysql_query("SELECT tag, link, count FROM tags"); 
  while($row = mysql_fetch_array($result)) { 
$arr[$row['tag']] = $row['count'];
$link = $row['link'];
//
// this is where i can echo links echo =$link //
//
  } 
  ksort($arr); 
  return $arr; 
}


function get_tag_cloud() {

// Default font sizes
$min_font_size = 12;
$max_font_size = 30;

// Pull in tag data
$tags = get_tag_data();

$minimum_count = min(array_values($tags));
$maximum_count = max(array_values($tags));
$spread = $maximum_count - $minimum_count;

if($spread == 0) {
    $spread = 1;
}

$cloud_html = '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $count) {
$size = $min_font_size + ($count - $minimum_count) 
	* ($max_font_size - $min_font_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
	. '" class="tag_cloud" href="tagprocess.php?tag=' . $link . '" title="\'' . $tag  . '\' returned a count of ' . $count . '">' 
	. htmlspecialchars(stripslashes($tag)) . '</a>';

}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;
};

?>
<h3>Sample Tag Cloud results</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

 

Please help!

Link to comment
Share on other sites

Cool.

 

  ksort($arr);

  return $arr;

}

 

 

function get_tag_cloud() {

 

// Default font sizes

$min_font_size = 12;

$max_font_size = 30;

 

// Pull in tag data

 

I would get rid of the function entirely.  It isn't buying you anything having it broken out from the get_tag_cloud() function and you need more than one piece of information from it.

 

function get_tag_data() { 
include "db.php";

$minimum_count = 0;
$maximum_count = 0;

$result = mysql_query("SELECT tag, link, count FROM tags ORDER BY tag"); 
while($row = mysql_fetch_array($result)) { 
  if ($row['count'] > $maximum_count) {
     $maximum_count = $row['count'];
  }
  if (!$minimum_count || $row['count'] < $minimum_count) {
    $minimum_count = $row['count'];
  }
  $arr[$row['tag']]['count'] = $row['count'];
  $arr[$row['tag']]['link'] = $row['link'];
} 

$spread = $maximum_count - $minimum_count;

if($spread == 0) {
    $spread = 1;
}

$cloud_html = '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $info) {
$size = $min_font_size + ($info['count'] - $minimum_count) 
	* ($max_font_size - $min_font_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
	. '" class="tag_cloud" href="tagprocess.php?tag=' . $info['link'] . '" title="\'' . $tag  . '\' returned a count of ' . $info['count ']. '">' 
	. htmlspecialchars(stripslashes($tag)) . '</a>';

}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;
};

Link to comment
Share on other sites

I think were finally getting there but ur going to hate me because i forgot to include this bot of code in the quote

 

<h3>Sample Tag Cloud results</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

 

which does include

get_tag_cloud();

 

This is what displays all the tags obviously. How do i display the tags with ur code without that function?

Link to comment
Share on other sites

Dohhh!!!!!

 

Sorry about that.  That was a bad copy paste on my part.  What I posted should be get_tag_cloud() not get_tag_data(). 

 

 

Just change

function get_tag_data() { 

 

... to ...

 

function get_tag_cloud() { 

 

 

.. and you should be good to go.

Link to comment
Share on other sites

I just get a blank page? Here is my current code:

 

<?php
function get_tag_cloud() { 
include "db.php";

$minimum_count = 0;
$maximum_count = 0;

$result = mysql_query("SELECT tag, link, count FROM tags ORDER BY tag"); 
while($row = mysql_fetch_array($result)) { 
  if ($row['count'] > $maximum_count) {
     $maximum_count = $row['count'];
  }
  if (!$minimum_count || $row['count'] < $minimum_count) {
    $minimum_count = $row['count'];
  }
  $tags[$row['tag']]['count'] = $row['count'];
  $tags[$row['tag']]['link'] = $row['link'];
} 

$spread = $maximum_count - $minimum_count;

if($spread == 0) {
    $spread = 1;
}

$cloud_html = '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $info) {
$size = $min_font_size + ($info['count'] - $minimum_count) 
	* ($max_font_size - $min_font_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
	. '" class="tag_cloud" href="tagprocess.php?tag=' . $info['link'] . '" title="\'' . $tag  . '\' returned a count of ' . $info['count ']. '">' 
	. htmlspecialchars(stripslashes($tag)) . '</a>';

}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;
};
?>

<h3>Sample Tag Cloud results</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

Link to comment
Share on other sites

Sorry about this.  If I could execute the code myself I would catch these sloppy mistakes...

 

I added two lines (look for the <----here comments)

<?php
function get_tag_cloud() { 
include "db.php";

$minimum_count = 0;
$maximum_count = 0;
$tags = array();                      //<------------------here
$result = mysql_query("SELECT tag, link, count FROM tags ORDER BY tag"); 
while($row = mysql_fetch_array($result)) { 
  if ($row['count'] > $maximum_count) {
     $maximum_count = $row['count'];
  }
  if (!$minimum_count || $row['count'] < $minimum_count) {
    $minimum_count = $row['count'];
  }
  $tags[$row['tag']] = array();    //<------------------here
  $tags[$row['tag']]['count'] = $row['count'];
  $tags[$row['tag']]['link'] = $row['link'];
}
...
?>

Link to comment
Share on other sites

Brilliant! I finally got it working! Thanks for all your help! I still got a blank page after using the final code u gave me. So i put all error reporting on and found that the code had the min and max font size missing! After that it worked fine.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.