siwelis Posted May 12, 2008 Share Posted May 12, 2008 I want to count each unique entry from every row within in one comma delimited column, while adding duplicate entries to the total count for the uniques... I hope I'm explaining this right. E.g. Column_A ROW1: What, Hello, Hi ROW2: Hello, Hey, Hi ROW2: Hello, Wazza, Hello echo totals: What = 1, Hello = 3, Hi = 2 etc... How do I extract and count column A's data and echo it? I've been working on this problem on and off for months. I will really appreciate the help on this puppy! Quote Link to comment https://forums.phpfreaks.com/topic/105338-solved-comma-delimited-column-extraction-lost/ Share on other sites More sharing options...
soycharliente Posted May 12, 2008 Share Posted May 12, 2008 An inefficient way that you could do it would be to create a multi-dimension array that will store each word and it's count. Then as you move through each row, check to see if each word is contained in the array, if so, increment the count, if not, add it and a count of 1. That probably has a large O, but fairly straight forward IMO. Quote Link to comment https://forums.phpfreaks.com/topic/105338-solved-comma-delimited-column-extraction-lost/#findComment-539522 Share on other sites More sharing options...
resago Posted May 12, 2008 Share Posted May 12, 2008 nah, just 1 array, use the words as keys and just increment the value if $a is a word from your csv then do $frequency["$a"]++; you get undefined index warnings, but they are expected in this instance. then just do foreach ($key->$value) echo "$key:$value/n<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/105338-solved-comma-delimited-column-extraction-lost/#findComment-539526 Share on other sites More sharing options...
Barand Posted May 12, 2008 Share Posted May 12, 2008 try <?php $sql = "select columnA FROM myTableName"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); $results = array(); while ($row = mysql_fetch_row($res)) { $arr = explode(',', $row[0]); foreach ($arr as $word) { $word = trim($word); if (isset($results[$word])) $results[$word]++; else $results[$word] = 1; } } echo '<pre>', print_r($results, true), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105338-solved-comma-delimited-column-extraction-lost/#findComment-539532 Share on other sites More sharing options...
siwelis Posted June 27, 2008 Author Share Posted June 27, 2008 Thank you all very much! I wanted to post the rest of the code here I used so it my help someone else in the future. Sorry it took so long, because I wanted to make sure I had a working example of the code as well. I ended up using the code from Barand except for the last part I added this: arsort($results); foreach($results as $word=>$count) { //limit to most used if ($count>= 5){ echo 'The number of '.$word.' is '.$count.'<br>'; } } I used arsort to get some order in the mix and created an IF statement to limit the results to the highest numbers. I have a finished tag section of my site, where I used comma delimited "keywords" as tags. Quote Link to comment https://forums.phpfreaks.com/topic/105338-solved-comma-delimited-column-extraction-lost/#findComment-576119 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.