How to code PHP function that displays a specific div from external file if div called by getElementById has no value?

China☆狼群 提交于 2019-12-25 04:15:10

问题


Thank you for answering my question so quickly. I did some more digging and ultimately found a solution for grabbing data from external file and specific div and posting it into another document using PHP DOMDocument. Now I'm looking to improve the code by adding an if condition that will grab data from a different div if the one called for initially by getElementById has now data. Here is the code for what I got so far.

External html as source.

<div id="tab1_header" class="cushycms"><h2>Meeting - 12:00pm to 3:00pm</h2></div>

My PHP file calling from source looks like this.

<?php

$source = "user_data.htm";
$dom = new DOMDocument();
$dom->loadHTMLFile($source);
$dom->preserveWhiteSpace = false;
$tab1_header = $dom->getElementById('tab1_header');

?>


<html>

<head>
    <title></title>
</head>

<body>

<div><h2><?php echo $tab1_header->nodeValue; ?></h2></div>


</body>


</html>

The following function will output a message if a div id can't be found but...

if(!tab1_header)
{
    die("Element not found");
}

I would like to call for a different div if the one called for initially has no data. Meaning if <div id="tab1_header"></div> then grab <div id="alternate"><img src="filler.png" /></div>. Can someone help me modify the function above to achieve this result.

Thanks.


回答1:


either split up master.php so div1\2 are in a file each or set them each to a var, them include master.php, and use the appropriate variable

master.php

$d1='<div id="description1">Some Text</div>';
$d2='<div id="description2">Some Text</div>';

description1.php

include 'master.php';
echo $d1;



回答2:


You can't do this solely with PHP includes unless you put the divs into separate files. Look into PHP templating; it's probably the best solution for this. Or, since you're new to the language, try using variables:

master.php

$description1 = '<div id="description1">Some Text</div>';
$description2 = '<div id="description2">Some Text</div>';

board1.php

include 'master.php';

echo $description1;

board2.php

include 'master.php';

echo $description2;

Alternatively, you could use JavaScript, but that might get a little messy.




回答3:


Short answer is: although it's possible it's probably very bad idea taking this approach.

Longer answer: the solution may turn out to be too complicated. If in your master.php file is only HTML markup, you could read content of that file with file_get_contents() function and then parse it (i.e. with DOMDocument library functions). You would have to look for a div with given id.

$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$divs = $doc->getElementsByTagName('div');

foreach ($divs as $div) 
{
   if( $div->getAttribute('id') == 'description1' )
   {
    echo $div->nodeValue."\n";
   }
}
?>

If your master.php file has also some dynamic content you could do following trick:

<?php
ob_start();
include('master.php');
$sMasterPhpContent = ob_get_clean();
// same as above - parse HTML
?>

Edit:

$tab_header = $dom->getElementById('tab1_header') ? $dom->getElementById('tab1_header') : $dom->getElementById('tab2_header');


来源:https://stackoverflow.com/questions/16552051/how-to-code-php-function-that-displays-a-specific-div-from-external-file-if-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!