Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:06:59+00:00 2026-05-28T08:06:59+00:00

Could someone please help me. I’m trying to create an xml list with all

  • 0

Could someone please help me. I’m trying to create an xml list with all the events ordered by date. I’m doing this by using a php script. Right now I have this:

$sql = "SELECT DATE_FORMAT(K.kalender_datum,'%W %d %M %Y') as afhaaldag, A.afval_naam
        FROM tblkalender K
        INNER JOIN tblafval A
        ON A.pk_afval_Id = K.fk_afval_Id
        WHERE kalender_datum > CURDATE()
        ORDER BY K.kalender_datum";
$query = mysql_query($sql);

$last_date = "";
$tag_open = false;

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml .= "<kalender>\n";

for($i = 0; $i<mysql_num_rows($query); $i++)
{   
    $itemrow = mysql_fetch_assoc($query);

    if($last_date != $itemrow['kalender_datum'])
    {
        //als de laatste datum die gebruikt is niet gelijk is aan de datum die hij binnen krijgt, moet hij een nieuwe afhaaldag beginnen
        if($tag_open)
        {
            $xml .= "\t</afhaaldag>\n";
            $tag_open = false;
        }
        $xml .= "\t<afhaaldag>\n";
        $xml .= "\t<datum>" . $itemrow['kalender_datum'] . "</datum>\n";
        $xml .= "\t<afval>" . $itemrow['afval_naam'] . "</afval>\n";    
        $last_date = $itemrow['kalender_datum'];
    }   

    else
    {
        $xml .= "\t<afval>" . $itemrow['afval_naam'] . "</afval>\n";
        $last_date = $itemrow['kalender_datum'];
        $tag_open = true;
    }   
}
$xml .= "</kalender>";
echo $xml;

That gives me this output in xml:

<kalender>
   <afval>Grofvuil</afval>
   <afval>Restafval</afval>
   <afval>GFT</afval>
   <afval>PMD</afval>
   <afval>Snoeiresten</afval>
   <afval>GFT</afval>
   <afval>Restafval</afval>
   <afval>GFT</afval>
   <afval>PMD</afval>
   <afval>GFT</afval>
   <afval>Snoeiresten</afval>
   <afval>Papier en karton</afval>
   <afval>Grofvuil</afval>
   <afval>Restafval</afval>
   <afval>GFT</afval>
   <afval>PMD</afval>
</kalender>

But what I need is this:

<kalender>
   <afhaaldag>
      <datum>2012-01-02</datum>
      <afval>GFT</afval>
      <afval>Restafval</afval>
      <afval>PMD</afval>
   </afhaaldag>
   <afhaaldag>
      <datum>2012-01-17</datum>
      <afval>Papier en karton</afval>
   </afhaaldag>
   <afhaaldag>
      <datum>2012-01-23</datum>
      <afval>GFT</afval>
   </afhaaldag>
</kalender>

Does someone know how I can do it. I know I need some sort of loop (I’m guessing a for loop). Could someone please help me out on this one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:07:00+00:00Added an answer on May 28, 2026 at 8:07 am

    I’m using python here, but you can follow the code without problems:

    kalender = [
    ('2012-01-02', 'GFT'),
    ('2012-01-02', 'Restafval'),
    ('2012-01-02', 'PMD'),
    ('2012-01-17', 'Papier en karton'),
    ('2012-01-23', 'GFT')
    ] 
    
    last_date = ""
    tag_open = False
    
    xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    xml += "<kalender>\n"
    
    
    for i in kalender:
        # i[0] == datum
        # i[1] == afval
    
        # i == $itemrow in your code
    
        datum = i[0]
        afval = i[1]
    
        if last_date != datum:
            if tag_open:
                xml += "\t</afhaaldag>\n"
                tag_open = False
    
            xml += "\t<afhaaldag>\n"
            xml += "\t<datum>" + datum + "</datum>\n"
            xml += "\t<afval>" + afval +  "</afval>\n"
            last_date = datum
                tag_open = True
        else:
            xml += "\t<afval>" + afval + "</afval>\n"
            last_date = datum
            tag_open = True
    xml += "\t</afhaaldag>\n"
    xml += "</kalender>"
    print xml
    

    And this is the output:

    <?xml version="1.0" encoding="UTF-8"?>
    <kalender>
        <afhaaldag>
        <datum>2012-01-02</datum>
        <afval>GFT</afval>
        <afval>Restafval</afval>
        <afval>PMD</afval>
        </afhaaldag>
        <afhaaldag>
        <datum>2012-01-17</datum>
        <afval>Papier en karton</afval>
        </afhaaldag>
        <afhaaldag>
        <datum>2012-01-23</datum>
        <afval>GFT</afval>
        </afhaaldag>
    </kalender>
    

    [Finished]

    The output is exact your are looking for, does it? Try to check again, because in your new output the datum tag isnt’ there, so that’s wierd.

    Notice I add two extra lines:

    tag_open = True  ##(in the True part of the If Statement)
    xml += "\t</afhaaldag>\n" ###Before ending the kalender tag.
    

    and remove the if statement in the else clause.

    Code: http://dl.dropbox.com/u/3389104/kal.py

    ——-End of Edit

    My comment (in code):

    $last_date = "";
    $tag_open = false;
    
    for($i = 0; $i<mysql_num_rows($query); $i++)
    {   
        $itemrow = mysql_fetch_assoc($query);   
    
        if ($last_date != $itemrow['kalender_datum'])  
        {
           //New Entry
           if (tag_open)
           {
               $xml .= "\t</afhaaldag>\n";
               tag_open = false;
           }
           $xml .= "\t<afhaaldag>\n";
           $xml .= "\t<datum>" . $itemrow['kalender_datum'] . "</datum>\n";
           $xml .= "\t<afval>" . $itemrow['afval_naam'] . "</afval>\n";
           $xml .= "\t</afhaaldag>\n";
    
           $last_date = $itemrow['kalender_datum']
        }
        else
        {
           if not (tag_open)
           {
               $xml .= "\t<afhaaldag>\n";
               tag_open = true;
           }
           $xml .= "\t<afval>" . $itemrow['afval_naam'] . "</afval>\n";
        }
    }
    

    Excuse some typos, i don’t have a pc with php right now. This code is my general idea, i dunno it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please help explain why I can't get this to work? I properly
I've recently written this with help from SO. Now could someone please tell me
Could someone please help me with loading maps using animation? I am trying to
Could someone please help me with this? I need to convert an nchar column
Could someone please help solve this problem?
could someone please help me out with the (simplified) code below. I'm trying to
Could someone please help me how to read the XML Document below using Linq
Could someone please help with the following jQuery I need all jQuery UI buttons
Could someone please help me with the regexp javascript code to replace all <br
could someone please help me with this warning. I get this on my console

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.