Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269

    Creare dinamicamente un xml con php

    Salve ragazzi io ho sue tabelle sul database, che sono le seguenti:

    codice:
    tabella "categoria"
    id_categoria(PK) - nome
    1                        Gossip
    2                        Veline
     
    tabella "immagine"
    id_immagine(PK) - id_categoria(FK) data - titolo - descrizione - path
    1-                      1-                     07/03/2011-Titolo 1 Descrizione 1-Path1  
    2-                      1-                     07/03/2011-Titolo 2 Descrizione 2-Path2
    3-                      1-                     07/03/2011-Titolo 3 Descrizione 3-Path3
    4-                      2-                     07/03/2011-Titolo 4 Descrizione 4-Path4
    5-                      2-                     07/03/2011-Titolo 5 Descrizione 5-Path5
    Adesso dovrei generare un file xml chiamato "gallery.xml", in base ai valori delle tabelle...

    l'output del file xml dovrebbe essere una cosa di questo tipo:

    codice:
    <gallery title="Nome" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
    	<category name="Gossip">
    		<image>
    			<date>Lavori</date>
    			<title>Titolo 1</title>
    			<desc>Lavori</desc>
    			<thumb>modulaartwork (4).jpg</thumb>
    			<img>modulaartwork (4).jpg</img>
    		</image>
    		<image>
    			<date>Lavori</date>
    			<title>Titolo 2</title>
    			<desc></desc>
    			<thumb>modulaartwork (2).jpg</thumb>
    			<img>modulaartwork (2).jpg</img>
    		</image>
    		<image>
    			<date>Lavori</date>
    			<title>Titolo 3/title>
    			<desc></desc>
    			<thumb>modulaartwork (3).jpg</thumb>
    			<img>modulaartwork (3).jpg</img>
    		</image>
             </category>
    	<category name="Calciatori">
    		<image>
    			<date>Lavori</date>
    			<title>Titolo 4</title>
    			<desc>Lavori</desc>
    			<thumb>modulaartwork (4).jpg</thumb>
    			<img>modulaartwork (4).jpg</img>
    		</image>
    		<image>
    			<date>Lavori</date>
    			<title>Titolo 5</title>
    			<desc></desc>
    			<thumb>modulaartwork (2).jpg</thumb>
    			<img>modulaartwork (2).jpg</img>
    		</image>
    		<image>
    			<date>Lavori</date>
    			<title>Modula Gioielli</title>
    			<desc></desc>
    			<thumb>modulaartwork (3).jpg</thumb>
    			<img>modulaartwork (3).jpg</img>
    		</image>
             </category>
    </gallery>
    Io avevo fatto una cosa di questo tipo in php (xmlCreate.php)
    Codice PHP:
    <?php

    include ("connessione.php");

    $sql "select * from categoria";
    $result mysql_query($sql) or die ("Errore nella selezione delle squadre " mysql_error());

    $elements

    //Creates XML string and XML document using the DOM.
    $xml = new DomDocument('1.0');

    // Create some elements.
    $xml_gallery     $xml->createElement"gallery" );

    $xml_image     $xml->createElement"images" );
    $xml_date         $xml->createElement"date" );
    $xml_title         $xml->createElement"title" );
    $xml_desc         $xml->createElement"desc" );
    $xml_thumb     $xml->createElement"thumb" );
    $xml_img         $xml->createElement"img" );


    // Set the attributes for tag <gallery>
    $xml_gallery->setAttribute"title""Modula Gioielli" );
    $xml_gallery->setAttribute"thumbDir""./images/thumbs/" );
    $xml_gallery->setAttribute"imageDir""./images/" );
    $xml_gallery->setAttribute"random""true" );


    while (
    $row mysql_fetch_array($result)) {
        
        
    // Create element <category>.
        
    $xml_category     $xml->createElement"category" );
        
    // Set the attributes for tag <category>
        
    $xml_category->setAttribute"name"$row['nome'] );
        
        
    // Altro ciclo per le immagini associate alla categoria
        
        // Append the whole bunch.
        
    $xml_gallery->appendChild$xml_category );
        
    $xml_category->appendChild$xml_image );
        
    $xml_image->appendChild$xml_date );
        
    $xml_image->appendChild$xml_title );
        
    $xml_image->appendChild$xml_desc );
        
    $xml_image->appendChild$xml_thumb );
        
    $xml_image->appendChild$xml_img );
        
    // Chiudi ciclo 2
    // Chiudi ciclo 1

    $test $xml->saveXML(); // put string in test1 
    $xml->save('gallery.xml'); // save as file
    Non sò se vada bene una cosa del genere, eventualmente qualcuno potrebbe farmi capire come completare il file xmlCreate.php.

    P.S
    Ovviamente penso che si dovrà creare una join per estrapolare i dati? è così? io ho provata a farla ma non funge come dovrebbe

    Grazie anticipatamente,
    Gaten
    Con i sogni possiamo conoscere il futuro...

  2. #2
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    up
    Con i sogni possiamo conoscere il futuro...

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2010
    Messaggi
    570
    puoi usare domdocument come già stai facendo (ho letteralmente solo buttato un occhiata al codice che hai postato, vista l'ora e il numero di righe).

    se hai qualche dubbio controlla anche su php.net, ad ogni modo se vuoi un consiglio, vista che è una delle prime volte, fai generare il codice e mandalo a schermo, facendo un echo dell' $xml->saveXML()
    se hai problemi poi dimmi

  4. #4
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Ragazzi io qui mi sono bloccato, praticamente non riesco a capire come estrapolare i dati dal db e ciclarli:

    Questo sono riuscito a fare:
    Codice PHP:
    <?php

    include ("connessione.php");

    $sql "select immagine.*, categoria.nome 
            from immagine, categoria where categoria.id_categoria = immagine.id_categoria"
    ;
    $result mysql_query($sql) or die ("Errore nella selezione delle squadre " mysql_error());

    //Creates XML string and XML document using the DOM.
    $xml = new DomDocument('1.0');

    // Creo l'elemento <gallery>
    $xml_gallery     $xml->createElement"gallery" );

    // Creo gli elementi della <categoria>
    $xml_image         $xml->createElement"images" );
    $xml_date         $xml->createElement"date" );
    $xml_title         $xml->createElement"title" );
    $xml_desc         $xml->createElement"desc" );
    $xml_thumb         $xml->createElement"thumb" );
    $xml_img         $xml->createElement"img" );


    // Set the attributes for tag <gallery>
    $xml_gallery->setAttribute"title""Modula Gioielli" );
    $xml_gallery->setAttribute"thumbDir""./images/thumbs/" );
    $xml_gallery->setAttribute"imageDir""./images/" );
    $xml_gallery->setAttribute"random""true" );


    /* while ($row = mysql_fetch_array($result)) {
        
        // Create element <category>.
        $xml_category     = $xml->createElement( "category" );
        // Set the attributes for tag <category>
        $xml_category->setAttribute( "name", $row['nome'] );
        

        // Append the whole bunch.
        $xml_gallery->appendChild( $xml_category );
        $xml_category->appendChild( $xml_image );
        $xml_image->appendChild( $xml_date );
        $xml_image->appendChild( $xml_title );
        $xml_image->appendChild( $xml_desc );
        $xml_image->appendChild( $xml_thumb );
        $xml_image->appendChild( $xml_img );
        
    } */

    $test $xml->saveXML(); // put string in test1 
    $xml->save('gallery.xml'); // save as file 
    ?>
    Qualcuno potrebbe aiutarmi a completarlo
    P.s: Ho commentato quello che ho cercato di fare ma non và.
    Con i sogni possiamo conoscere il futuro...

  5. #5
    Che problema ti da?
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  6. #6
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Nessun Problema! Il problema stà nel fatto che non riuscendo a continuare non riesco nemmeno a provare!
    Con i sogni possiamo conoscere il futuro...

  7. #7
    Originariamente inviato da gaten
    Nessun Problema! Il problema stà nel fatto che non riuscendo a continuare non riesco nemmeno a provare!
    Non capisto cosa intendi! Se decommenti la parte commentata succederà pur qualcosa o no?
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  8. #8
    Utente di HTML.it L'avatar di gaten
    Registrato dal
    Jul 2007
    Messaggi
    1,269
    Risolto così:
    Codice PHP:
    <?php
    header 
    ("content-type: text/xml");

    include(
    "connessione.php");

    $sql="SELECT * FROM `categoria` ORDER BY `id_categoria` ASC";

    $result mysql_query($sql) or die ("Errore query: ".mysql_query($sql));

    $xml '<gallery title="Modula Gioielli" thumbDir="http://'.$_SERVER['SERVER_NAME'].'/images/" imageDir="http://'.$_SERVER['SERVER_NAME'].'/images/" random="true">';

    while(
    $category mysql_fetch_assoc($result)){
        
    $xml .= '<category name="'.$category['nome'].'">';
        
    $result2 mysql_query("SELECT * FROM `immagine` WHERE `id_categoria` = '".$category['id_categoria']."' ORDER BY `id_immagine` ASC");
            while(
    $image mysql_fetch_assoc($result2)){
                
    $xml .= "<image>";
                
    $xml .= "<date>".$image['data']."</date>";            
                
    $xml .= "<title>".$image['titolo']."</title>";
                
    $xml .= "<desc>".$image['descrizione']."</desc>";
                
    $xml .= "<thumb>".$image['path']."</thumb>";            
                
    $xml .= "<img>".$image['path']."</img>";
                
    $xml .= "</image>";
            }
        
    $xml .= "</category>";
    }

    $xml .= '</gallery>';

    $XMLFile fopen("gallery.xml","w") or die("ERRORE: Non posso aprire il file!");
    fwrite($XMLFile$xml);
    fclose($XMLFile);
    ?>
    ;

    Grazie a tutti!
    Con i sogni possiamo conoscere il futuro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.