Jump to content

Using PHP to make my website dynamic


doubledee

Recommended Posts

Hello everyone.  This is my first post, so be nice!

 

I am building a website that will have a lot of content, similar to a newspaper.  I have some pretty good HTML/CSS pages written, but the problem is that I need a way to make things more dynamic.

 

One of my templates has a Header, Left Column, Middle Column, Right Column, and Footer.  Everything stays the same from page to page except for the Middle Column (which holds each article).

 

As it stands now, if I had 12 articles, I would have to have 12 nearly duplicate HTML pages which isn't good!

 

I started studying PHP a while ago, but put that on hold to learn HTML/CSS, so I've kinda forgotten how PHP can help me out!  :-\

 

Can someone help me figure out how to use PHP to my benefit?

 

Thanks,

 

 

 

Debbie

 

Link to comment
Share on other sites

 

 

You are looking for the "include"-function in PHP.

 

By making a .php file for all of the columns (except from the content) you can easily include the columns into all of the different pages.

This will save you a lot of time editing, since you only have to edit ONE file instead of TWELVE.

 

Example for index.php:

include('page_header.php');   // THIS INCLUDES THE HEADER
include('page_column_left.php');  // THIS INCLUDES THE LEFT COLUMN

// HERE YOU CAN WRITE THE CONTENT OF THE CURRENT PAGE (UNIQUE ON EACH DIFFERENT PAGE)

include('page_column_right.php');  // THIS INCLUDES THE RIGHT COLUMN
include('page_footer.php');    // THIS INCLUDES THE FOOTER

 

Redo this for all of your pages basically, and you have saved some odd hours of updating.

 

Note: Use a database instead if you know how to use one. Faster, easier and more convenient.

 

 

Good luck!

Link to comment
Share on other sites

You are looking for the "include"-function in PHP.

 

Yes, I remember those.

 

 

By making a .php file for all of the columns (except from the content) you can easily include the columns into all of the different pages.

 

I don't have to break things up that much, do I?

 

Couldn't I just split the template into two parts:  "CodeBeforeNewsArticle.php" and "CodeAfterNewsArticle.php"?

 

OR

 

Couldn't I have my template "StandardTemplate.php" and then have an include inside that template.  Then I just change what is included?  :shrug:

 

 

This will save you a lot of time editing, since you only have to edit ONE file instead of TWELVE.

 

Yes, that is the goal!

 

 

Note: Use a database instead if you know how to use one. Faster, easier and more convenient.

 

 

Good luck!

 

Why is it easier using a database?

 

I used to know some MySQL, but forgot it with PHP!  :-\

 

 

 

Debbie

 

 

Link to comment
Share on other sites

I could stick toothpicks in my eyes as well, but I prefer not to...  ;)

 

Too funny.

 

Good luck

 

PS; I ditto nicholasolsen

 

I have something like this, I did as nicholasolsen said to do on the front page. For the two column sub pages I commented out the left-column and made the middle column wider with a 2nd css. On about 10 sub pages(which I use for categories) info is retrieved with a fixed sql statement and on about 200 pages under them info is retrieved with get variables. If I new what I know now when I built it I would have not had fixed pages(or sub files, they add work) but I would have gotten everything with get variables.

 

Link to comment
Share on other sites

A read of this thread should get you started.

 

I looked at the link, but it would help to talk about my specific problem.

 

Thanks,

 

 

Debbie

 

 

There is a reply (by me) in that specific thread that describes the basic of dynamic web sites. If you don't understand something, tell us, your not going to get a tutorial written especially for you.

Link to comment
Share on other sites

There is a reply (by me) in that specific thread that describes the basic of dynamic web sites. If you don't understand something, tell us, your not going to get a tutorial written especially for you.

 

No need to be rude about it.

 

I never said go write a tutorial for me?!

 

I was just saying that having a conversation about my specific issue would better help me understand how to tackle things.

 

 

Debbie

 

 

Link to comment
Share on other sites

Hello Debbie,

  First off I want to ask you why you dismissed the use of wordpress out of hand?  I'm no great fan of wordpress, however, based on your description it sounds like it offers everything you are asking for out of the box.  The only effort involved (and I understand that it is non-trivial for someone who doesn't understand php or programming) is to translate your design into a wordpress template.

 

Second, Nicholas pretty much gave you the bare-bones minimalist answer to your question in terms of the mechanics, but what I'd suggest is to implement a simple controller that takes a single url parameter of article.

 


www.yoursite.com?article=your_article_title

 

So what you would do at that point is take your site and create a master template.  This will be 99% the existing markup you have.  You can start by making a copy of the article html you have and renaming it to .php.

 

One of the great things about php is that you can intermix php and html by dropping in and out of php by putting the start and end tags where you need them.  So rather than splitting everything into its own seperate include file you can instead just have the article section inside a php block.

 

// your existing markup

  $article = basename($_GET['article']);
  $basepath = '/path/to/articles/';
  $baseext = '.html';
  $filename = $basepath . $article . $baseext;
  if (file_exists($filename)) {
     include($filename);
  } else {
     // Someone tried to screw with your system or there is a bad link.  You might want to return a 404 error or show a default article
  }
?> 

... the rest of your markup

 

 

This code has a few basic security measures built in and assumes that you will name are your articles as "title.php" or "title.html".

 

 

Link to comment
Share on other sites

Hello Debbie,

  First off I want to ask you why you dismissed the use of wordpress out of hand?

 

Same reason I wouldn't pay someone to go get pregnant for me and raise a kid for me.

 

I want to learn how to build my own website from scratch.

 

 

Second, Nicholas pretty much gave you the bare-bones minimalist answer to your question in terms of the mechanics, but what I'd suggest is to implement a simple controller that takes a single url parameter of article.

 

And I was saying it would help more if we can talk about where I am confused.

 

I never said I wanted someone to go write my app for me.  (That's for WordPress people.)

 

I am hoping to find people who want to help me improve what I already built from scratch.

 

If you (or anyone else) has time for Q & A then I have lots of questions that will help me learn how to do this.

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

I forgot to add that you would name this "controller" index.php, so that it is the default page for your site.

 

Thanks for the code, but we really need to discuss where I am confused before getting into code.  (Thus why Thorpe's response wasn't all that helpful just yet.)

 

 

Debbie

 

Link to comment
Share on other sites

Ok, so it's great to see that you're interested in learning how to build your own site technology from the ground up.

 

In terms of what I provided, I just took what nicholas offered and addressed your question, throwing in my own 2 cents.  I'm not sure if there's a clear outstanding question based on this reply chain. 

 

What I offered allows you to:

 

- have all your existing html + css work in a file that is mostly what you already had

- Have separate articles in .php or .html pages (my example assumes .html pages that can be updated as simply as adding a new one to your site.

- do so with a modicum of security.  If you have any questions about any of that please feel free to ask

 

Any functions i used can easily be reference by putting in www.php.net/functionname

 

 

 

 

 

 

Link to comment
Share on other sites

Ok, so it's great to see that you're interested in learning how to build your own site technology from the ground up.

 

Since you keep replying, I'll assume you want to help.  (Plus you're friendly!)  ;)

 

Starting over...

 

My website is intended to provide news and information.  One of the templates that I created from scratch looks like this...

 

There is a Header which has the company name, log in info, and a my account link.  Below that (still in the header) is a nifty Top Menu Bar with Drop-Down Submenus.  It looks very similar to the one on the home page of the http://www.chicagotribune.com/  Below that is a Right Column which holds advertising and never moves or changes.  To the left is a Middle/Main Column which is where content goes (including articles).  Finally, there is a Footer at the bottom.

 

Let's say a visitor lands on this template - which is currently hard-coded.  She browses the top menu, and hovers over "Business".  A drop-down appears, and she selects "Chamber of Commerce". (Business>>Chamber of Commerce)

 

In the "Middle Column", I want a "sub-page" to appear that has contact information on the Chamber of Commerce.  (Everything else on this page should remain the same as described above.)

 

Next, let's say she navigates to Business>>Better Business Bureau.

 

In the "Middle Column", I want a "sub-page" to appear that has contact information on the Better Business Bureau.  (Everything else on this page should remain the same as described above.)

 

 

That is ONE simple example of how I am trying to convert my website from a bunch of static pages to using templates that can populate "sub-pages" with different, dynamic content.

 

Because I am super rusty on PHP, and because the goal is to *quickly* improve the maintainability of my code/website, I'd like to start with baby steps.  (So, coming up with some elaborate object-oriented solution is overkill at this point.)

 

If I can get this so my "Template" and say 15-20 Articles/Content Pages are separate, that would be a big improvement.  Then as things evolve, maybe I can look at writing fancier code and even using a database.

 

Hope that helps explain better what I am trying to do and where I am confused.

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

What I provided is still my answer to your question ;)

 

The only thing I think that would be of further help would be for you to post the html you have, and have me take that and insert the code and then have you run it on your own site.  Not having seen any of your current markup, I made educated guesses as a placeholder for what you might actually have.  I used "article" as the parameter, but you could use page if that makes you more comfortable.

 

In order to understand what I suggested you do need to understand url's and in particular url parameters.  PHP's mechanism for giving you access in your php script to url parameters is the $_GET superglobal array. 

 

I do realize you are a self admitted rank beginner here, so I provided minimal code that does not involve oop or mysql or anything like that -- purely some simple file handling and include(). 

 

Include basically does what the name suggests.  If you have a file and you include a file, it is as if you had the contents of the file, and at the point where the include() happens, you will have a file exactly as if someone had copy/pasted the included file into it at that point. 

 

 

Link to comment
Share on other sites

Thanks for trying to help, but can we step back for a moment and discuss things outside of coding?

 

Like what exactly?

 

I'd like to talk about things at a conceptual level before diving into code.  (Sorta like reading the manual BEFORE operating a chainsaw...)  ;)

 

Here is a link to a simple mock-up that I made...

 

http://doubledee.byethost2.com/index_dd.html

 

 

That page is currently hard-coded, yet most of that page won't change (e.g. Header, Left & Right Column and Footer).  So if I have 12 pages, then I need to copy most of that code 12 times.  And if I make the slightest change in one (e.g. change top menu to purple), then I'd have to change it in 12 places?!  :o

 

Let's say a user goes to my site and from the menu chooses Business>>Jobs.  I want the middle column to be dynamically populated with the article "Current Job Openings".

 

And if the user then goes to Business>>Investing, then I want the middle column to be dynamically populated with the article "Things to know before you invest!".

 

And so on...

 

Of course my website will be much more complicated than that, but I figure that is a simple way to test going from HTML to PHP (possibly using "includes")?!

 

I would like to understand conceptually what is the best way to dissect my website and come up with some different architectural/coding approaches.

 

My goal is to get my website done in short order, yet make it practical enough in its design to be able to maintain it.  (Down the road I can get into advanced topics like object-oriented programming and all that, but for now, I'd like to move beyond static HTML pages - most of which have 70% the same code - and create a more "dynamic" and "manageable" website!)

 

Hope that makes more sense...

 

 

Debbie

 

 

Link to comment
Share on other sites

Before you continue further with the coding of dynamic elements, for accessibility and to adhere to modern html markup standards. (XHTML is now depreciated) I would recommend you change your doctype to <!Doctype HTML> and then replace:

 

<div id="header"></div>

with

<header></header>

 

and..

 

<div id="footer"></div>

with

<footer></footer>

 

then style the <header> and <footer> elements in css accordingly.

 

as far as the php goes, I would really recommend sitting down with a nice book (http://www.amazon.com/Best-PHP-related-books/lm/2EUGZVDI09X47), read up on some tutorials (http://www.tutorialized.com/tutorials/PHP/1), and browse the php manual for specific functions you have issues with(www.php.net). And then ask further questions here as you come across them.

 

It may take a lot of work, but I would recommended looking in to OO PHP (Object Oriented PHP). It is a much better solution for a very dynamic news site. Especially when you want to include user interaction such as comments.

 

I wish you the best of luck with your project. If you have any questions, ask away. But key thing here be as specific as possible, you will get much better responses.

 

 

ps: I am not sure if this is meant to be functional, but the "indicates minimum width" in the center column does not appear to be working for me on the Google Chrome browser.

Link to comment
Share on other sites

Debbie,

  You have repeated the same question 3 times. 

 

Did you *read* my first reply?  Did you read my followup?  There is nothing to talk about in the abstract.  We get it.. you have a menu and you want the content div to change.  Are you interested in how to do that?  If so, I provided the answer for you.  It does not require a database and could be working for you in about 10 minutes time.

 

Summary:

 

1 php script  "Index.php".  Contains the header/footer/sidebards etc.  + small block of php that loads the content div using a url parameter.

 

Other scripts/pages only need to have the content that goes into main section. 

 

That is the answer to your question in regards to how to do what you are asking for. 

 

No you won't have 18 redundant pages.  You have 1 that contains your layout and the logic to be able to load in the appropriate content page.

 

Code for index.php

br />	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">



3 equalising column min-max with sticky footer












News

Breaking
Local

U.S.
World


Politics

Local

White House
Congress
Gov't
Elections


Business


Economy
Markets
Investing
Small Biz
Jobs

Real Estate


Sports

NBA
MLB

NFL
NHL
NCAAF
Golf


Travel


U.S.
S. America
Europe
Africa
Asia

Australia
Tips


Weather

Current

Forecast
U.S.
News
Records


Opinion


Debbie D.
Frank S.
Suzy Q.
Jane D.
Staff

Reader Comments









				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :
				Other content goes here : Other content goes here : Other content goes here :




			$article = basename($_GET['article']);
			$basepath = '';
			$baseext = '.html';
			$filename = $basepath . $article . $baseext;
			if (file_exists($filename)) {
				include($filename);
			} else {
				// Someone tried to screw with your system or there is a bad link.  
			}
			?> 					





				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :
				Adverting goes here : Adverting goes here : Adverting goes here :










footer





 

Note the url for the Breaking news link.  That is the only one I changed for the purpose of demonstrating how this works.

 

new file named "breaking.html"

 

This is breaking news

Today we coded up some php.

 

Put this in the same directory as the index.php, which should be in the same directory as your original html file.

 

Click on Breaking news link... see content.

 

 

 

 

 

 

 

 

 

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.