RSS Forums RSS

Remov duplicates from array

Reply
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

  #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:
<?php if(isset($_GET['alph'])){
$alph = $_GET['alph'];
$sql = "SELECT * FROM paid WHERE title LIKE '$alph%' ORDER by asc";
$query = mysql_query($sql);
while($data = mysql_fetch_array($query)){
$desc = substr($data['description'],0,100);
echo "<tr><td valign=\"top\">".$data['title']." ".$data['description']."</tr></td>";
}}
?>
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!!
AddThis Social Bookmark Button
Reply With Quote  
Posts: 1,037
Reputation: ShawnCplus is a jewel in the rough ShawnCplus is a jewel in the rough ShawnCplus is a jewel in the rough 
Solved Threads: 155
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: Remov duplicates from array

  #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  
Posts: 278
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

  #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
CREATE TABLE new_table AS
SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];

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

Step 3: rename the new_table to the name of the old_table
RENAME TABLE new_table TO old_table;
Reply With Quote  
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

  #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  
Posts: 278
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

  #5  
Dec 5th, 2008
then you do like this...example
CREATE TABLE table2 (
id INT NOT NULL UNIQUE AUTO_INCREMENT,
name VARCHAR(20) NOT NULL 
);

INSERT INTO table2(id,name) VALUES
(1,'Things Fall Apart'),
(2,'Things Fall Apart'),
(3,'The Famished Road'),
(4,'Things Fall Apart'),
(5,'The Famished Road'),
(6,'Thirteen cents'),
(7,'Thirteen cents');

CREATE  TABLE 
temp2(id VARCHAR(10), name VARCHAR(20))
TYPE=HEAP;

INSERT INTO temp2(name) SELECT DISTINCT name FROM table2;

DELETE FROM table2;

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 8:05 am.
Reply With Quote  
Posts: 278
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

  #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  
Posts: 905
Reputation: digital-ether is a jewel in the rough digital-ether is a jewel in the rough digital-ether is a jewel in the rough digital-ether is a jewel in the rough 
Solved Threads: 57
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Posting Shark

Re: Remov duplicates from array

  #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.

$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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the PHP Forum
Views: 568 | Replies: 6 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:42 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC