Jump to content

[SOLVED] Counting how many dupliate records


CoreyR

Recommended Posts

Ok, I have a field name "cname" and there are multiple records some are the same, some are not.

 

Example:

1. ebay

2. google

3. ebay

4. yahoo

5. ebay

 

How would I go about printing a list like this?

Ebay = 3

Google = 1

Yahoo =1

 

Iv'e tried the foreach function, but for some reason it is not valid. I know you can only use a foreach with an array, but I'm just learning arrays.

 

Thanks

Link to comment
Share on other sites

$result = mysql_db_query(SELECT * FROM table ORDER BY field_name);
$temp = "";

while ($data = mysql_fetch_array($result)) {
  if ($temp != $data[field_name]) {
     $temp = $data[field_name];
     $num_rows = mysql_num_rows(SELECT * FROM table WHERE field_name = '$temp');
     echo "$temp = $num_rows<br />";
  }
}

 

That should work..if not tell me errors and I'll figure it out. I'm at work and have no way to look at my scripts to see if it's right.

Link to comment
Share on other sites

$result = mysql_db_query(SELECT * FROM table ORDER BY field_name);
$temp = "";

while ($data = mysql_fetch_array($result)) {
   if ($temp != $data[field_name]) {
      $temp = $data[field_name];
      $num_rows = mysql_num_rows(SELECT * FROM table WHERE field_name = '$temp');
      echo "$temp = $num_rows<br />";
   }
}

 

That should work..if not tell me errors and I'll figure it out.  I'm at work and have no way to look at my scripts to see if it's right.

 

Besides syntax errors, that wouldn't work if the arrays weren't ordered by field_name, as the same field_name would still be displayed if it came more than 1 row after the last one of the same name.

 

I'd add all the records to an array as they come, checking each time to see if the item already exists in the array to avoid duplication.

 

This should work:

 

// DECLARE THE ARRAY
$field_array=array();

// SELECT ALL THE FIELDS FROM THE DATABASE
$result=mysql_query("SELECT field FROM table") or die(mysql_error());

// ITERATE THROUGH THE RESULTS OF THE ABOVE  QUERY
while ($row=mysql_fetch_array($result,MYSQL_BOTH)){

// ADDS EACH FIELD TO TO AN ARRAY ONLY ONCE
if (!in_array($row[field],$field_array)){
$field_array[]=$row[field];

// FINDS OUT HOW MANY RESULTS FOR EACH FIELD THEY ARE
$num_fields=mysql_num_rows(mysql_query("SELECT * FROM tableWHERE field LIKE '$row[field]'"));
echo("$row[field] - $num_fields </br>");
}
}

Link to comment
Share on other sites

if you order by fieldname..it rearranges the table by field name, grouping all the same together before it is processed. then it would cycle through each enrty, it only grabs the num rows in the DB WHERE $field_name = $temp, after it decides if that $temp != $data[fieldname]. when it does come across one that doesnt equal it, it assigns $temp the new value, searches the table for all the rows with field_name = $temp, counts them up, displays and continues to run through each row, till it finds a nother that isnt equal

 

it should work cuz i use the same concept to group names based on a certain type in another website i have. if you order by, it orders the whole table in that order grouping ebay, yahoo, etc together...

 

Besides, I don't see syntax errors. and it IS ordered by field_name (you jsut replace 'field_name' with the appropriate fieldname.............the same with any instance of 'table'

 

the only reason it wouldnt work is if i typed something wrong. but the concept is right as long as everything is typed right.

Link to comment
Share on other sites

Ah right I didn't see the order by in the first query, my mistake oh and the syntax errors were the bad typing yeah, both methods should work fine.

 

Also for speed's sake, in either method use "GROUP BY field", instead of "ORDER BY field" asc as it limits the amount of results to the query to how many unique fields there are.

Link to comment
Share on other sites

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.