parti
parti

Reputation: 215

PHP Simple HTML DOM Parser: Insert HTML

I'm trying to use Simple HTML DOM Parser to extract some html from page B and insert the html code into the current page. (Please be gentle, I'm a PHP newbie)

Page B html I'm targeting looks like this: <div id="testdiv">B</div>

I need the same code to be inserted in current page.

My PHP on current page:

<?php
include_once '%resource(simple_html_dom.php)%';
$ret = Array();
$html = file_get_html('b.html');
$ret[] = $html->find('div[id=testdiv]', 0);
$ret->outertext = $ret->makeup() . $ret->innertext . '</div>';
echo $ret;
?>

I've tried to cobble this together, but still no luck. I would expect when viewing the current page to see "B", but I definitely need all the html coding to be there as well ;)
Currently getting an error "Call to a member function makeup() on a non-object"

P.S. Once that's running, I probably need a way to bring in the css and js that's on pg B.

Thanks for the help, Bill

UPDATED Code:

<?php
include_once '%resource(simple_html_dom.php)%';
$html = file_get_html('b.html');
foreach($html->find('div[id=testdiv]') as $ret)
  echo $ret;
?>

So now I need to be able to get specif css & js files from page B so whatever content is in the div will render properly. Here's an example Current Page If you look in the header of Page B there will be a typical css and js file called b_files/stacks_page_page1.css & b_files/stacks_page_page1.js

These are the files I need to have referenced in the current page w/ the relative path changed as necessary. I also added a Page C which is in a folder (B is in root w/ current page) so path will be differnt. I guess combining the css files together & js files together would be good so there aren't a bunch of dom calls.

Overall, what's the code to bring those files in?

Upvotes: 1

Views: 2223

Answers (3)

Colin Pear
Colin Pear

Reputation: 3100

You are getting the "Call to a member function makeup() on a non-object" because $ret is an array and doesnt have a method called "makeup()."

I am not sure exactly all you are trying to do but something like this might get you a little closer. I am assuming you have some function named makeup() that produces a string "< div>":

Not Tested

<?
php include_once '%resource(simple_html_dom.php)%';  
$html = file_get_html('b.html'); 
$ret = $html->find('div[id=testdiv]', 0)->innertext; 
$ret = makeup() . $ret . '</div>'; 

echo $ret; 
?> 

Upvotes: 1

user1732887
user1732887

Reputation: 298

foreach($html->find('div[id=testdiv]') as $content)
    echo $content;

the result woud be <div id="testdiv">B</div> and it will be displayed on the browser as

B

if you want to display the code:

foreach($html->find('div[id=testdiv]') as $content)
    echo "<xmp>".$content."</xmp>";

real result: <xmp><div id="testdiv">B</div></xmp>

then on the browser's display:

<div id="testdiv">B</div>

Upvotes: 1

h0tw1r3
h0tw1r3

Reputation: 6818

There are a number of problems with the code. $ret[] means push onto the array, but then the next line you are calling object methods, which is why you get the error.

Once that problem is fixed, $ret->makeup is not a valid method according to the API (unless you have modified/extended it). I'm not sure what you think you're doing with $ret->outertext = $ret->makeup() . $ret->innertext . '</div>', but if you're expecting B in your output:

<?php
include_once 'simple_html_dom.php';
$html = file_get_html('b.html');
$ret = $html->find('div[id=testdiv]', 0);
if ($ret) {
    echo $ret->innertext;
}

Upvotes: 1

Related Questions