Forum Moderators: coopster
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?
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'";)
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.