Jump to content

[SOLVED] generate webpage with php code ?


anatak

Recommended Posts

Hello,

 

I am trying to generate a webpage with some php code in it by using a php script.

the problem is that the php code is not displayed when I look at the generated page.

 

here is an example

 

 

$string='
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<link rel="stylesheet" href="stylesheet/one_column_style.css" type="text/css" />
<link rel="stylesheet" href="stylesheet/text_style.css" type="text/css" />
<title>'.$site_name.'</title>
</head>
<body>
<div id="wrap">
<div id="header"><h1 class="center">'.$site_name.'</h1>
<div id="hori_navcontainer">
<ul>
<li><a href="'.$_SERVER[php_SELF].'?p=p01">Home</a></li>
<li><a href="'.$_SERVER[php_SELF].'?p=p02">Contact</a></li>
<li><a href="'.$_SERVER[php_SELF].'?p=p03">For Sale</a></li>
</ul>
</div> 
</div>
<div id="main">';
//random picture
//language link with graphic
$string=$string.'
<a href="html_eng/home.html">Welcome to '.$site_name.'<br /><img src="picture/900_SS.jpg" /><a/><br />	
<a href="html_fra/home.html">Bienvenu a '.$site_name.'<br /><img src="picture/900_SS.jpg" /><a/>

</div>
<div id="footer">footer text
<?php echo "test on php"; ?>
</div>
</div>

</body>
</html>';

$index_html_path=$public_html_path.'/index.php';
$file_handle = fopen($index_html_path, "w");
fwrite($file_handle, $string);
fclose($file_handle);
?>

 

The problem is that

<?php echo "test on php"; ?>

isn't doing anything. I was hoping to be able to create a page with some php code in it. The idea was to avoid all the database connections but still to use some scripts like showing a random picture.

 

Another thing I can not seem to do is to include a file.

example I have a navigation bar that I used to keep in a separate file.

including the file with an include(filename) does not work either.

 

Can someone tell me how I could generate a page with php code in it ?

 

Link to comment
Share on other sites

Remember: php is a SERVER-SIDE language, NOT client-side, like javascript.

It means that PHP scripts GENERATES html/xml/js/etc outputs.

You can't embed PHP code directly on your html code to run it on client-side (browser).

 

It means that if you make something like this:

 

<p><?php echo "Hello World!"; ?></p>

 

the server will interpret and process your php code BEFORE send data back to the client (browser).

 

The visitor will see on source code:

<p>Hello World!</p>

 

He can't see php codes on source code because the server does not send it to the client.

 

You can make things like this:

 

<?php
echo '<p>A simple counter:</p>';
for ($i = 0; $i < 10; $i++)
{
    echo 'Number '. $i .'<br />';
}
echo '<p>End counter!</p>';
?>

 

Got it?

Link to comment
Share on other sites

The problem is that

<?php echo "test on php"; ?>

isn't doing anything.

 

What do you mean by "isn't doing anything"? Do you mean that you see the php tags on the screen? If so, you have one of three problems:

 

1) Your server isn't set up to process php

2) You are using the wrong file extension (try .php)

3) You are looking directly at the document, and not passing it through a server. PHP documents have to be passed through a server with php installed/enabled on it, in order to process the php. If you put some php in a document then open that document up in your browser, it won't work.

Link to comment
Share on other sites

hmm

I guess I have been drinking some weird stuff.

Now the "test on php" string is showing in the webpage when I open it in my browser

so that is one problem solved.

 

in hindsight maybe I was looking at the page from my cache before I put in that little piece of code.

 

Thank you for helping me out.

I will without a doubt be back with more stupid questions.

I am going to try to include some  text using include("filename.inc") and will see how that works out because that was the original idea.

I could not get to work so I tried the <?php echo "test on php"; ?>

thing to test.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.