SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: xml from mysql

  1. #1
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xml from mysql

    Hi,

    I am trying to generate this xml schema from a mysql database:

    HTML Code:
    <MENU>		
    	<FOLDER NAME="Web" link="web.swf">
    		<FILE NAME="Printbook" LINK="printbook.jpg"/>
    		<FILE NAME="site" LINK="site.jpg"/>
    	</FOLDER>
    	<FOLDER NAME="Print link="print.swf"">
    		<FILE NAME="Submenu Link 1" LINK="sublink31.html" />
    	</FOLDER>
    </MENU>
    heres my php so far:

    PHP Code:
    <?php
    $db_name 
    "portfolio"
    $link mysql_connect("localhost""root""****") or die("Could not connect to server!");
    $table_name 'folder';
    $select_db mysql_select_db($db_name$link); 
    $query "SELECT * FROM " $table_name;
    $result mysql_query($query$link) or die("Could not complete database query"); 
    $num mysql_num_rows($result); 
    if (
    $num != 0) {
     
    $filefopen("results.xml" "w"); 
      
    $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
      
    $_xml .="<menu>\r\n";
      while (
    $row mysql_fetch_array($result)) { 
        
    $_xml .="   <folder name='" $row[name] . "'  link= '" $row[link] . "'>\r\n";
        
     
    $_xml .=" </folder>\r\n";
        }
       
      
    $_xml .="</menu>";
    fwrite($file$_xml);
    fclose($file); 
      echo 
    $_xml;
    } else {
      echo 
    "No Records found";
    }

    ?>
    This generates this:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <menu>
       <folder name='web'  link= 'web.swf'>
     </folder>
       <folder name='print'  link= ''print.swf'>
     </folder>
    </menu>
    but i cant work out how to add in the files to each folder. Any ideas?

    many thanks,
    Gareth

  2. #2
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you ar grabbing the file data out of the database, you need summit like this:
    PHP Code:
    <?php
     $db_name 
    "portfolio";
     
    $link mysql_connect("localhost""root""****") or die("Could not connect to server!");
     
    $table_name 'folder';
     
    $select_db mysql_select_db($db_name$link);
     
     
    $query "SELECT * FROM " $table_name;
     
    $result mysql_query($query$link) or die("Could not complete database query");
     
    $num mysql_num_rows($result);
     
     if (
    $num != 0)
     {
         
    $filefopen("results.xml" "w");
         
    $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
         
    $_xml .="<menu>\r\n";
         while (
    $row mysql_fetch_array($result))
         {
             
    $_xml .="   <folder name='" $row[name] . "'  link= '" $row[link] . "'>\r\n";
             
             
    // YOU NEED A LOOP SUMMIT LIKE THIS IN HERE!!!
             
             
    $query 'SELECT * FROM fileTable WHERE folder="'.$row[name].'";';
             
    $files mysql_query($query$link) or die("Could not complete database query");
             
    $numOfFiles mysql_num_rows($result);
             
             while (
    $file mysql_fetch_array($result))
             {
                 
    $_xml .= '\t\t<FILE NAME="'.$file[name].'" LINK="'.$row[link].'"/>\r\n';
             }
             
             
    // END YOU NEED A LOOP SUMMIT LIKE THIS IN HERE!!!
             
             
    $_xml .=" </folder>\r\n";
         }
         
    $_xml .="</menu>";
         
    fwrite($file$_xml);
         
    fclose($file);
         echo 
    $_xml;
     }
     else
     {
         echo 
    "No Records found";
     }
     
     
    ?>
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  3. #3
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    many thanks for that

    i am getting these errors when i run the code..


    Warning: fwrite(): supplied argument is not a valid stream resource in C:\public_html\MakeXML\make2.php on line 36

    Warning: fclose(): supplied argument is not a valid stream resource in C:\public_html\MakeXML\make2.php on line 37
    \t\t\r\n\t\t\r\n


    any ideas why?

    Gareth


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
  •