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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:13:22+00:00 2026-06-12T20:13:22+00:00

I have a script that grabs an xml of my database generated by php

  • 0

I have a script that grabs an xml of my database generated by php and I would like to shuffle the rows before the php script echos the xml so that each time i access the database xml file, I’ll receive the database in a different order.

here’s part of my php script that outputs the xml:

$dom =  new DOMDocument("1.0");
$node = $dom->createElement("database");
$parnode = $dom->appendChild($node); 

$query = "SELECT * FROM database WHERE 1";
$result = mysql_query($query);

header("Content-type: text/xml");


while ($row = @mysql_fetch_assoc($result)){
    // ADD TO XML DOCUMENT NODE
    $node = dom->createElement("data");
    $newnode = $parnode->appendChild($node);

    $newnode->setAttribute("id", $row['id']);
    $newnode->setAttribute("name", $row['name']);
    $newnode->setAttribute("date", $row['date']);
    $newnode->setAttribute("latitude", $row['latitude']);
    $newnode->setattribute("longitude", $row['longitude']);

}

This is where I’d like to randomize the xml output if possible. This seems like the most logical place, but if there’s a better place, that’s fine with me.

echo $dom->saveXML();

Here’s a sample of my xml:

<database>
  <data id="1" name="blah" date="2012-10-10" latitude="0" longitude="0"/>
  <data id="3" name="blah" date="2012-10-10" latitude="0" longitude="0"/>
  <data id="4" name="blah" date="2012-10-10" latitude="0" longitude="0"/>
</database>

Simply put, I would like the xml rows to be in a different order each time i access it. Thanks for your help.

  • 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-06-12T20:13:24+00:00Added an answer on June 12, 2026 at 8:13 pm

    One way around this problem is to use a temporary array to store the rows fetched from DB, then shuffle this array, then walk through it:

    $records = array();
    while ($row = @mysql_fetch_assoc($result)){
        $records[] = $row;
    }
    
    shuffle($records);
    
    foreach ($records as $row) {
        $node = dom->createElement("data");
        $newnode = $parnode->appendChild($node);
    
        $newnode->setAttribute("id", $row['id']);
        ...
    }
    

    The other way is use the original array as it is, but randomize the insertion process instead:

    $prevnode = null;
    while ($row = @mysql_fetch_assoc($result)){
        $node = dom->createElement("data");
        if ($prevnode && rand(0, 1) === 0) {
          $newnode = $prevnode->insertBefore($node);
        } else {
          $newnode  = $parnode->appendChild($node);
        }
        $prevnode = $newnode;
    
        $newnode->setAttribute("id", $row['id']);
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that grabs links from another website. I am storing
I have a PHP script that grabs data from an external service and saves
I have a PHP script that connects to an API and grabs around 200
I have a simple little script that grabs XML and converts it to CSV.
I have a script that grabs data from a MySQL database table and creates
I have a script that grabs the 'id' of a clicked 'div' and uses
I have a script that runs a stored procedure in my SQL server database,
i have a script in jquery (that grabs a value from a select field
I have a working perl script that grabs the data I need and displays
Basically I have a script in Python that grabs the text from an open

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.