Remov duplicates from array

Reply

Join Date: Jun 2008
Posts: 6
Reputation: bigbob is an unknown quantity at this point 
Solved Threads: 0
bigbob bigbob is offline Offline
Newbie Poster

Remov duplicates from array

 
0
  #1
Dec 4th, 2008
I hope someone can help I've been trying to sort this for hours.
I am trying to pull info from my database and display everything which I have done successfully, however I have duplicate records in my database due to records in different subcategories.
Anyway I want to display everything but only once and not display the duplicates.
At pres my code is this:
  1. <?php if(isset($_GET['alph'])){
  2. $alph = $_GET['alph'];
  3. $sql = "SELECT * FROM paid WHERE title LIKE '$alph%' ORDER by asc";
  4. $query = mysql_query($sql);
  5. while($data = mysql_fetch_array($query)){
  6. $desc = substr($data['description'],0,100);
  7. echo "<tr><td valign=\"top\">".$data['title']." ".$data['description']."</tr></td>";
  8. }}
  9. ?>
Now I have tryed DISTINCT in the sql statement and that didn't work and I have tryed using array_unique() eveywhere but to be honest I'm not exactly sure how to use array_unique() with mysql_fetch_array.
If someone could point me in the right direction it will save this hole in my wall getting any bigger as I feel I can do nothing but bash my head against it.

PLEASE HELP!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Remov duplicates from array

 
0
  #2
Dec 4th, 2008
Well you can't really do array_unique on mysql_fetch_array since it actually acts as an iterator for the MYSQL Resource that is returned by mysql query.
The correct way to fix it would be to fix your database to have unique keys so you don't have duplicates.
The hacky way to fix it would be to choose a field like title and add the title to another array as you iterate through the list. and don't output if it already exists in the array.
  1. $entries = array();
  2. while($data = mysql_fetch_array($query)) {
  3. $entries[]=$data['title'];
  4. if(!in_array($data['title'], $entries)) {
  5. // Your code here
  6. }
  7. }
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Remov duplicates from array

 
0
  #3
Dec 5th, 2008
There is way to remove duplicate data or row like...
Step 1: Move the non duplicates (unique tuples) into a temporary table
  1. CREATE TABLE new_table AS
  2. SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];

Step 2: delete delete the old table
  1. DROP TABLE old_table;

Step 3: rename the new_table to the name of the old_table
  1. RENAME TABLE new_table TO old_table;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 6
Reputation: bigbob is an unknown quantity at this point 
Solved Threads: 0
bigbob bigbob is offline Offline
Newbie Poster

Re: Remov duplicates from array

 
0
  #4
Dec 5th, 2008
Hi thanks for the reply's. Aamit I dont want to delete the duplicates I just don't want to display them when I echo out the results form the table I just want to display the results once.

Thanks ShawnCplus, but when I tryed your surgestgion I didn't get anything to display.

So still trying. Thanks though guys
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Remov duplicates from array

 
0
  #5
Dec 5th, 2008
then you do like this...example
  1. CREATE TABLE table2 (
  2. id INT NOT NULL UNIQUE AUTO_INCREMENT,
  3. name VARCHAR(20) NOT NULL
  4. );
  5.  
  6. INSERT INTO table2(id,name) VALUES
  7. (1,'Things Fall Apart'),
  8. (2,'Things Fall Apart'),
  9. (3,'The Famished Road'),
  10. (4,'Things Fall Apart'),
  11. (5,'The Famished Road'),
  12. (6,'Thirteen cents'),
  13. (7,'Thirteen cents');
  14.  
  15. CREATE TABLE
  16. temp2(id VARCHAR(10), name VARCHAR(20))
  17. TYPE=HEAP;
  18.  
  19. INSERT INTO temp2(name) SELECT DISTINCT name FROM table2;
  20.  
  21. DELETE FROM table2;
  22.  
  23. INSERT INTO table2(id,name) SELECT id,name FROM temp2;

you can place your data in temp table..and display main table without duplication of data by using DISTINCT .....
Last edited by Aamit; Dec 5th, 2008 at 9:05 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Remov duplicates from array

 
0
  #6
Dec 5th, 2008
you can do like this also....
your main table is paid table.....

insert all data of paid table in table2....
and again insert data in temp table and by using distinct...

you got your output without duplication...in table2....
display table2....

and your paid table remain as original....
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Remov duplicates from array

 
0
  #7
Dec 8th, 2008
You don't need to create temp tables to hold the distinct results.

You can just add DISTINCT to your query, for the field that you want to be distinct.

  1. $sql = "SELECT DISTINCT(field), * FROM paid WHERE title LIKE '$alph%' ORDER by asc";

where field is the one you want distinct.

The GROUP BY clause will also serve the same purpose.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC