Forum Moderators: coopster

Message Too Old, No Replies

How would you PHP expert do the following

displaying information depending on page

         

le_gber

8:55 pm on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there,

at the moment I use php includes on one of my sites and wondered how to best display data that is only relevant to the page.

At present I have hard coded variables before the <html> opening tag and using these variables I use if() in the includes to show or hide some data (let's say I have one include for the left part that hold the whole data for the complete site left side and then only show the relevant info to the page).

I am thinking of expanding the site and wondered if it wouldn't be easier to have a single include checking the page url and use these vars as way of filtering.

How did you guys created fully dynamic sites?

StupidScript

12:33 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you're talking about showing or not showing HTML code depending on the page file name or are you passing variables to a template-type page using the URI or cookies or sessions?

With the first, you can pretty easily grab the file name out of the URI by using

$_SERVER['SCRIPT_NAME']
, and then continue with your
if()
statements down the page, hiding code
if()
it's not the right file name, like:

<?

$thispage=$_SERVER['SCRIPT_NAME'];

if ($thispage=="about.php") {

?>

<h1>About Us</h1>

<?

}

elseif ($thispage=="order.php") {

?>

<h1>Order Form</h1>

<?

}

elseif ... etc.

Better to use

switch()
, but you get the idea.

Similarly, you can grab a URI attribute by accessing the

$_GLOBAL
array or one of many other techniques, and do the same. Cookie data can be grabbed from the
$_COOKIE
array ... and so on.

Personally, I use URI attributes to trigger database activity that then populates the page using any number of variable bits of info, like:

ht tp://example.com/page.html?category=candy

Then I assign the URI attribute to a local variable and use it to get info from the database:

$qury=mysql_query("select * from sitepages where page = '$category'";)

le_gber

9:04 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yep it was for the first case - no db but simply showing / not showing html

cheers for that - I think I'll try to use one include per section of the site instead of one for the whole site - it will give me more flexibility and make it more manageable.

StupidScript

5:30 pm on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good idea. You could name the include files as you do the attributes for simplicity:

ht tp://example.net/page.html?this=about

[ header stuff and menus and whatnot ]

<?

$thispg=$_POST['this'].".html";

include $thispg;

?>

[ footer stuff and menus and whatnot ]

would include

about.html
between the headers and the footers.