benji87 Posted March 5, 2008 Share Posted March 5, 2008 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> Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 5, 2008 Share Posted March 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 5, 2008 Author Share Posted March 5, 2008 Im not sure why its set like that. This isnt my own code im just trying to modify it. The link isnt set thats part of what im asking on how to set it. Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 5, 2008 Share Posted March 5, 2008 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' Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 5, 2008 Author Share Posted March 5, 2008 Ok ive got that sorted there is only one entry per tag and the count is incremented. Now how do i get the link variable to work and increment of +1 in that tags row everytime a tagis clicked? Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 5, 2008 Share Posted March 5, 2008 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}'"); Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 6, 2008 Author Share Posted March 6, 2008 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! Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 To get the sorting it appears that your best bet is to put the sort in the query and get rid of the ksort(); "SELECT tag, link, count FROM tags ORDER BY tag" Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 6, 2008 Author Share Posted March 6, 2008 But im not trying to sort anything im trying to get it to register the $link variable inside $cloud_tags = array() Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 Sorry, id10T error, I didn't scroll down far enough. Is this the only place get_tag_data() is used? That will have a big effect on my suggestion. Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 6, 2008 Author Share Posted March 6, 2008 Yer this is the whole script i posted Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 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; }; Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 6, 2008 Author Share Posted March 6, 2008 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? Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 6, 2008 Author Share Posted March 6, 2008 Were getting there! I now have this error: Warning: Invalid argument supplied for foreach() in /home/alns/public_html/contactfrm/tag.php on line 28 Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 Goodness I did a sloppy job with this. Either replace $arr with $tags or replace $tags with $arr. Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 6, 2008 Author Share Posted March 6, 2008 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> Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 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']; } ... ?> Quote Link to comment Share on other sites More sharing options...
benji87 Posted March 7, 2008 Author Share Posted March 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 7, 2008 Share Posted March 7, 2008 Glad I could help. Sorry it was soooo painful. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.