PHP to XML

Discussion in 'PHP' started by smudger, Apr 3, 2008.

  1. #1
    Help !

    i need a php script to query my database and produce an XML file with the following format

    <?xml version="1.0" encoding="utf-8"?>
    <gallery name="Gallery">
    <picture name="picture1" description="test 1 " thumb="thumbs/1.jpg">images/1.jpg</picture>
    <picture name="picture2" description="test xyz " thumb="thumbs/xyz.jpg">images/xyz.jpg</picture>
    </gallery>


    i will have a MySQL database holding the image names, decriptions and paths

    has anybody done something like this before ?


    thanks


    Nigel
     
    smudger, Apr 3, 2008 IP
  2. vishnups

    vishnups Banned

    Messages:
    166
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I have done it numerous times :)
     
    vishnups, Apr 3, 2008 IP
  3. Dade72

    Dade72 Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you mean something like this?

    function getXml() {
    $this->connect();
    $iRes = mysql_query($this->sSql);
    if(!mysql_num_rows($iRes)) {
    return "EMPTY";
    } else {
    $iNumFields = mysql_num_fields($iRes);
    $iNumRes = mysql_num_rows($iRes);

    $sRet = "<?xml version=\"1.0\"?>\n";
    $sRet .= "<RESULTSET COUNT=\"$iNumRes\">\n";

    while($iRow = mysql_fetch_array($iRes)) {
    $sRet .= " <RESULT>\n";

    for($a = 0; $a < $iNumFields; $a++) {
    $sTmp = mysql_field_name($iRes,$a);
    $sRet .= " ".strtoupper("<$sTmp>");
    $sRet .= $iRow["$sTmp"];
    $sRet .= strtoupper("</$sTmp>")."\n";
    }

    $sRet .= " </RESULT>\n";
    }

    $sRet .= "</RESULTSET>\n";

    return $sRet;
    }
    }
     
    Dade72, Apr 3, 2008 IP
  4. smudger

    smudger Active Member

    Messages:
    258
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #4
    yes, something like that ...

    but can i use that exact code ?
     
    smudger, Apr 4, 2008 IP
  5. smudger

    smudger Active Member

    Messages:
    258
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #5
    OK this is where i have got to now

    it almost works .... but not quite ! ......

    <?php

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

    $host = "localhost";
    $user = "xyz";
    $pass = "xyz";
    $database = "xyz";

    $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
    mysql_select_db($database, $linkID) or die("Could not find database.");

    $query = "SELECT * FROM family ORDER BY family_name DESC";
    $resultID = mysql_query($query, $linkID) or die("Data not found.");

    $xml_output = "<?xml version=\"1.0\"?>\n";
    $xml_output .= "<gallery name=\"Test Pictures\">\n";

    for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
    $row = mysql_fetch_assoc($resultID);
    $xml_output .= "\t<picture name=\"NEEDS TO BE DYNAMIC\">\n";
    $xml_output .= "\t\t<family>" . $row['family_name'] . "</family>\n";
    // Escaping illegal characters
    $row['text'] = str_replace("&", "&", $row['text']);
    $row['text'] = str_replace("<", "<", $row['text']);
    $row['text'] = str_replace(">", "&gt;", $row['text']);
    $row['text'] = str_replace("\"", "&quot;", $row['text']);
    $xml_output .= "\t\t<text>" . $row['licence_details'] . "test</text>\n";
    $xml_output .= "\t</picture>\n";
    }

    $xml_output .= "</gallery>";

    echo $xml_output;

    ?>


    the problem is ... i need to load the attribute 'name' from a value in my database - where i have marked "NEEDS TO BE DYNAMIC" in the script above

    is this possible ?

    can somebody please point me in the right direction ?


    thanks
     
    smudger, Apr 5, 2008 IP
  6. smudger

    smudger Active Member

    Messages:
    258
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #6
    can anybody help me further with this?
     
    smudger, Apr 6, 2008 IP
  7. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Not sure what you exactly need.

    For example,
    $xml_output .= "\t\t<family>" . $row['family_name'] . "</family>\n";
    is already dynamic.

    Just do the same for:
    xml_output .= "\t<picture name=\"$row['picture_name']\">\n";
     
    Dagon, Apr 6, 2008 IP
  8. smudger

    smudger Active Member

    Messages:
    258
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Hi Dagon

    yes thats what i figured as well

    but when i have tried it, it gives me a parse error

    Parse error: parse error, unexpected T_CONCAT_EQUAL in xml.php on line 21
     
    smudger, Apr 6, 2008 IP
  9. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #9
    Sorry, correct syntaxis is:

    xml_output .= "\t<picture name=\"".$row['picture_name']."\">\n";
     
    Dagon, Apr 6, 2008 IP
  10. smudger

    smudger Active Member

    Messages:
    258
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Sorry ... still giving the same parse error

    but i feel you are closer because the code is the correct colours in Dreamweaver !
     
    smudger, Apr 6, 2008 IP
  11. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #11
    I also forgot the $ in front ;)

    $xml_output .= "\t<picture name=\"".$row['picture_name']."\">\n";

    mm I think i should get some coffee :p
     
    Dagon, Apr 6, 2008 IP
  12. smudger

    smudger Active Member

    Messages:
    258
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #12
    excellent !

    i didn't spot that either !

    now it works !

    thanks thanks , and thanks again !
     
    smudger, Apr 6, 2008 IP