Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    can only retrieve xml from one page

    I am using the SimpleXMLElement for php to get an xml file. In one page the function:

    PHP Code:
    $xml_file = new SimpleXMLElement(file_get_contents($layout));
        
    $child $xml_file->children();
    $child[$i]->title 
    will work fine. But in a file which gets included at the top of the page, this code will not work. The values needed to access the correct xml file is retrieved from the current url and my url is being set no problem in every page with the code at the top of the page. Only my SimpleXMLElement() wont work here.

    The code above is exactly the same on each page and all the necessary values are set on page as well. Only this code will only fire on just one page (Where it gets included into the page half way down the html).

    Any ideas onto what I should look for or beaware of?

    Thanks a lot

  • #2
    Regular Coder
    Join Date
    May 2010
    Location
    Bathurst, Australia
    Posts
    180
    Thanks
    1
    Thanked 22 Times in 22 Posts
    You're probably going to have to give more code to solve this (or I would need more code to solve this :P) You should be able to use that code on it's own with NO html, so it's weird you have this problem. But my guess is that it's not WHERE the code is situated by WHY you've put it there. What have you done different on that page that you didn't on the others?
    Disclaimer: I'm hungover 70% of the time i'm on here, any information given may not be correct, or even legible.

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks for the reply, this is just not making sense. Here is my code that works all except the part at the bottom about getting the contents from the XML file:

    PHP Code:
    <?php



        
    //gets current url
        
    $url $_SERVER['REQUEST_URI'];
        
        
        
    // Removes Application root from URL
        
    if(preg_match(EBOOK_ROOT_REGEX$url))
        {
            
    $url str_replace(EBOOK_ROOT.DS,'',$url);
        }
        else if (
    preg_match(ADD_CART_ROOT_REGEX$url))
        {
            
    $url str_replace(ADD_CART_ROOT.'?id=','',$url);

            
    $n $url;
            
    $n substr($n2); // gets id of book
            
    $url substr($url02); // gets category of book
            /*
            $book_id = substr($url, 2);
            $url = str_replace($book_id,'',$url); // assigns $url the two letter code 
            $book_id = substr($book_id, 1); // gets book name and id
            $book_char = strrpos($book_id, '/', -1); // gets position of '/' in $book_id
            $book_name = $book_id; // copy value
            $book_id = substr($book_id, $book_char + 1); // assigns $book_id the id of book
            $book_name = substr($book_name, 0, $book_char); // assigns $book_name name of book
            */
            
            
            
        
    }
        


    // new variable $controller declared
    $ebook_home 'ebook_library';


    // $view refers to pages or actually sections of pages to fill in the main body of the page
    // The functioning code is found in views/layout/photos.php
    switch ($url) {

        case 
    "mk":
            
    $layout MARKETING_XML;
        break;
        
        case 
    "bs":
            
    $layout BUSINESS_XML;
        break;
        
        case 
    "eb":
            
    $layout CREATE_EBOOKS_XML;
        break;
        default:
            
    //$layout = $url;

            
    }
        echo 
    '$url: '.$url.'<br />';
        echo 
    '$layout: '.$layout.'<br />';


        
        
    $xml_file = new SimpleXMLElement(file_get_contents($layout));
        
    $child $xml_file->children();
        
    $book_id $child[$n]->id;
        echo 
    'ebook_select: '.$book_id;
        
    $book_name $child[$n]->title;

    ?>
    The last block of code does not work at all in this file.

    Further down the page I included a file which lays out the xml data with html and little snippets of PHP for dynamic data: (this file works perfectly)

    PHP Code:
    <?php
    // file gets included into ebooks.php

    ?>

    <!-- ebook shopping cart menu -->

        <div class="ebook_cart_menu">
            <a href="#">view cart</a>
            <a href="#">clear cart</a>
        </div>
        
    <?php


    // new variable layout declared
    if (isset($layout)) {
        
    //include_once($_SERVER['DOCUMENT_ROOT'].ROOT.'/'.$layout.'.php');
        
    $xml_file = new SimpleXMLElement(file_get_contents($layout));

        
        
        
    $child $xml_file->children();
        
    //$id = $xml_file->ebook->attributes();
        //print_r($child);
        
    if(phpversion() >= '5.3.0')
        {
        
    $count $xml_file->count();

        }
        else    
        {
        
    $doc = new DOMDocument();
        
    $str $xml_file->asXML();
        
    $doc->loadXML($str);
        
    $count $doc->getElementsByTagName("ebook")->length;
        
        } 

        
        
    //print_r($xml_file);
        

        
        
    echo '<table border="1"><tr>';

        
    $i 0;
        while(
    $i <= $count)
        {
            
            echo 
    '<td class="ebook_cell">';
            echo 
    '<img src="'.$child[$i]->bookCover.'" height="150" width="150"/><br />'.
                 
    '<h2 class="ebook_title">'.$child[$i]->title.'</h2>'.'<br />'.
                 
    '<div class="text">'.$child[$i]->description.'</div>'.'<br />'.
                 
    '<div class="ebook_price">'.$child[$i]->price.'</div>'.'<br />'.
                 
    '<a href="'.ROOT.'/add_to_cart.php?id='.$child[$i]->url.'" class="ebook_add_to_cart">Add to cart</a>';
            echo 
    '</td>';     
            
    $i++;
            echo 
    '<td class="ebook_cell">';
            echo 
    '<img class="ebook_cover" src="'.$child[$i]->bookCover.'" height="150" width="150"/><br />'.
                 
    '<h2 class="ebook_title">'.$child[$i]->title.'</h2>'.'<br />'.
                 
    '<div class="text">'.$child[$i]->description.'</div>'.'<br />'.
                 
    '<div class="ebook_price">'.$child[$i]->price.'</div>'.'<br />'.
                 
    '<a href="'.ROOT.'add_to_cart.php?id='.$child[$i]->url.'" class="ebook_add_to_cart">Add to cart</a>';
            echo 
    '</td></tr><tr>';
            
    $i++;
        }


        echo 
    "</tr></table>";
        
    } else {

        include_once(
    $_SERVER['DOCUMENT_ROOT'].TEMPLATES.'/ebook_library.php');
    }

    ?>
    As you can see they both use the exact same initialization of the simpleXMLElement() function. Both functions use the same exact variable for a parameter ($layout) and $layout is holding the correct values the entire way through as well.

    Could my problem lie in the file paths based on where each file came from that was called in correlation to the file to my XML file?

    my top file has a path: functions/ebook_select.php
    my second file (which works) included here is: templates/show_ebooks.php

    Both of these above files are ones called into main page files which are accessed via menu navigation.

    The third place I try this XML call which doesnt work is placed directly into a main page file. I did a print_r($child[5]->title) and this returned nothing in my direct call to a main page or root file.

    I am trying to identify a pattern, but not seeing anything.

    Thanks for the help

  • #4
    Regular Coder
    Join Date
    May 2010
    Location
    Bathurst, Australia
    Posts
    180
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Code:
    $layout = MARKETING_XML;
    By the look of that MARKETING_XML is a variable, yet I don't see it declared. You sure it's not supposed to be
    Code:
    $layout = "MARKETING_XML";
    If i'm right i'm going to assume you just facepalmed yourself.
    Disclaimer: I'm hungover 70% of the time i'm on here, any information given may not be correct, or even legible.

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Lol, oh I wish that was the problem. MARKETING_XML is a defined constant in my global.php file. This file is the first one included on every page.

    Here is the code for my MARKETING_XML constant.

    PHP Code:
    define('MARKETING_XML''database/marketing.xml'); 
    besides, as I said $layout is not the problem. If it were then none of my xml calls would work. I also have $layout being echoed to my page to inform me that $layout is being set properly for that page load.

    I really do appreciate the suggestions, and look forward to the next one.

    Thanks in advance
    Ben

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Lol, oh I wish that was the problem. MARKETING_XML is a defined constant in my global.php file. This file is the first one included on every page.

    Here is the code for my MARKETING_XML constant.

    PHP Code:
    define('MARKETING_XML', 'database/marketing.xml');
    besides, as I said $layout is not the problem. If it were then none of my xml calls would work. I also have $layout being echoed to my page to inform me that $layout is being set properly for that page load.

    I really do appreciate the suggestions, and look forward to the next one.

    Thanks in advance
    Ben

  • #7
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts
    bumping up


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •