Welcome Guest, Not a member yet? Register   Sign In
SimpleXML Object is not passed to view
#1

[eluser]pixelknipsr[/eluser]
Hi there,

I hope that someone has the answer to my problem :-)

I'd like to pass a simplexml object from my controller to my view. Here's the code:
Code:
function loadxml($url)
    {
        $curl_handle=curl_init();
        curl_setopt($curl_handle,CURLOPT_URL,$url);
        curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,3);
        curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
        return curl_exec($curl_handle);
        curl_close($curl_handle);
    }

    function Index()
    {
        $url = 'http://some-site.com/myData.xml';
        $xmlLoading = $this->loadxml($url);
        $xml = simplexml_load_string($xmlLoading);
        $outputarray = array();
        if ($xml) {
            foreach ($xml->Auctions->Auction as $auction) {
                array_push($outputarray, $auction);
            }
        }
        $this->load->view('header');
        $this->load->view('listing_view', $outputarray);
        $this->load->view('footer');                              
    }

The XML is correctly loaded and the $outputarray has the correct data are it. Still I get a "undefined variable" in my view :-( Can't I transfer such an object?

I also tried to pack everything into an array as described here: http://codingforums.com/showthread.php?t=87283 and it didn't work either.

Thanks for help me out.
#2

[eluser]Raiko[/eluser]
Can we see your view?

Also, in the loadxml function, you're returning before you close curl. You can't do that. The close_curl function is never getting called.
#3

[eluser]pixelknipsr[/eluser]
Hi Raiko,

Thanks pointing out my error in the loadxml function.

Here's my little class:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Listing extends Controller {
    function Listing()
    {
        parent::Controller();
    }
    function loadxml($url)
    {
        $curl_handle=curl_init();
        curl_setopt($curl_handle,CURLOPT_URL,$url);
        curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,3);
        curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
        return curl_exec($curl_handle);
        curl_close($curl_handle);
    }
    
    function Index()
    {
        $url = 'http://www.ricardo.ch/Default/GetXMLListing.asp?catg=1&listingtype=7&listingsort=1&MaxPerPage=2';
        $xmlLoading = $this->loadxml($url);
        $xml = simplexml_load_string($xmlLoading);
        print_r($xml); // $xml is loaded correctly
        $outputarray = array();
        if ($xml) {
            foreach ($xml->Auctions->Auction as $auction) {
                array_push($outputarray, $auction);
            }
        }
        $this->load->view('header');
        $this->load->view('listing_view', $outputarray);
        $this->load->view('footer');                              
    }
}
?>

And here's my view:
Code:
<?php
print_r($outputarray);
?>

I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: outputarray
Filename: views/listing_view.php
Line Number: 2
#4

[eluser]scatterscatter[/eluser]
I was having a similar issue until I embedded it into an array, like:

Code:
$process['xml'] = $xml = new SimpleXMLElement($data);

$this->load->view('index', $process);
#5

[eluser]pixelknipsr[/eluser]
Thanks for your help :-D

I finally found an answer:

I added this line:
Code:
$data['xml'] = $outputarray;

cheerio.




Theme © iAndrew 2016 - Forum software by © MyBB