paulmo Posted December 30, 2009 Share Posted December 30, 2009 first I'm inserting in table (this works): mysql_query("INSERT INTO user (name, message, created) VALUES ('$name', '$message', NOW()) ") or die(mysql_error()); next I need to retrieve (limit) the top 15 words in message column, user table, sorted vertically with occurrence # beside the word, like: sea 23 tree 12 leaf 3 etc. I've started this but I need help. $archive = mysql_query("SELECT * FROM user ORDER BY message") or die (mysql_error()); $results = mysql_query($archive); Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/ Share on other sites More sharing options...
fenway Posted December 30, 2009 Share Posted December 30, 2009 MySQL can't help you there -- you'll have to write a stored procedure or do this in application code. Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-985788 Share on other sites More sharing options...
paulmo Posted December 30, 2009 Author Share Posted December 30, 2009 thanks, real helpful. go sox. Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-985797 Share on other sites More sharing options...
fenway Posted December 30, 2009 Share Posted December 30, 2009 thanks, real helpful. go sox. ??? Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-985827 Share on other sites More sharing options...
paulmo Posted December 30, 2009 Author Share Posted December 30, 2009 after some consideration i'm pretty sure i'll do this with mysql. this is getting closer but it's only returning user text submit words, not the top 15 words in column 'message', which is what I want. mysql_query("INSERT INTO user (name, message, created) VALUES ('$name', '$message', NOW()) ") or die(mysql_error()); $archive = mysql_query("SELECT CONCAT(message, ' ', COUNT(*)) FROM `user` GROUP BY message ORDER BY COUNT(*) DESC LIMIT 15") or die (mysql_error()); $n=0; while($row = mysql_fetch_assoc($archive)){ $n++; echo ($row['message']); echo "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-986047 Share on other sites More sharing options...
fenway Posted January 3, 2010 Share Posted January 3, 2010 Huh? I don't see any searching. Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-987741 Share on other sites More sharing options...
kickstart Posted January 4, 2010 Share Posted January 4, 2010 Hi Crude way of doing it in php would be something like this (not tested):- <?php $archive = mysql_query("SELECT message FROM user ORDER BY message") or die (mysql_error()); $results = mysql_query($archive); $WordCount = array(); while ($Messages = mysql_fetch_array($result)) { $Words = explode(' ',$Messages); foreach($Words AS $Word) $WordCount[$Word]++; } arsort($WordCount); $iCnt = 0; foreach($WordCount AS $field=>$value) { echo "$field occurs $value <br/>"; $iCnt++; if ($iCnt >= 15) break; } ?> Only splits words up based on spaces, so you would probably want to modify it to split the messages up properly While I can think of a way of doing this in pure MySQL it would not be nice code and I wouldn't want to use it for real. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-988170 Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 thanks, real helpful. go sox. I'm guessing you are a redneck Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-988199 Share on other sites More sharing options...
paulmo Posted January 6, 2010 Author Share Posted January 6, 2010 thanks Keith! helps to see how a guru would do it. I'll have to study your arsort () more closely. Quote Link to comment https://forums.phpfreaks.com/topic/186650-limitsort-most-common-words-in-column-help/#findComment-989279 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.