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 3485106
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:52:04+00:00 2026-05-18T10:52:04+00:00

Problem: I want to get all locations from a database and encase them all

  • 0

Problem: I want to get all locations from a database and encase them all in the <loc> tag, the following code puts each location in a <loc> tag, leading to several <loc> when I only need one. I know it’s doing this because it’s inside a loop (the simpleXML part), but have no idea how to solve it.

    if($r7){ //$r7 = If query was successfull..     
       while($row = mysqli_fetch_array($r7, MYSQLI_ASSOC)){
        $convXML_from_loc = $convXML_from->addChild('loc',$row['location']);
       }
    }

If I take it out of the loop, it just puts the first location in the database there (iirc).

The alternative to this is just echo "<xml>"; which I thought was bad practice, because nothing would have parent and child elements, and everything would be on the same level.

I would appreciate any guidance on this issue, as well as links to any relevant information on this subject.

Regards.

EDIT: If it was unclear, I need to put them all within a single loc tag, like <loc>Row 1, Row 2</loc>. At the moment it’s giving <loc>Row 1</loc><loc>Row 2</loc>.

  • 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-18T10:52:05+00:00Added an answer on May 18, 2026 at 10:52 am

    You have a while loop that iterates over every row. You’re going to do whatever is in the loop for as many times as there are rows. So you will be making as many <loc> elements as there are rows, one for each row.

    The solution is to generate a string that contains all of the row data inside of the while loop, and then add the <loc> element with that string outside of the loop. This causes addChild() to be called only one time, thus creating only one <loc> element.

    $location_data;
    
    if($r7){ //$r7 = If query was successfull..     
        while($row = mysqli_fetch_array($r7, MYSQLI_ASSOC)){
            $location_data .= $row['location'] . ' ,';
        }
    }
    
    $location_data = substr($location_data, 0, -2); // strip off the final space and comma
    $convXML_from_loc = $convXML_from->addChild('loc',$location_data); // will result in '<loc>Row 1, Row 2</loc>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one problem , I want to get some data from XML file
Problem: I want to list n number of games from each genre (order not
I want to get data using CURL but I have a problem. When I
i have a problem. i want to get info about my string have other
This seems to be a recurring problem to me. I want to get started
Problem: I want disparate sections of my code to be able to access a
I have javascript function mixed with ASP MVC Razor that get data from database
I am working with django-mptt and I'm stuck. I want to get all objects
I'm using the following code to export all the emails in a specific gmail
So, this is my problem. I want to be able to add onevent's from

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.