user1462495
user1462495

Reputation: 43

Add HTML elements to the current page using PHP

So I have the need to dynamically add html content using php which isnt the tricky part but I'm trying to put the HTML into a different location in the document than where the PHP is being run. So for example:

<div id="firstDiv">
    <?php
        echo "<div id=\"firstDivA\"></div>";
        echo "<div id=\"secondDivA\"></div>";
    ?>
</div>
<div id="secondDiv">
</div>

But I want to be able to place the some HTML inside "secondDiv" using the PHP that is executed in the "firstDiv". The end result should be:

<div id="firstDiv">
    <div id="firstDivA"></div>
</div>
<div id="secondDiv">
    <div id="secondDivA"></div>
</div>

But I have no idea how to go about doing that. I read about some of the DOM stuff in PHP 5 but I couldn't find anything about modifying the current document.

Upvotes: 4

Views: 21822

Answers (5)

jchook
jchook

Reputation: 7220

You can open/close "blocks" of PHP wherever you like in your HTML

<div id="firstDiv">
    <?php echo '<div id="firstDivA"></div>'; ?>
</div>
<div id="secondDiv">
    <?php echo '<div id="secondDivA"></div>'; ?>
</div>

You can also capture the output if necessary with ob_start() and ob_get_clean():

<?php
$separator = "\n";

ob_start();
echo '<div id="firstDivA"></div>' . $separator;
echo '<div id="secondDivA"></div>' . $separator;
$content = ob_get_clean();

$sections = explode($separator, $content);
?>
<div id="firstDiv">
    <?php echo $sections[0]; ?>
</div>
<div id="secondDiv">
    <?php echo $sections[1]; ?>
</div>

Upvotes: 5

Boris Gu&#233;ry
Boris Gu&#233;ry

Reputation: 47585

If you want to work with XML-data (read XHTML), you'd rather use an appropriate XML processor.

DomCrawler is an excellent to work with DOM. It works with the native DOM Extension and therefore is fast and widely used.

Here an example from the doc on how to add content:

  $crawler = new Crawler();
  $crawler->addHtmlContent('<html><div class="foo"></div></html>');

  $crawler->filter('div')->attr('class') // returns foo

Upvotes: 0

Rey Gonzales
Rey Gonzales

Reputation: 856

I'm not sure what you're asking. Perhaps just add an echo statement to the second div.

<div id="firstDiv">
    <?php echo "<div id=\"firstDivA\"></div>"; ?>
</div>
<div id="secondDiv">
    <?php echo "<div id=\"secondDivA\"></div>"; ?>
</div>

Or do you mean you want to make DIV changes after PHP? Try jQuery! Or do you mean you want to make DIV changes before PHP is finished? Perhaps phpQuery is good for you then.

Upvotes: 0

secretformula
secretformula

Reputation: 6432

The .php file is continuous thus if you have two separate <?php ?> tags they will be able to share the same variables.

 <div id="firstDiv">
 <?php
    echo "<div id=\"firstDivA\"></div>";
    $div2  = "<div id=\"secondDivA\"></div>";
 ?>
 </div>
 <div id="secondDiv">
    <?php echo $div2 ?>
 </div>

This will give the desired effect. (Demonstrates the use of variables)

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Why not just move the relevant code to the right place?

<div id="firstDiv"> 
    <?php 
        echo "<div id=\"firstDivA\"></div>"; 
    ?> 
</div> 
<div id="secondDiv"> 
    <?php 
        echo "<div id=\"secondDivA\"></div>"; 
    ?> 
</div> 

Upvotes: 1

Related Questions