EXPLODE problem

Hey guys

So my question is related to explode()

Let me illustrate this with the codes (excerpt) first:


foreach ($query as $querys){
echo $querys;
$querys= explode("null",$querys);
var_dump($querys);
$check = "select first_name, last_name, email from mailing_list WHERE cell like '%$querys%'";

}
}

Output of “echo $querys” is:

Not SpecifiedNot SpecifiedNeuronGlia CellNeuronNeuronNeuronNeuron

As you can see this is one long string, and I only need the Glia Cell from this since it matches the data from the table

Then the output for var_dump($querys) is

array(1) { [0]=> string(13) "Not Specified" } array(1) { [0]=> string(13) "Not Specified" } array(1) { [0]=> string(6) "Neuron" } array(1) { [0]=> string(9) "Glia Cell" } array(1) { [0]=> string(6) "Neuron" } array(1) { [0]=> string(6) "Neuron" } array(1) { [0]=> string(6) "Neuron" } array(1) { [0]=> string(6) "Neuron" }

Why are the keys all [0]? Shouldn’t the keys start with [0] then go all way up?

And as you could expect, the sql command couldn’t go through … so how could I fix it up so only the specific string could be chosen?

By the way, I used “null” as delimiter for explode() since there’s no space in between the long string, but is this correct?

Thanks :slight_smile:

Since the text “null” appears nowhere in your string the explode is not going to change anything except to convert $querys from a string into an array where the string is in element [0].

$query appears to be an array containing scalar strings:


<?php
$query=array('Not Specified','Not Specified','Neuron','Glia Cell','Neuron','Neuron','Neuron','Neuron');
foreach ($query as $querys){
$check = "select first_name, last_name, email from mailing_list WHERE cell like '%$querys%'";
echo nl2br($check.PHP_EOL);
}

If you want “Glia Cell” from the array, you’d reference it as echo $query[3];

You aren’t working with one long-string, that behavior can be explained quite simply like so:


<?php
$query=array('Not Specified','Not Specified','Neuron','Glia Cell','Neuron','Neuron','Neuron','Neuron');
foreach($query as $q){echo $q;}
echo "<br />\\r\
";
foreach($query as $q){$q=explode('poopoo',$q);var_dump($q);}

All the best,

Ok so I kind of getting it

I believe I have to fix up 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!

The whole script:

$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);

}

} 

Note that I delete the explode( ) and simply the foreach loop

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

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

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.

My MySQL JOIN logic isn’t the best when it comes to writting queries to degrade gracefully when NULL values are detected in one of the SQL columns/keys etc…

This query should do more or less what you want so far: (assuming I’ve understood you correctly)


<?php
$LinkID=5;//dummy input

$sql="SELECT DISTINCT CT_NAME FROM cell_type ct LEFT JOIN link_specificities ls ON ct.CT_ID=ls.LS_CELL_TYPE INNER JOIN links ON ls.LS_LINK = links.L_ID WHERE links.L_ID = '%s'";
$query=sprintf($sql,mysqli_real_escape_string($connect,$LinkID));
echo $query;
$result=mysqli_query($connect, $query) or die(mysqli_error($connect));
$results=array();
while(list($ct_name)=mysqli_fetch_row($result)){$results[]=$ct_name;}
echo '<pre>',print_r($results,true),'</pre>';
?>

Then you’d loop through the results with your mailing list query…

Depending on your situation you could probably even setup another JOIN query to interact with the mailing_list table so your PHP script can email people easier. Although I don’t follow your setup well enough to code it any better.

MANY THANKS, Ultimater

It works perfectly fine now and the distinctive elements can be chosen finally

guess one good sql command sometimes is all it needs

Again, thanks for your awesome help !!! :smiley: