Remove duplicate elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vinnar
Forum Commoner
Posts: 32
Joined: Tue Feb 01, 2011 11:00 am

Remove duplicate elements

Post by Vinnar »

Hay guys,

So please take a look at the whole script first:

Code: Select all

$select5 = "SELECT LS_CELL_TYPE FROM link_specificities

INNER JOIN links ON link_specificities.LS_LINK = links.L_ID

WHERE links.L_ID = $LinkID";

$get5 = mysqli_query($connect, $select5) or die(mysqli_error($connect));

while ($row5 = mysqli_fetch_array($get5)){

$cellType = $row5['LS_CELL_TYPE'];

$select6 = "SELECT DISTINCT CT_NAME FROM cell_type WHERE CT_ID = '$cellType'";

$get6 = mysqli_query($connect, $select6) or die(mysqli_error($connect));

while ($row6 = mysqli_fetch_array($get6)){
$queries = array($row6['CT_NAME']);

}

$query = array_unique($queries);

foreach ($queries as $query){

$check = "select first_name, last_name, email from mailing_list WHERE cell like '%$query%'";

echo nl2br($check.PHP_EOL);

update($check);

}

} 
Now what I want to do is to remove the duplicate strings from the long string

and the output of $query = array_unique($queries); is like this:

Code: Select all

select first_name, last_name, email from mailing_list WHERE cell like '%Not Specified%'

select first_name, last_name, email from mailing_list WHERE cell like '%Not Specified%'

select first_name, last_name, email from mailing_list WHERE cell like '%Neuron%'

select first_name, last_name, email from mailing_list WHERE cell like '%Glia Cell%'

select first_name, last_name, email from mailing_list WHERE cell like '%Neuron%'

select first_name, last_name, email from mailing_list WHERE cell like '%Neuron%'

select first_name, last_name, email from mailing_list WHERE cell like '%Neuron%'

select first_name, last_name, email from mailing_list WHERE cell like '%Neuron%' 


It seems it's not remove any duplicate strings...why is that

Thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Remove duplicate elements

Post by AbraCadaver »

Because you assign the unique values to $query but you loop over $queries. Try:

Code: Select all

$queries = array_unique($queries);

foreach ($queries as $query){
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Vinnar
Forum Commoner
Posts: 32
Joined: Tue Feb 01, 2011 11:00 am

Re: Remove duplicate elements

Post by Vinnar »

Because you assign the unique values to $query but you loop over $queries.
Sorry that was a typo, it was supposed to be

$queries = array_unique($queries);

but now I think the source of the problem may all boil down to this line:

$select6 = "SELECT DISTINCT CT_NAME FROM cell_type WHERE CT_ID = '$cellType'";

Since there are various outputs of variable $cellType

and this select statement goes through every respective $cellType, and its results are selected distinctively

No wonder the result is still

Not SpecifiedNot SpecifiedNeuronGlia CellNeuronNeuronNeuronNeuron

So I think I would need to put "SELECT DISTINCT" on top this select statement so only DISTINCTIVE strings from this long string are chosen

I want to do this all in one sql command, but not sure about the approach

Please help!
Post Reply