2

I have one question to ask you.

I have 2 PHP files first one is index.php and another one is body.php

index.php contain HTML template like

<html>
     <head>
        <title></title>
     </head>
     <body>
        <? include('body.php') ?>
     </body>
</html>

and body.php query data from database(such as name, nickname, age).

I need body.php to change tag or add more tag in index.php

How should i do in PHP command?

thanks

1
  • you have 2 options, 1. dont output anything and hold it in output buffer then use str_replace on the buffer to replace/add the tags or build appon the basic index content without outputting first, with php its easier & more performance friendly to build appon content and output last then to keep echoing. Commented Jun 7, 2011 at 6:34

5 Answers 5

4

In your example, body.php can have any HTML output you need. The output of body.php will be included in your final output.

If you need to make the final output of index.php dependent on the body.php file, (for example to insert a title) you can load your content into variables, which can be outputted later.

<? 
  include ('body.php'); 
  /* $title and $bodyHTML are set in the include file */
?>


<html>
     <head>
        <title><? echo $title; ?></title>
     </head>
     <body>
        <? echo $bodyHTML;?>
     </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

2

You can use fopen() and fwrite() to modify the content of index.php from body.php (assuming that you have the write permissions, of course).

If you mean change the content while the user is viewing index.php and then change index.php, then that isn't possible without telling the user to "click here and view the new code!" (since at that point, you can no longer use headers to refresh the page).

PHP is not a dynamic content language like, for example, JavaScript.

Comments

1

You can't alter variables in part of the page that has already been output. You can use output buffering to capture the output to that point and then do string substitutions on it

<?php ob_start(); // start buffering output
?>
<html>
     <head>
        <title></title>
     </head>
     <body>
        <?php
                    include('body.php');
                    // Get the contents of the buffer and then clear the buffer
                    $buffer = ob_get_clean();
                    // Replace your keyword with a variable loaded from body.php
                    $buffer = str_replace('%nickname%', $nickname, $buffer);
                    // output the altered head
                    echo $buffer;
                    // Stop buffering and output what we just echoed
                    ob_end_flush();
                ?>
     </body>
</html>

There are a number of PHP template and theming engines out there that make doing this kind of thing easier. Smarty is a fairly popular one. Another one I like is Savant but I'm personally partial to the one I created called Enrober.

Comments

0

write db related stuff in body.php file and call those functions from index.php. Loop those results and built through related tags and display.

Thats it.........

Comments

0

You could output the entire thing from a php object called domdocument, which allows dynamic creation of html documents. That way, you could change the tags and their content as dynamically as you want.

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.