NameSilo

Using includes?

Spaceship Spaceship
Watch

Host47

Established Member
Impact
27
I have a website that I have sliced and out to html, then renamed to php (lazy way i know) in the centre i use php include to take the news from the forums and display it in centre of home page...how do i make the buttons do the same, i.e if i click templates a new page opens where the news once was and so on...please don't say iframes my only options...
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
You could do something like this (pretty much what i use, although a little less complicated);

For navigation links:
index.php?page=somepage

Then for the actual including code do something like this

PHP:
if (isset($_GET['page']))
{
$includes = $_GET['page'];
}

else if (!isset($_GET['page']))
{
$includes = 'news';
}

include($includes . '.php');

This basically asks if the page in the url is set then use that page, if not then use the news page.

Hope it helps.
 
0
•••
Then for the actual including code do something like this


if (isset($_GET['page']))
{
$includes = $_GET['page'];
}

else if (!isset($_GET['page']))
{
$includes = 'news';
}

include($includes . '.php');

Where do i put the above code, sorry for asking a dumb question, ths is my 1st time :P.

p.s how much would you charge to do 1 link for me so i can see how it's done....
 
0
•••
I can do it for free.

Do this;

PHP:
<?php
if (isset($_GET['page']))
{
$includes = $_GET['page'];
}

else if (!isset($_GET['page']))
{
$includes = 'news';
}

include($includes . '.php');
?>
Put that code there wherever you want the page to appear.

Assign each link like this (these are examples);
home = index.php?page=home
news = index.php?page=news


Hope that helps.
 
0
•••
It's not as good, but you could also do just simple HTML pages for each of the pages, and just put a different include in each page for the content.. But Liam's way is MUCH better. I've been meaning to do this myself for all of my websites... But I'm far too busy.

Rep Added Liam :)
 
0
•••
Using switch(); is another option..
PHP:
<?php

//Example link <a href="?page=blah">Blah</a>
$page = trim(strip_tags($_GET['page']));

switch($page)
{
  case 'blah':
       include("blah.php");
       break;
  case 'contact':
       include("contact.php");
       break;
  case 'links':
       include("links.php");
       break;
  default:
       include("home.php");
}
?>
 
0
•••
Which is better? Switch or Liam's code?
 
0
•••
They both work, but switches code makes a tedious task of adding in more pages as you have to add in a new case each time. With mine you don't need to add anything.

And glad i could help :)
 
0
•••
With liam's code, however, there is a possibility that someone could do something to the effect of 'http://www.yoursite.com?page=http://www.someonessite.com/theirscript' and execute code on your page.

I would suggest either using switch() or using this code insteaed:
Code:
<?php
if(isset($_GET['page']))
  if(file_exists("./pages/" .$_GET['page']. ".php"))
    include("./pages/" .$_GET['page']. ".php");
  else
    include("./pages/404.php");
else
  include("./pages/default.php"); 
?>
 
0
•••
Yeah it is always a good idea to make sure you check the pages.

Can also be done with an array, check if the page is an an array of files you set.
 
0
•••
oh man, my head hurts, liam sent me a page with the code but it doesn't work....so i'll reg 2 domains at namecheap to anyone that can add the links to the index.php for me, whilst retianing the main loadpage as the news from the forums...
 
0
•••
I have told you how to do it, i have done it for you and testing it and it works fine!
 
1
•••
reaper:

Where you have the include already (the one you said you put in, in the very first post) just put in:

PHP:
<?php
if(isset($_GET['page'])){
  if(file_exists("./pages/" .$_GET['page']. ".php")){ // change "pages" to the name of the directory your news page and other stuff is
    include("./pages/" .$_GET['page']. ".php"); // change "pages" again
  }
  else{
    include("./pages/news.php"); // again "pages" and also change "news.php" to the location of the file you were including at the beginning 
  }
}
else{
  include("./pages/news.php"); // again "pages" and also change "news.php" to the location of the file you were including at the beginning
}
?>
... in its place
 
1
•••
all sorted now, liam domais regged and pushed..TR & rep appreciated.

Poordoggie, cheers, rep added anyways....
 
1
•••
Thanks a lot, glad i could help :)
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back