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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:02:28+00:00 2026-06-18T23:02:28+00:00

I’m creating a game where when a timer ends the user can enter their

  • 0

I’m creating a game where when a timer ends the user can enter their name. I pass the name and score to a PHP file with AJAX. That PHP file adds it to an XML file. I loop through it to create a table holding the scores and it’s then returned to AJAX and I then output it on the screen with jQuery. I have all of this working fine right now.

What I want to accomplish is this:
1. After the score is added to the XML file I want to order the nodes according to score, in descending order
2. I then want to populate the table with the values in order. I’d also like to limit it to only the top 10 scores.

Basically where I’m running into problems coming up with a solution is the ordering. Once the XML is ordered populating the table and limiting it to 10 should be pretty straight forward. Any suggestions on how I should do this?

XML : http://people.rit.edu/lxl1500/Prog4/Project%202/scoreboard.xml

jQuery Ajax call:

function addScore(score,name){
    var url = 'scoreboard.php';
    $.post(url,{Score:score, Name:name},function(data){
    $('#score').html(data).show();
   });
}

scoreboard.php:

<?php
$score = $_POST['Score'];
$name = $_POST['Name'];

if($name == ''){
    $name = 'Player'.rand(0,5000);
}

$scoreboard = new domDocument;
$scoreboard->load('scoreboard.xml');

$root=$scoreboard->documentElement;

$entry = $scoreboard->createElement('entry');

$userScore = $scoreboard->createElement('score',$score);

$userName = $scoreboard->createElement('name',$name);

$entry->appendChild($userName);
$entry->appendChild($userScore);

$root->appendChild($entry);

$scoreboard->save('scoreboard.xml');

$scores = $scoreboard->getElementsByTagName('entry');

$string = '<table id="score-table" cellspacing="10"><tbody><tr><th align="left">User</th><th align="left">Score</th></tr>';


foreach($scores as $score){
    $getScore = $score->getElementsByTagName('score')->item(0)->nodeValue;
    $getPlayer = $score->getElementsByTagName('name')->item(0)->nodeValue;
    $string.="<tr><td>$getPlayer</td><td>$getScore</td></tr>";
}

$string.='</tbody></table>';

echo $string;

?>

Any help would be greatly appreciated! Thanks.

  • 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-18T23:02:29+00:00Added an answer on June 18, 2026 at 11:02 pm

    You can build a sorted XML file, that is add the nodes to the xml file in sorted order, something like

    $entries = $root->getElementsByTagName('entry');
    
    $added = false;
    foreach ($entries as $item) {
        if ($score <= $item->getElementsByTagName('score')->item(0)->nodeValue) continue;
        $root->insertBefore($entry, $item);
        $added = true;
        break;
    }
    
    // if not yet added, add it
    if (!$added) {
        $root->appendChild($entry);
    }
    

    For this to work the file has to be sorted (or empty).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.