Page 1 of 1

Remove duplicate elements

Posted: Tue Mar 22, 2011 3:07 pm
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.

Re: Remove duplicate elements

Posted: Tue Mar 22, 2011 3:26 pm
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){

Re: Remove duplicate elements

Posted: Tue Mar 22, 2011 3:32 pm
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!