SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Sep 2002
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Recursive Function through XML?

    I guess I'm not as good at recursive functions as I thought. Below I have an XML file that I need to recursively go through. Mainly because not all the XML files are going to be the same (it's a huge pain). However, I really would like to not 'echo' the html out to a web page. I would really like to be able to store it in an $html variable and return that variable when the function is done with its recursion.

    Any ideas on how to do this? The XML is below along with my code to 'echo' the html directly to the browser.

    thanks!

    Code:
    <title name="Coffee Basics">
          <topic label="Welcome" content="../../courses/coffee_basics/welcome.swf" swfSize="large" runtime="444"></topic>
          <topic label="Trainer's Introduction" content="../../courses/coffee_basics/Trnrs_Intro.swf" swfSize="large" runtime="444"></topic>
          <section label="VBtP">
            <section label="Coffee">
              <topic label="History of Coffee" content="../../vignettes/voice_behind_the_products/coffee/flv/history_coffee.flv" runtime="126.89"/>
              <topic label="What Makes Coffee Program Unique?" content="../../vignettes/voice_behind_the_products/coffee/flv/program_unique.flv" runtime="146.81"/>
              <topic label="Sourcing Practices" content="../../vignettes/voice_behind_the_products/coffee/flv/why_no_fair.flv" runtime="88.02"/>
              <topic label="Coffee Certifications" content="../../vignettes/voice_behind_the_products/coffee/flv/makes_organic.flv" runtime="91.76"/>
              <topic label="In-store Roasting Program" content="../../vignettes/voice_behind_the_products/coffee/flv/roast_onsite.flv" runtime="49.88"/>
              <topic label="Recommending Coffee" content="../../vignettes/voice_behind_the_products/coffee/flv/rec_consume.flv" runtime="51.22"/>
            </section>
            <section label="Meet">
              <topic label="Passion for Coffee" content="../../vignettes/voice_behind_the_products/coffee/flv/passion_.flv" runtime="48.35"/>
              <topic label="What Inspired You" content="../../vignettes/voice_behind_the_products/coffee/flv/inspire_coffee.flv" runtime="77.54"/>
              <topic label="Favorite Story" content="../../vignettes/voice_behind_the_products/coffee/flv/fav_story.flv" runtime="230.46"/>
            </section>
            <section label="Coffee Fast Facts">
              <topic label="Growing, Harvesting, Processing, Roasting" content="../../vignettes/voice_behind_the_products/coffee/flv/grow_process.flv" runtime="242.21"/>
              <topic label="Where are Coffees Grown?" content="../../vignettes/voice_behind_the_products/coffee/flv/climate_coffee.flv" runtime="34.33"/>
              <topic label="Regional Taste Differences" content="../../vignettes/voice_behind_the_products/coffee/flv/taste_diff.flv" runtime="80.71"/>
              <topic label="Taste Terminology" content="../../vignettes/voice_behind_the_products/coffee/flv/taste_terms.flv" runtime="60.19"/>
              <topic label="Coffee Species - Arabica and Robusta" content="../../vignettes/voice_behind_the_products/coffee/flv/arabica_robusta.flv" runtime="63.96"/>
              <topic label="Evaluating Quality" content="../../vignettes/voice_behind_the_products/coffee/flv/eval_coffee.flv" runtime="118.72"/>
              <topic label="Storing Coffee at Home" content="../../vignettes/voice_behind_the_products/coffee/flv/store_cofee.flv" runtime="49.48"/>
            </section>
          </section>
          <topic label="Course Evaluation" content="../../components/course_eval.swf" swfSize="small"></topic>
          <topic label="Certificate of Completion" content="../../components/certificate.swf" swfSize="small"></topic>
        </title>
    PHP Code:
    function edit_nav($xml) {

        foreach (
    $xml->attributes() as $k => $attribute) {
            echo 
    '<p>' $k ' :: <a href="#" onclick="edit_label(this)" title="Eidt:' $attribute '">' $attribute '</a></p>';
        }
        echo 
    '<hr>';
        
        foreach (
    $xml->children() as $children) {
            
    edit_nav($children);
        }
        
        return;


  2. #2
    SitePoint Member
    Join Date
    Jun 2006
    Location
    Chicago or Urbana/Champaign, depending
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use globals:

    PHP Code:
    $html '';

    function 
    edit_nav($xml$first 0) {
        
    // get the $html variable that exists outside the function
        
    global $html;

        foreach (
    $xml->attributes() as $k => $attribute) { // ps: edit is spelled wrong ;)
            
    $html .= '<p>' $k ' :: <a href="#" onclick="edit_label(this)" title="Eidt:' $attribute '">' $attribute '</a></p>';
        }
        
    $html .= '<hr>';
        
        foreach (
    $xml->children() as $children) {
            
    edit_nav($children);
        }

        
    // if first is true, it returns $html, if it isn't, it returns null
        
    return ($first $html null);
    }

    // call like this
    $html edit_nav($xml1); 
    I can't say for sure that it'll work, (since I can't test it) but it should.


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
  •