ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

MY_Xml_Writer - Easy XML writer library

March 10, 2009 9:12pm

Subscribe [13]
  • #16 / Apr 26, 2009 11:14am

    Shimura

    12 posts

    It’s already display one node for one result. I haven’t a total result for one node.

    I have a foreach with a value $corr = array ( Graphist, AD, Art Director,...);  and in my foreach i make a if with preg_match :

    Code :

    $icounter = 0;
             
    foreach ($correspondance_graph as $corr){

    /* Test Projets Graphique */
    if(preg_match('`'.$corr.'`', '`'.$row->fonction.'`' )){

    $xml->addNode('Projet_Graph',count($icounter));

    $icounter++;

    }
    }

    $row->fonction this the result of my query.

    The result is always :

    1</Projet_Graph>
    1</Projet_Graph>
    1</Projet_Graph>

  • #17 / Apr 26, 2009 11:16am

    Shimura

    12 posts

    And it’s the same with $xml->addNode(‘Projet_Graph’,$icounter);

    I think that my foreach make always a node when i have a result of my test preg_match with a word.

  • #18 / Apr 26, 2009 11:59am

    JoostV

    527 posts

    Dear Shimura,

    Could you be more specific? I have no idea what you are trying to accomplish.

  • #19 / Apr 26, 2009 12:09pm

    Shimura

    12 posts

    I have a table MySQL with function in a Web Project : for example : Art Director, Developer,... And in my XML i try to recover the name of the job like a value (+1) when the prag_match find the same word in my table of correspondence ($correspondance_graph = array(AD, Graphist,...) with my query ($row->fonction, query(SELECT fonction FROM ...) i recover a value in my XML like that :

    1</Projet_Graph> -> one match word and for two match word : 2</Projet_Graph>
    and not this result :

    1</Projet_Graph>
    1</Projet_Graph>

    This the problem.

  • #20 / Apr 26, 2009 12:12pm

    Shimura

    12 posts

    I think that the problem is with the foreach or the condition when he finds a word with a correspondence, he makes a node and not a total of value.

  • #21 / Apr 26, 2009 12:55pm

    JoostV

    527 posts

    It might be easier if you could solve this problem in you SQL;

    $sql = 'SELECT COUNT(function) as num_fonctions, fonction FROM tablename GROUP BY fonction';

    Then all you’d have to do is create a loop like this:

    $corr = array('Graphist', 'AD', 'Art Director');
    foreach ($correspondance_graph as $corr) {
        if ($corr == $row->fonction) {
            $xml->addNode(Projet_Graph, $row->num_fonctions);
        }
    }
  • #22 / Apr 26, 2009 1:44pm

    Shimura

    12 posts

    Same problem :/

    One node for one result not a total result.

    I have my colums num_fonctions (1,1,1,1,1) in PHPmyAdmin and in my xml :

    1</Projet_Graph>
    1</Projet_Graph>

    How i make a total of that ?

  • #23 / Apr 26, 2009 2:46pm

    JoostV

    527 posts

    Hi Shimura,

    I really need some more info. What exactly is in your table and what exactly are you trying to display?

  • #24 / Apr 26, 2009 3:33pm

    Shimura

    12 posts

    In my table equipes, i have id, fonction(Name of job in a project), id_etudiant, id_projet.

    The result of SQL in PHPmyadmin is :

    [num_functions] [fonction] [id_etudiant]
        1               Graphist     200
        1                 AD         139

    And when i’m in the result of the foreach ($correspondance_graph as $corr) , that’s recover the count yes. But with the foreach, the xml display :

    One result for a node, like this :
    1</projet_graph>
    1</projet_graph>

    And i’m trying to display this for one student :
    2</projet_graph>


    The problems comes that when my foreach find a correspondence, he displays one node :
    1</projet_graph>

    Thank you.

  • #25 / Apr 26, 2009 3:52pm

    JoostV

    527 posts

    OK, so now I know what is in your table.

    $equipes->id;
    $equipes->fonction;
    $equipes->id_etudiant;
    $equipes->id_projet;

    I imagine that your table equipes sets functions that ties students to projects: e.g. student 1 is a graphist for project 23, and AD for project 24, and so on.

    I am still having a hard time understanding what exactly you WANT to display in the XML.

    Do you want to display all functions for a single student? Or all students that take part in a project?

    Show me an example XML, please. I cannot help you if I don’t know what the problme is.

  • #26 / Apr 26, 2009 5:00pm

    Shimura

    12 posts

    This is an example of XML TEST :

    <Etudiant Promotion="2010" Nb_id="224">
            <Annees>h1,h2,h3</Annees>
            <pre>****</pre>
    <p>        <Nom>****</Nom><br />
            <Evaluation><br />
                <Eval_Projet>3</Eval_Projet><br />
                <Eval_Technologie>3</Eval_Technologie><br />
                <Eval_Graphique>3</Eval_Graphique><br />
                <Eval_Maketing>3</Eval_Maketing><br />
            </Evaluation><br />
            <br />
                <Fonction_Projet>Chef de projet</Fonction_Projet><br />
                1</Projet_Projet><br />
                <Fonction_Projet>Designer</Fonction_Projet><br />
                1</Projet_Graph><br />
                <Fonction_Projet>Designer</Fonction_Projet><br />
                1</Projet_Graph><br />
                <Fonction_Projet>Développeur</Fonction_Projet><br />
                1</Projet_Tech><br />
                <Fonction_Projet>Développeur</Fonction_Projet><br />
                1</Projet_Tech><br />
                <Fonction_Projet>Développeur</Fonction_Projet><br />
                1</Projet_Tech><br />
                <Fonction_Projet>Développeur</Fonction_Projet><br />
                1</Projet_Tech><br />
                <Fonction_Projet>Graphiste</Fonction_Projet><br />
                1</Projet_Graph><br />
                <Fonction_Projet>Intégrateur</Fonction_Projet><br />
                1</Projet_Tech><br />
                1</Projet_Tech><br />
                <Fonction_Projet>Soutien</Fonction_Projet><br />
                <Fonction_Projet>Soutien</Fonction_Projet><br />
                <Fonction_Projet>Soutien</Fonction_Projet><br />
            </Projet><br />
            <Stages><br />
                <Stage_Poste>Développeur Flash</Stage_Poste><br />
                <Id_Etudiant>224</Id_Etudiant><br />
            </Stages>
    </pre>

    You see the problem :

    <Fonction_Projet>Designer</Fonction_Projet>
                1</Projet_Graph>
                <Fonction_Projet>Designer</Fonction_Projet>
                1</Projet_Graph>

    And i want :

    <Fonction_Projet>Designer</Fonction_Projet>
     <Fonction_Projet>Designer</Fonction_Projet>
     2</Projet_Graph>

    I recover on result 1 for one node. But me i want a total result for one node.

  • #27 / Apr 26, 2009 5:04pm

    Shimura

    12 posts

    And this is an example of one student only, i have about 40 students for a promotion.

  • #28 / Apr 26, 2009 6:56pm

    JoostV

    527 posts

    First off, you might want to reconsider teh XML you’re creating.
    - all XML is lowercase
    - Nest your tags.
    - you might want to use the same tag for all functions, so <function id=“graph”>, instead of using a different tag for each function

    Anyway. First retrieve all functions for all students, and store them in an array with the student id as key

    /*Fetch all functions for all students*/
    $sql = 'SELECT *, COUNT(`fonction`) as num_fonctions FROM equipes GROUP BY `id_etudiant`, `fonction`';
    $query = $this->db->query($sql);
    
    /*Store in an associative array, with the student id as key*/ 
    $student_functions = array();
    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row) {
            $student_functions[$row['id_etudiant']][] = $row;
        }
    }

    Then, for the branch, you create a loop for a certain student like this (let’s pretend that the ID for that student is available in $id_etudiant)

    /*Add all functions for this student*/
    $xml->startBranch('projet');
    foreach ($student_functions[$id_etudiant] as $function) {
        $xml->startBranch('fonction', array('id' => $function['fonction']));
        $xml->addNode('num_functions', $function['num_fonctions']);
        $xml->endBranch();
    }
    $xml->endBranch();

    This will give you a clean piece of properly nested XML without any repetitions, like this:

    
        <fonction id="Chef de projet">
            <num_functions>1</num_functions>
        </fonction>
        <fonction id="Designer">
            <num_functions>2</num_functions>
        </fonction>
    </projet>

    I’m sure you can figure out any modifications yourself.

    Hope to have been of help here.

    EDIT: alternatively, you could also construct an XML like:

    
        <fonction>
            <jobtitle>Chef de projet</jobtitle>
            <type>project</type>
            <num_functions>1</num_functions>
        </fonction>
        <fonction>
            <jobtitle>Designer</jobtitle>
            <type>graph</type>
            <num_functions>2</num_functions>
        </fonction>
    </projet>

    But that’s really up to you.

  • #29 / Feb 27, 2011 1:01pm

    POPPY Hero 2

    1 posts

    how i can generate it to .xml file (sory i not good english)

  • #30 / Feb 27, 2011 2:17pm

    JoostV

    527 posts

    Just grab the data and save it to a file, for instance using the file helper:

    http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases