SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: EXPLODE problem

  1. #1
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    EXPLODE problem

    Hey guys

    So my question is related to explode()

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

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

    }

    Output of "echo $querys" is:

    PHP Code:
    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

    PHP Code:
    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

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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].
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast Ultimater's Avatar
    Join Date
    Jun 2005
    Location
    Los Angeles
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $query appears to be an array containing scalar strings:
    PHP Code:
    <?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 Code:
    <?php
    $query
    =array('Not Specified','Not Specified','Neuron','Glia Cell','Neuron','Neuron','Neuron','Neuron');
    foreach(
    $query as $q){echo $q;}
    echo 
    "<br />\r\n";
    foreach(
    $query as $q){$q=explode('poopoo',$q);var_dump($q);}
    All the best,
    Ultimater

    GuestBook: View Sign

  4. #4
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The whole script:

    PHP Code:
    $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:


    PHP Code:
    select first_namelast_nameemail from mailing_list WHERE cell like '%Not Specified%'
    select first_namelast_nameemail from mailing_list WHERE cell like '%Not Specified%'
    select first_namelast_nameemail from mailing_list WHERE cell like '%Neuron%'
    select first_namelast_nameemail from mailing_list WHERE cell like '%Glia Cell%'
    select first_namelast_nameemail from mailing_list WHERE cell like '%Neuron%'
    select first_namelast_nameemail from mailing_list WHERE cell like '%Neuron%'
    select first_namelast_nameemail from mailing_list WHERE cell like '%Neuron%'
    select first_namelast_nameemail from mailing_list WHERE cell like '%Neuron%' 

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

    Thanks.

  5. #5
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  6. #6
    SitePoint Enthusiast Ultimater's Avatar
    Join Date
    Jun 2005
    Location
    Los Angeles
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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.
    Ultimater

    GuestBook: View Sign

  7. #7
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 !!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •