php loop or nested loop?

Hi Experts,

Firstly - your assistance is much appreciated.

Basically this is what I would like to accomplish.  Say I have 675 items I have found in a query and output like this:

<msg><recipient>0445111111</recipient><text>THIS IS A TEST</text><date>01/07/2015></date><user>test</user></msg>
<msg><recipient>0446111111</recipient><text>THIS IS A TEST</text><date>01/07/2015></date><user>test</user></msg>

rather than outputting like this, I would like to putput in this method:
<msg><user>test</user><date></date><groupmsg>[{“RECIPIENT”:”61435800957”, “MESSAGE_TEXT”:”Message 1”, “REFERENCE”:”unique01”},
{“RECIPIENT”:”61406040271”, “MESSAGE_TEXT”:“Message 2”, “REFERENCE”:”unique02”}]</msg>

so basically - for up to every 100 items, it would be enclosed on the one line, so for 675 items found I would expect 7 lines.

if anyone could help it would be appreciated!!
mavmanauNetwork Engineer/SysadminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
can you post the scripts of your looping? basically we need to set a counter, if it reached 100, we need add tag "<groupmsg>" into the output.
0
mavmanauNetwork Engineer/SysadminAuthor Commented:
Hi,

What I was thinking of doing - was feeding the whole lot of the XML to a new page and doing a serialise xml to loop through the records. As the source application is proprietary software, would this work?

ie HTTP post a variable XMLDATA to a new page,  Then loop through that file at every 100 it creates a new tag (unless of course it finishes running prior to hitting 100)
0
Ryan ChongCommented:
>>What I was thinking of doing - was feeding the whole lot of the XML to a new page and doing a serialise xml to loop through the records. As the source application is proprietary software, would this work?
so is that mean the proprietary source application is generating a XML data source and you wish to load it with customization display output? I think this is feasible as I don't foresee any issues if the source contains the info we wanted. What we need is to customize the output accordingly.

you can post your existing scripts so we can see where we can improve it.
0
Ray PaseurCommented:
It sounds like there is a "backstory" here.  Perhaps this information is in a database and is being recovered with a query?  If we knew the origin and true contents of the information it would be easier to help you.  But we are missing so much of the picture!

In the original post, there is no one-to-one correspondence between the first output group and the second output group.  For example, “RECIPIENT”:”61435800957 -- where does this come from?  When we have only fragments of data like this, it's impossible to write even a simple test case.

It also looks like the second output group contains a mixture of XML and JSON.  That is a recipe for confusion.  You should choose one or the other, but not both.  From my experience I would advise JSON instead of XML.  JSON is "lighter" and more capable of representing different data types.  For more information, please see: http://json.org

Please refer to the SSCCE, and then post back with a true copy of the inputs you have and a true copy of the outputs you want.  We don't want you to post proprietary information or passwords, but please be careful that as you obscure these fields, we are still able to follow the data elements from the inputs to the outputs in a way that is unambiguous.

If you're new to PHP and want to learn the language, this article can help you find dependable learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
To expand on the posts above.

It looks like you are wanting to group all your rows by user and date- so you have one <msg></msg> element for each unique user / date combination and then inside this you want to create a JSON string containing an array of the Recipient / Message data that is attributed to that user and that date.

As mentioned above - why not make the output all JSON?

Your intended output XML is incorrect you are missing a closing </groupmsg>

What you are wanting to do is very straight forward - you need to do something like this

// AN ARRAY TO STORE THE GROUPED DATA
$data = array();

// LOOP THROUGH RESULTS IN YOUR QUERY
foreach($query_result as $row) {

  // DO WE ALREADY HAVE AN ELEMENT FOR THIS USER
  // IF NOT, CREATE ONE
  if (!isset($data[$row['user']])) {
    $data[$row['user']] = array();
  }

  // DO WE ALREADY HAVE A AN ELEMENT FOR THIS USER ON THIS DATE
  // IF NOT, CREATE ONE
  if (!isset($data[$row['user']][$row['date']])) {
    $data[$row['user']][$row['date']] = array();
  }

  // USER DATE ELEMENT EXISTS SO ADD THE MESSAGE DATA TO THAT 
  // ARRAY ELEMENT
  $data[$row['user']][$row['date']][] = 
      array (
        'RECIPIENT' => $row['recipient'],
        'MESSAGE_TEXT' => $row['message'],
        'REFERENCE' => $row['reference']
      );
}
// DATA IS NOW GROUPED BY USER AND DATE
// THERE ARE A VARIETY OF OPTIONS FOR OUTPUT.

Open in new window


What is not clear (in addition to the sample data not tying up) is what the 100 records have to do with anything - do you mean that your data tends to have around 100 records per user / date?

To finish the code we would need to know some of the answers to the questions posted above
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.