Jump to content

[SOLVED] Comma Delimited Column Extraction - Lost!


siwelis

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.