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 🙂

    Vinnar;10976672 wrote:

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

    how do you expect explode to work then if there nothing to explode on?

    i tested your code and got an empty array as i expected, i cant see how your output is possible based on the code posted.

    how do determine what part of the string it is you want?

    there must be an easier approach to this in the fist place , can you give us the overall picture?

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

        $query = array_unique($queries);

        $queries is an array of one value, so array_unique() wont do anything

          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!

            Write a Reply...