Jump to content

Recommended Posts

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!

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.

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>";

 

 

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>';
?>

  • 1 month later...

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.