My question is related to an answered question that was asked here on the ever reliable forum - Daniweb.

I have been trying to insert the parsed data as a string into mysql table in vain.

This is what i am trying to do.....

From the solution in the link above, i changed the code to this:

# loop each date > item > item
        foreach($item as $elem)
            $r= sprintf('%s %s %s'
                         , $elem->attributes()->Name
                         , $elem->attributes()->Type
                         , $elem);
}

I changed $r[] to $r. This ensured that i get individual arrays from the Item nodes. However, when i try to insert $r into Mysql table, each individual element of $r is inserted as a unique row see table below for the data structure:

+----+----------------------------------+
| id | dates                            |
+---------------------------------------+
|  1 |  received Date 2015/11/08 00:00  |
|  2 |  accepted Date 2016/04/16 00:00  | 
|  3 |  entrez Date 2016/08/31 06:00    | 
|  4 |  pubmed Date 2016/08/31 06:00    |
|  5 |  medline Date 2016/08/31 06:00   |
|  6 |  epublish Date 2016/05/23 00:00  |
|    |                                  |
|    |                                  | 
|    |                                  | 
+---------------------------------------+

What, i am trying to get is this data structure. see below:

+----+-------------------------------------------------------------------------------------------------+
| id | dates                                                                                           |
+------------------------------------------------------------------------------------------------------+
|  1 |  received Date 2015/11/08 00:00, accepted Date 2016/04/16 00:00, entrez Date 2016/08/31 06:00    |  
+-------------------------------------------------------------------------------------------------------+

When i

echo $r

i get this result(see below), but i am not able to get it into a table as a string. What am i doing wrong?

received Date 2015/11/08 00:00, accepted Date 2016/04/16 00:00, entrez Date 2016/08/31 06:00, pubmed Date 2016/08/31 06:00, medline Date 2016/08/31 06:00, epublish Date 2016/05/23 00:00

Hello,

so, standing at the original example you want two rows for each date > item, correct?

Use implode() after each cycle, change this:

# loop each date > item
foreach($items as $item)

    # loop each date > item > item
    foreach($item as $elem)
        $r[] = sprintf('%s %s %s'
                     , $elem->attributes()->Name
                     , $elem->attributes()->Type
                     , $elem);

to:

# loop each date > item
foreach($items as $item)
{
    # initialize the $r array at each loop
    $r = [];

    # loop each date > item > item
    foreach($item as $elem)
    {
        $r[] = sprintf('%s %s %s'
                     , $elem->attributes()->Name
                     , $elem->attributes()->Type
                     , $elem);
    }

    # convert the $r array to a string
    $s[] = implode(',', $r);
}

The contents of $s will look like this:

array (2) [
    string (184) "received Date 2015/12/18 00:00,accepted Date 2016/03/31 00:00,aheadofprint Date 2016/04/14 00:00,entrez Date 2016/04/15 06:00,pubmed Date 2016/04/15 06:00,medline Date 2016/04/15 06:00"
    string (152) "epublish Date 2016/05/23 00:00,entrez Date 2016/07/12 06:00,pubmed Date 2016/07/12 06:00,medline Date 2016/07/12 06:00,pmc-release Date 2017/01/01 00:00"
]

If you want them all in the same string, then just initialize the $r array before or the first loop (# loop each <date> node) and move the implode out of the loops, at the end, basically:

$r = [];

# loops

$s = implode(',', $r);

I am not sure you want to concatenate either certain element values in the array OR all element values in the array? If the latter, you could use implode() function. If the former, why don't you simply construct a string as value for sql from each element atrribute you want (e.g. Name, Type, etc.)?

Thank you @Cereal and @Taywin for the thoughtful and useful comments. The implode()option has given me what i wanted.

commented: You're welcome! +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.