Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Writing Xml file with php

    Hello. I am programming a game. When game is over the user enters his first and last name. But i have no clue how to do this. It must be done like this : user enters his first and last name and score is automatical. Im not strong in php. Php should write node with information just given and only with the score that is higher than the lowest score.

    I have script that shows usernames and score points. But the main problem is that i want to write 10 top scores in xml file. The ordering is not important. Can anyone help me please?

    Code:
    <?xml version="1.0" ?>
    <list>
    	<name first="James" last="Johnson" gpa="1222" />
    	<name first="John" last="Smith" gpa="1111" />
    	<name first="Serena" last="Williams" gpa="1000" />
    	<name first="Indiana" last="Jones" gpa="999" />
    	<name first="Linda" last="Brown" gpa="888" />
    	<name first="Robert" last="Davis" gpa="777" />
    	<name first="Seno" last="Cular" gpa="666" />
    	<name first="Mike" last="Wilson" gpa="555" />
    	<name first="Mary" last="Moore" gpa="444" />
    	<name first="Will" last="Taylor" gpa="333" />
    </list>

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First read the file to a string:

    $xml = file_get_contents('/path/to/file.xml');

    Then you can manipulate the XML using either string functions, regular expressions or PHP's xml parsers (DOM, SimpleXML etc).

    I think the easiest is the DOM if you're familiar with it.
    http://www.php.net/manual/en/book.dom.php

    simple xml if also fairly easy to use:
    http://us2.php.net/simplexml

    You'll just need to retrieve all the "name" xml nodes and their attribute values into an array. Then sort the array with "sort" or "usort".

    Make sure the new entry is higher then the lowest. Create an XML node for it and insert it into the XML Object.

    Write this back to the file.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi. Thank You. You are very kind for answering. I have this kind of php code:
    Code:
    <?php
    $file_handle = fopen('toplist.xml','a');
    $content = '<?xml version="1.0"?>
    <name>';
    foreach ($result as $row)
    {
    $content.="\n<name first='".$row->mark."' last='".$row->mark."' gpa='".$row->mark."' />\n";
    }
    fwrite($file_handle,$content);
    fclose($file_handle);
    ?>
    How do i send information to php file? and how do i calculate witch is the lowest score? I have to do it fast. I have no time to learn. Maybe someone or you can help me with the code?

    The flash variables are firstname lastname and score
    Last edited by Genero; 05-31-2009 at 06:56 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •