Computer Hope

Internet & Networking => Web design => Topic started by: Dread on November 26, 2009, 11:52:51 PM

Title: PHP common code for pages
Post by: Dread on November 26, 2009, 11:52:51 PM
I'm working locally with the latest WAMP Server on Windows. I understand the include concept so we don't have to rewrite the code. But then we have to include that line in all the pages to have that effect for all the pages. For instance including the 3 php files header, left-nav and footer. Can I have pages with code ONLY for those page body and have the header, left-nav, footer included automatically for all the pages in a folder ? I'm not sure if this is possible locally but at least is that possible for a domain hosted in a web-server (like in yahoo) & how ?
My apologies if this doesn't make any sense, let me know ....
Title: Re: PHP common code for pages
Post by: kpac on November 27, 2009, 12:32:58 PM
Quote
Can I have pages with code ONLY for those page body and have the header, left-nav, footer included automatically for all the pages in a folder ?
Sorry, but here you lost me. ;D

If you can explain it a bit more I'll be happy to help if I can.
Title: Re: PHP common code for pages
Post by: Dread on November 28, 2009, 09:15:08 PM
I have the files header.php, left-nav.php, footer.php. I would ideally include these files in the other pages, page1.php & so on to page100.php. I kind of hate the idea of writing code to include files in all these 100 pages. Is there a better way to automatically include files to all the page1...100.php ? Does that make any sense ?
I appreciate your help.
Title: Re: PHP common code for pages
Post by: kpac on November 29, 2009, 04:53:45 AM
Yeah, it makes sense now.

Unfortunately there is no other way to include the files in all the pages. The easiest thing to do now would be to copy and paste the line so you don't have to type.
Title: Re: PHP common code for pages
Post by: Dread on November 29, 2009, 11:21:22 PM
So that means everyone in the web development is writing the include code for the header/left-nav/footer in every single page of the 100s or 1000 page site ?? Well, that's something not cool. I guess that's how it is now. Thanks for your response.
Title: Re: PHP common code for pages
Post by: BC_Programmer on November 30, 2009, 02:24:54 AM
Normally when you have 100's or 1000\s of pages you can eliminate the different content and move it into a database of some sort. Then use page parameters to select which record to show.

Either that, or isolate separate content into each of the 100 or 1000 pages and use the same PHP file to selectively include them based on page parameters.
Title: Re: PHP common code for pages
Post by: Dread on November 30, 2009, 09:25:12 PM
... move it into a database of some sort. Then use page parameters to select which record to show.

Well, do have the MySql DB .. and all the pages would have the same header,left-nav and footer. Wouldn't I still have to include the file with the dynamic code in all the pages ??
Title: Re: PHP common code for pages
Post by: BC_Programmer on December 01, 2009, 02:54:58 AM
No. BEcause you wouldn't have separate pages.

Say- for example, the pages were all articles. What would be done is the articles would be stored in database; and, using page parameters, accessed based on a id or key of some sort. The "articleText" or similar Database field would be inserted in a specific location of the page- that is, the php file would incude() all the appropriate left, top, right, etc and what have you includes, the HTML for formatting the article text (table? span tag? who knows) the pages would all be the same except for the article content; and while there would sort of be multiple "pages" there would only be ONE file.

Title: Re: PHP common code for pages
Post by: kpac on December 01, 2009, 09:36:14 AM
Here's an example for an articles section.

Code: [Select]
<?php

// Retrieve the article id from the location, e.g. articles.php?article_id=XXX
$articleid $_GET['article_id']

// Connect to database
$con mysql_connect($db_server$db_user$db_pass);
if (!
$con)
{
die('Could not connect: ' mysql_error());
}

// Select DB
$db_selected mysql_select_db($db_name$con);
if (!
$db_selected
{
die('Can\'t use DB: ' mysql_error());
}

// Generate MySQL query
$result mysql_query("SELECT * FROM articles WHERE article_id='$articleid'");
if(!
$result)
{
die("Can't execute query: " mysql_error());
}

// Include header, leftnav whatever
include(dirname(__FILE__) . "/header.php"); // ETC.

// As long as the article id matches the DB, do this.
while($row mysql_fetch_array($result))
{
        echo 
$row['article_content'];
}

// Include more
include(dirname(__FILE__) . "/footer.php");

?>

Title: Re: PHP common code for pages
Post by: Dread on December 01, 2009, 04:24:17 PM
Thanks guys, i'm going to try it out & let you know how it goes .. you guys are awesome!