webguync Posted January 7, 2011 Share Posted January 7, 2011 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 https://forums.phpfreaks.com/topic/223731-need-help-with-why-tag-cloud-doesnt-work/ Share on other sites More sharing options...
GrooN Posted January 8, 2011 Share Posted January 8, 2011 Now before reading your code throughout, I have to ask, is there any special reason why you don't use the function json_encode()? ^^ - or did you know if its existence at all? Link to comment https://forums.phpfreaks.com/topic/223731-need-help-with-why-tag-cloud-doesnt-work/#findComment-1156476 Share on other sites More sharing options...
webguync Posted January 8, 2011 Author Share Posted January 8, 2011 I didn't know of it's existence. Please explain! Link to comment https://forums.phpfreaks.com/topic/223731-need-help-with-why-tag-cloud-doesnt-work/#findComment-1156477 Share on other sites More sharing options...
GrooN Posted January 8, 2011 Share Posted January 8, 2011 Well it is not that complicated, it encodes your object into json format ^^ You can read more about it here: http://php.net/manual/en/function.json-encode.php And when you use json_encode, you should also know json_decode, which does the opposite Link to comment https://forums.phpfreaks.com/topic/223731-need-help-with-why-tag-cloud-doesnt-work/#findComment-1156481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.