Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use php code to add lines to html file?

Status
Not open for further replies.

alan123

MIS
Oct 17, 2002
149
US
I have a html file, now I want another php program to manipulate this file and add lines such as:
<?
include_once("abc");
?>
to the top of this html file, so it will look like:
Code:
<?
include_once("abc");
?>
<html>
<body>
....
</body>
</html>

how can I do that with php code? anyone can help me?

Thanks
 
you could pass a variable that depending it's value then prints/ echos the bit of code you want- it would require some input though.
it'd be something like

code passed from other page is a number/ variable

if variable passed = 1 do the thing here
or is variable is 2 do that etc
else do nothing, or do a default thing (whichever you want)

you could pass the variable from a link or a form

----------------------------------------
Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
Alan,

I Don't know exactly what you want to do. If you want to do something like DaRNCaT suggest -then consider this example:

First - the include file [tt]index.inc.php[/tt]

Code:
<?php

  function HTMLbegin($title) {
  // First, we'll break out of PHP for a short while ...
  // The following lines are some HTML lines.   
  ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
    <HTML>
    <HEAD>
      <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">    
  <?php
  // Insert a page title depending on the user's choice
    echo "  <TITLE>This is my $title font page</TITLE>\n";  // The title is inserted.
  // Now we break out of PHP again ...
  ?>
      <STYLE TYPE="text/css">
  <?php
  // Set the font-family depending on the user's choice
    echo "  BODY { font-family : $title; }\n";  // The title is inserted.
  // Now we break out of PHP again ...
  ?>
      </STYLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
  <?php
  //    Back in PHP again
  }  // The HTMLbegin() function ends here


  function HTMLbody($title) {
  // We're breaking out of PHP ...
  ?>
  <H1>Greetings!</H1>
  <HR>
  <?php
  // Back in PHP ...
  echo "You're now on my <B><U>$title</U></B> page!<BR>\n";
  }  // The HTMLbody() function ends here


  function HTMLlinks() {
  // We're breaking out of PHP ...
  ?>
  <A HREF="index.php?what=verdana">Verdana Font Page</A>
  <BR>
  <A HREF="index.php?what=impact">Impact Font Page</A>
  <BR>
  <A HREF="index.php">Arial Font Page</A>
  <?php
  }  // The HTMLbody() function ends here


  function HTMLend() {
  // We're breaking out of PHP ...
  ?>
  </BODY>
  </HTML>
  <?php
  }  // The HTMLend() function ends here


  function buildHTML($title) {
    HTMLbegin($title);
    HTMLbody($title);
    HTMLlinks();
    HTMLend();
  }

?>

Now, the page file itself - [tt]index.php[/tt]

Code:
<?php
 
  $what = $_REQUEST["what"];      // Get the value of the var "what".

  if(!$what)                      // If the var "what" is not set ...
    $what = "arial";              // Then we set it to "arial".

  include_once("index.inc.php");  // Include the functions.

  buildHTML($what);               // Use buildHTML() from index.inc.php to make the HTML.

?>

Does this help you? If not -just get back!

Good Luck ?;O)


Jakob
 
Thanks for the reply.
What I need is based on parameter, add hard code to existing html file and save it as new file.
For example, the original html file:
Code:
<html>
<body>
this is for test.
</body>
</html>

Php program generates:
Code:
<?php
...
for ($i=0;$i<5;$i++) {
//add $i to html file
}
...
?>

I want to save the html files as:
//file test0.php
Code:
<?php
value='0'; //when $i=0
?>
<html>
<body>
...
</body>
</html>

//file test1.php
Code:
<?php
value='1'; //when $i=1
?>
<html>
<body>
...
</body>
</html>

//file test2.php
Code:
<?php
value='2'; //when $i=2
?>
<html>
<body>
...
</body>
</html>

"value" in test*.php is hard coded, and saved as a specific file.


thanks.
 
Hi,

Well, the point of PHP is the ability to make (serverside) dynamic pages (amongst other cool things).

So your sample files with different names may as well be one page (test.php) displaying different things depending how you call it (parsing parameters). Have a look at my example again. That illistrates how one sigle page can look in three different ways.

Hope this makes sense. If not -be a bit more specific.

Best Regards


Jakob

PS. Going to Florida on holiday so I probably won't check in for a week from today...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top