4

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.

1
  • Why would you do that? What's the problem in moving the code inside the other DIV? Commented Jun 17, 2012 at 23:58

5 Answers 5

5

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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> 

Comments

1

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)

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.