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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.