Creating PHP File for Repeated Content

I’m not sure if this is where I should put it as I’m just new here. But I just want to know if it is possible to create a .php file for the repeated content? An example would be, if I want to change the promo date of a certain coupon in the index and would like that change to take effect to all of the pages of the website by only editing the index page, how can I do it? Is it possible to do it in a .php file?

What I want is to lessen the time of editing each page and just edit the .php file instead of going through all of the 50+ pages of the website.

If this is possible, how can I do that and which particular code should I use?

I hope this question makes sense. :slight_smile:

I really need help on this matter so any suggestions would be greatly appreciated.

I hope someone could help me on this. I really need to learn more about this as I’m trying to work on this page. I’m slowly learning PHP and I know that I still need to know more. This is somewhat complicated for me.

Just Break the page into chunks like : header,footer ,center ,a separate php page.
Include them to build a webpage, so u have change only these files.
u can also call a function to show the promocode on ur site ,where function will be in another php page.

Thank you rajeev13 for your fast reply. But may I know how I will be able to break the pages in chunks? What code should I use? I’m really new to PHP and I am really not that confident to work on these codes.

let say we have a index.php that shows the default page .look out below the sample code :
index.php


<?
session_start();
include_once('includes/headerData.php');
include_once('style.css');
?>

</head>
<body>
<?
include_once('includes/header.php');
?>
	<div id="center" > 
            
        </div>
      
      
      
   <? include_once('includes/footer.php');?>
	
	</body>
</html>
	

headerData.php


<html>
<head>
<script type="text/JavaScript"></script>
<link href='http://fonts.googleapis.com/css?family=Chela+One|Finger+Paint|Montserrat+Subrayada|Cherry+Swash&subset=latin,latin-ext' rel='stylesheet' type='text/css'>

header.php

<? 
session_start();

?>
<div id="header">
			<div id="h1">
                            <a href="index.php"> <h2 id="h1" align="center">Sample web page</h2></a>
			</div>	
 </div>  
 

footer.php




<div id="footer" align="center" >&#169 rajeev ranjan </div>
 
 
 	

What if there’s only index.html in the file? How should I start on the index.php file?

To keep this really simple, you could create a php page, say ‘updates.php’, with just the data you want to update regularly,e.g.


<?php
$promo_date = '15FEB13';
$other_item = 'other'; //etc. etc.
?>

Then in all the pages that require that info, add the line:


include('updates.php');

Then those variables and their values will be available in your other pages.

Thank you 2ndmouse for this very simple code and explanation. I kind of understand it now a little.

So on that second code, will I just put this code "include(‘updates.php’); " anywhere in each of the html page of the website? Of course in the location where I want that updates.php to appear in the webpage. Is that it?

No. To use php code in your pages, the pages must be page.php not page.html

For example:

If your index page is index.html you can add the following code above your html :


<?php
include('updates.php');
?>
<Your html here>

Then rename the page index.php
Your server should load that automatically if its directory is loaded in the browser.

You can use the included values in your html by inserting <?php echo $promo_date ?> where ever you need that value to appear.

Hope that helps

I renamed the index.html to index.php but it’s no longer opening. What wrong thing must I have done here?

Did you add the php code to the top of that page?

<?php
include('updates.php');
?> 

Yes I did but I’m not sure if I did it correctly. Here it is…

Seems your image will take a while for approval.

Your index.php file should look something like this:


<?php
include('updates.php');
?>
<html><head>
</head><body>
Your content
</body></html>

If that fails, create a file named phpinfo.php, insert the following code to that page:


<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>

Upload the page to your site and run it in your browser. You should see displayed all current PHP settings on your server. If you don’t get this displayed, then it probably means that PHP is not installed on your server.

I don’t know what happened but when I opened the homepage I got this message on my screen: Parse error: syntax error, unexpected T_STRING in /home/content/08/6277108/html/airconditioningokc/updates.php on line 6

I just followed your instruction but I still get errors. So how do I install PHP in my server?

That looks like a php error, so I guess php must be installed already. Did you run the phpinfo file yet?

The image of your code is still pending approval, so I haven’t yet seen your file contents. Can you paste your file contents between

 tags in your next post here, so that I can see what you have there.

Meanwhile, here's one to try:

First, create the file: updates.php with the code below and upload to your server.


```php
&lt;?php
$promo_date = '15 FEB 2013';
?&gt;

Then create a file: test.php and upload it to your server in the same directory as updates.php.

Code for test.php:

&lt;?php
include('updates.php');
?&gt;
&lt;html&gt;&lt;head&gt;
&lt;/head&gt;&lt;body&gt;
&lt;?php echo $promo_date; ?&gt;
&lt;/body&gt;&lt;/html&gt;

Then run test.php in your browser - it should display the promo date

Ok it displayed the promo date correctly. This is what showed in the test.php page:

15 FEB 2013

And this is what’s in that pending image:

<?php
include('updates.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

But I already changed it with:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
include('updates.php');
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

and then phpinfo file but this error message appeared:

Parse error: syntax error, unexpected T_STRING in /home/content/08/6277108/html/airconditioningokc/updates.php on line 6

The error is in file: updates.php

What have you got in that file?

Oh this was what’s in the updates.php file:

<?php
$promo_date = '15 FEB 2013';
$analytics_code = '<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-20639892-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>'
?>

I thought that I could also include the analytics code there. Because my goal was to also put the analytics code in every page through a php file so I thought this was ok.

Yes, that shouldn’t be a problem.

You’ve forgotten the semi-colon at the end of your string

</script>’
?>

should be:

</script>';
?>

That’s what php is complaining about.

Oh is that it? Ok let me try it again and update you. Thank you very much. I’m really learning here. :slight_smile: