Jump to content

need help with why tag cloud doesn't work


webguync

Recommended Posts

Hey there, I am trying out some tag cloud code, but something is amiss, as am getting no tags displaying. Using PHP/MYSQL and JQuery.

<?php

//connection information
  $host = "localhost";
  $user = "username";
  $password = "pw";
  $database = "DBName";

//make connection
  $server = mysql_connect($host, $user, $password);
  $connection = mysql_select_db($database, $server);

//query the database
  $query = mysql_query("SELECT * FROM tags");


//start json object
$json = "({ tags:["; 

//loop through and return results
  for ($x = 0; $x < mysql_num_rows($query); $x++) {
    $row = mysql_fetch_assoc($query);

	//continue json object
    $json .= "{tag:'" . $row["tag"] . "',freq:'" . $row["frequency"] . "'}";

	//add comma if not last row, closing brackets if is
	if ($x < mysql_num_rows($query) -1)
		$json .= ",";
	else
		$json .= "]})";
  }

//return JSON with GET for JSONP callback
$response = $_GET["callback"] . $json;
echo $response;

//close connection
mysql_close($server);

?>



and the JS

 

<script type="text/javascript">
		$(function() {

			//get tag feed
			$.getJSON("/tagcloud.php?callback=?", function(data) {

				//create list for tag links
				$("<ul>").attr("id", "tagList").appendTo("#tagCloud");

				//create tags
				$.each(data.tags, function(i, val) {

					//create item
					var li = $("<li>");

					//create link
					$("<a>").text(val.tag).attr({title:"See all pages tagged with " + val.tag, href:"http://localhost/tags/" + val.tag + ".html"}).appendTo(li);

					//set tag size
					li.children().css("fontSize", (val.freq / 10 < 1) ? val.freq / 10 + 1 + "em": (val.freq / 10 > 2) ? "2em" : val.freq / 10 + "em");

					//add to list
					li.appendTo("#tagList");

				});
			});
		});
	</script>

 

when i turn on error reporting I get a lot of undefined index notices for tag and frequency so not sure if that is causing the problem or not.

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.