SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PhP guru's ---> Php to xml

    Hello there

    I am trying to get a simple guestbook working with flash (front-end) and php/xml (back-end).

    First i got a simple php-script that writes data to guest.db file and the data looks like:
    barry||barry%40fula.nl||Aug%2024||2001||19%3A58||Hi%20there

    The php-script i use looks like this:
    <?PHP
    if ($command=="view"){
    $info=file("guest.db");
    $max=sizeof($info);
    for ($i=0;$i<$max;++$i){
    $temp=explode("||",$info[$i]);
    echo("&name".($i+1)."=".$temp[0].
    "&email".($i+1)."=".$temp[1].
    "&date".($i+1)."=".$temp[2].
    "&year".($i+1)."=".$temp[3].
    "&time".($i+1)."=".$temp[4].
    "&message".($i+1)."=".$temp[5]);
    }
    echo("&max=".$max);
    }
    if ($command=="sign"){
    if ($name!="" && $message!=""){
    $fp=fopen("guest.db","a");
    fputs($fp, rawurlencode(StripSlashes($name))."||".
    rawurlencode(StripSlashes($email))."||".
    rawurlencode(date("M d"))."||".
    rawurlencode(date("Y"))."||".
    rawurlencode(date("H:i"))."||".
    rawurlencode(StripSlashes($message))."\n");
    fclose($fp);
    }
    echo("done=1");
    }
    ?>


    Now i want to replace the guest.db with guest.xml. My xml doc has the following structure:

    <?xml version="1.0" standalone="yes" ?>
    <Guestbook>
    <Message Name="" Email="" Date="" Time="" Words=""/>
    </Guestbook>

    I think i must replace the guest.db file in the php-script with guest.xml, right?
    But how do i rewrite the php-script so that it has the xml doc as output??

    Long story i know,
    Greetz
    D-flyer

  2. #2
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am a little bit closer to the solution. I use the following php:

    <?PHP

    if ($command=="sign"){

    $info=file("guest.xml");

    if ($naam!="" && $words!=""){
    /* open.xml file en schrijf naar het einde v/h bestand*/
    $fp=fopen("guest.xml","a");

    $tofile.="<"."?"."xml version=\"1.0\""."?".">\n";
    $tofile.="<guestbook>\n";


    $tofile.="\t<message>\n";
    $tofile.="\t\t<naam>".$naam."</naam>\n";
    $tofile.="\t\t<email>".$email."</email>\n";
    $tofile.="\t\t<words>".$words."</words>\n";
    $tofile.="\t</message>\n";


    $tofile.="</guestbook>\n";

    fwrite($fp,$tofile);
    fclose($fp);

    }
    echo("done=1");
    }
    ?>


    If my guestbook is clean (no entries) than this PHP generates a valid .xml file. But with a second entry i get a invalid .xml file.

    How do i enable this php script so that it doesn't write the xml-declaration and the <guestbook> tags with other entries?

    Grtz,
    D-flyer

  3. #3
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should probably use PHP's XML support (http://php.net/manual/en/ref.xml.php) instead of parsing it with regexps.

  4. #4
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just have it add the lines in the middle, and have it write to an xml file that you already wrote the declaration in.

    PHP Code:
    <?PHP 

    if ($command=="sign"){ 

    $info=file("guest.xml"); 

    if (
    $naam!="" && $words!=""){ 
    /* open.xml file en schrijf naar het einde v/h bestand*/ 
    $fp=fopen("guest.xml","a");
    $tofile.="\t<guestbook>\n"
    $tofile.="\t<message>\n"
    $tofile.="\t\t<naam>".$naam."</naam>\n"
    $tofile.="\t\t<email>".$email."</email>\n"
    $tofile.="\t\t<words>".$words."</words>\n"
    $tofile.="\t</message>\n"
    $tofile.="\t</guestbook>\n"
    fwrite($fp,$tofile); 
    fclose($fp); 


    echo(
    "done=1"); 

    ?>
    And then go and write yourself an xml document with the declaration.

  5. #5
    Your daddy. WALoeIII's Avatar
    Join Date
    Apr 2001
    Location
    USA
    Posts
    526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    does that work? or do you have to insert the new data between the guestbook tags, and may only have them once.

  6. #6
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Snoozy,
    Indeed i only (can) use the <guestbook> tags once. So what is a way to accomplish this?
    Last edited by D-flyer; Aug 27, 2001 at 05:25.

  7. #7
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    qslack --> I am going to parse .xml thru flash 5, but thx for the linkage.

    I know i have to use it someday


Bookmarks

Posting Permissions

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