GamerGun Posted February 25, 2009 Share Posted February 25, 2009 Hello y'all I'm having a problem which I'm not able to fix, so that's why I'm asking for your help. I have a database, which contains a table "games". In games, there is a field called "tagging". Those records (from "tagging") are being used for creating a tag cloud. The problem is that every record contains multiple words. For example: 3 foot, ninja, kickass, fight, sword, blood They are all comma separated. Now i use the following code: <?php $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = explode(",",$tagging['tagging']); $tags[$tagging['tagging']] = $tagging['count']; } $max = max($tags); foreach ($tags as $tagging => $hits) { $percent = round(($hits / $max) * 100); echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> "; } ?> This creates an cloud based on all the records, but these should be exploded. If i print the array, it shows me this: echo "<pre>"; print_r ($tags); echo "</pre>"; Array ( [* math, balls, lines, jet, game, games, flash, fun, awesome, win, fupa, puzzle, puzzles] => 1 [* word, text, puzzle, action, small, casual, family, friendly, dictionary, fast, quick, time] => 1 [3 foot, ninja, kickass, fight, sword, blood] => 1 [3d, ball, drop, plate] => 1 ) But what i want is every word separate. When i have that, i can use each word for the foreach to create the cloud. So to make a long story short: the cloud is now based on each record, instead of every word from each record. Hope that I'm clear. Thanks for any help. Pz -T Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/ Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 It is doing exactly what you told it to do. while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = explode(",",$tagging['tagging']); $tags[$tagging['tagging']] = $temp; } You explode it, then do nothing with the exploded value. And why are you using the original tagging data as an index, I would use the game name for the index, but that is just me. Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771021 Share on other sites More sharing options...
sasa Posted February 25, 2009 Share Posted February 25, 2009 try while( $tagging = mysql_fetch_assoc( $tagquery ) ) { str_replace(',', ',', $tagging['tagging'], $tmp); $tags[$tagging['tagging']] = $tmp+1; } Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771051 Share on other sites More sharing options...
blintas Posted February 25, 2009 Share Posted February 25, 2009 Try exploding twice, once for the commas then again for the spaces while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = explode(",",$tagging['tagging']); $temp2 = explode(" ",$temp); $tags[$tagging['tagging']] = $tagging['count']; } Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771058 Share on other sites More sharing options...
GamerGun Posted February 25, 2009 Author Share Posted February 25, 2009 It is doing exactly what you told it to do. while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = explode(",",$tagging['tagging']); $tags[$tagging['tagging']] = $temp; } You explode it, then do nothing with the exploded value. And why are you using the original tagging data as an index, I would use the game name for the index, but that is just me. You were right, but with using this, the count part is gone? try while( $tagging = mysql_fetch_assoc( $tagquery ) ) { str_replace(',', ',', $tagging['tagging'], $tmp); $tags[$tagging['tagging']] = $tmp+1; } I don't get what this is doing, seems like it's creating more lines, but still no separate words. Try exploding twice, once for the commas then again for the spaces while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = explode(",",$tagging['tagging']); $temp2 = explode(" ",$temp); $tags[$tagging['tagging']] = $tagging['count']; } I see what you mean, and this i think is the most logical, but i don't know how to use the data from $temp2, without loosing the $tagging['count']; Because now, it's again doing nothing with that data. Thanks all. Pz -T Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771099 Share on other sites More sharing options...
GamerGun Posted February 26, 2009 Author Share Posted February 26, 2009 Testing a bit, while waiting for an solution. With this code; $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = explode(",",$tagging['tagging']); $temp2 = explode(" ",$temp); $tags[$tagging['tagging']] = $tagging['count']; } print_r ($temp); I get this: Array ( [0] => zombies [1] => shooting [2] => ninja kiwi [3] => assault [4] => guns [5] => rifle [6] => shooting [7] => creeps [8] => walls ) With this; $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = explode(",",$tagging['tagging']); $temp2 = explode(" ",$temp); $tags[$tagging['tagging']] = $tagging['count']; } print_r ($temp2); It outputs Array ( [0] => Array ) 1. Why is temp2 empty? 2. Why is temp only outputting one line? 3. Most important, how to combine the while part with this: //$max = max($tags); foreach ($tags as $tagging => $hits) { $percent = round(($hits / $max) * 100); echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> "; } Thanks. Pz -T Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771662 Share on other sites More sharing options...
blintas Posted February 26, 2009 Share Posted February 26, 2009 hey dude, got your PM just realized we are dumb. $tagging['tagging'] is an array, so we shouldn't be exploding it to convert to a string. Try this, going to convert to a string, then do an str_replace, then put it back into an array u can use on the cloud $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = implode(",",$tagging['tagging']); $temp2 = str_replace(" ",",",$temp); $final_temp = explode(",",$temp2); //$temp = explode(",",$tagging['tagging']); //$temp2 = explode(" ",$temp); //$tags[$tagging['tagging']] = $tagging['count']; } print_r ($temp2); ok Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771764 Share on other sites More sharing options...
GamerGun Posted February 26, 2009 Author Share Posted February 26, 2009 Hello mate, Thanks for your response. Now i have: $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $temp = implode(",",$tagging['tagging']); $temp2 = str_replace(" ",",",$temp); $final_temp = explode(",",$temp2); } print_r ($final_temp); //$max = max($tags); //foreach ($tags as $tagging => $hits) //{ // $percent = round(($hits / $max) * 100); // echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> "; //} Which gives (a lot): Warning: implode() [function.implode]: Invalid arguments passed in index.php on line 162 I think the reason is because of the [ ] inside the implode function. I don't know a way around it tho. I tried this but that doesn't work of course hehe. $bla = $tagging['tagging']; $temp = implode(",",$bla); Thanks! Pz -T Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771777 Share on other sites More sharing options...
blintas Posted February 26, 2009 Share Posted February 26, 2009 print_r($tagging['tagging']); plz Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771778 Share on other sites More sharing options...
GamerGun Posted February 26, 2009 Author Share Posted February 26, 2009 No output, except for the same error message. Tried to create output like this: $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); while( $tagging = mysql_fetch_assoc( $tagquery ) ) { $tags[$tagging['tagging']] = $tagging['count']; //$temp = implode(",",$tagging['tagging']); //$temp2 = str_replace(" ",",",$temp); //$final_temp = explode(",",$temp2); } print_r($tagging['tagging']); But then it's all empty, no output. Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771782 Share on other sites More sharing options...
blintas Posted February 26, 2009 Share Posted February 26, 2009 well if it's empty that means there is no data in your tables....? that's why you can't pass the argument through?? Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771801 Share on other sites More sharing options...
GamerGun Posted February 26, 2009 Author Share Posted February 26, 2009 There is, since i get output with the code in the start post. Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771803 Share on other sites More sharing options...
blintas Posted February 26, 2009 Share Posted February 26, 2009 don't do a while statement, just print_r Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771808 Share on other sites More sharing options...
GamerGun Posted February 26, 2009 Author Share Posted February 26, 2009 You mean like this? //tagcloud proberen $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); //while( $tagging = mysql_fetch_assoc( $tagquery ) ) { //$tags[$tagging['tagging']] = $tagging['count']; $temp = implode(",",$tagging['tagging']); $temp2 = str_replace(" ",",",$temp); $final_temp = explode(",",$temp2); } print_r($tagging['tagging']); //$max = max($tags); //foreach ($tags as $tagging => $hits) //{ // $percent = round(($hits / $max) * 100); // echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> "; //} //einde tagcloud proberen Gives: Warning: implode() [function.implode]: Invalid arguments passed in /home/mqxfaokl/domains/domain.nl/public_html/index.php on line 165 Just once. Thx Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771809 Share on other sites More sharing options...
blintas Posted February 26, 2009 Share Posted February 26, 2009 //tagcloud proberen $tags = Array(); $tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging FROM games GROUP BY tagging ORDER BY tagging ") or die( mysql_error() ); /* //while( $tagging = mysql_fetch_assoc( $tagquery ) ) { //$tags[$tagging['tagging']] = $tagging['count']; $temp = implode(",",$tagging['tagging']); $temp2 = str_replace(" ",",",$temp); $final_temp = explode(",",$temp2); } */ $tagging = mysql_fetch_assoc( $tagquery ) print_r($tagging['tagging']); //$max = max($tags); //foreach ($tags as $tagging => $hits) //{ // $percent = round(($hits / $max) * 100); // echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> "; //} //einde tagcloud proberen Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-771811 Share on other sites More sharing options...
GamerGun Posted February 26, 2009 Author Share Posted February 26, 2009 You forgot a ; after $tagging = mysql_fetch_assoc( $tagquery ) But this gives: * math, balls, lines, jet, game, games, flash, fun, awesome, win, fupa, puzzle, puzzles Thanks Pz -T Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-772073 Share on other sites More sharing options...
GamerGun Posted March 3, 2009 Author Share Posted March 3, 2009 Bump Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-775305 Share on other sites More sharing options...
GamerGun Posted March 12, 2009 Author Share Posted March 12, 2009 Can anyone please help? Quote Link to comment https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/#findComment-782816 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.