| | |
Remov duplicates from array
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
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:
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!!
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 Syntax (Toggle Plain Text)
<?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>"; }} ?>
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!!
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.
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.
php Syntax (Toggle Plain Text)
$entries = array(); while($data = mysql_fetch_array($query)) { $entries[]=$data['title']; if(!in_array($data['title'], $entries)) { // Your code here } }
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.
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.
•
•
Join Date: Apr 2008
Posts: 293
Reputation:
Solved Threads: 11
There is way to remove duplicate data or row like...
Step 1: Move the non duplicates (unique tuples) into a temporary table
Step 2: delete delete the old table
Step 3: rename the new_table to the name of the old_table
Step 1: Move the non duplicates (unique tuples) into a temporary table
PHP Syntax (Toggle Plain Text)
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
PHP Syntax (Toggle Plain Text)
DROP TABLE old_table;
Step 3: rename the new_table to the name of the old_table
PHP Syntax (Toggle Plain Text)
RENAME TABLE new_table TO old_table;
•
•
Join Date: Jun 2008
Posts: 6
Reputation:
Solved Threads: 0
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
Thanks ShawnCplus, but when I tryed your surgestgion I didn't get anything to display.
So still trying. Thanks though guys
•
•
Join Date: Apr 2008
Posts: 293
Reputation:
Solved Threads: 11
then you do like this...example
you can place your data in temp table..and display main table without duplication of data by using DISTINCT .....
PHP Syntax (Toggle Plain Text)
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 9:05 am.
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.
where field is the one you want distinct.
The GROUP BY clause will also serve the same purpose.
You can just add DISTINCT to your query, for the field that you want to be distinct.
PHP Syntax (Toggle Plain Text)
$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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Other Threads in the PHP Forum
- Previous Thread: Sms facility
- Next Thread: sql query string logical problem
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dropdown dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






