Jump to content

Less PHP pages


Ne.OnZ

Recommended Posts

Hello, I was wondering if there is any way to do this. I have this content box "<div id="content">Stuff here</div>". I was wondering when a user clicks a link I can just have "include(page.php)" go to that "content div", so I don't have to make multiple pages having the same exact code over and over.

 

Did I even make sense lol?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/
Share on other sites

It did make sense, and it will work. This is actually what wikipedia does for their content pages.

 

However, if the content is different in substance, I suspect (but don't know for sure) that you may take a hit on your SEO ratings. Search engines will see the content as existing on the same page and therefore won't index it as separate content.

 

You can get around this however by using mod-rewrite (which is what wikipedia does). If one page's content is www.yoursite.com/index.php?content=main, and another page's content is www.yoursite.com/index.php?content=about_us, then you set up the mod rewrite so that when you access:

 

www.yoursite.com/main

 

it shows the user the content of www.yoursite.com/index.php?content=main, and when you access

 

www.yoursite.com/about_us

 

it shows the user the content of www.yoursite.com/index.php?content=about_us. In this way, search engines will index the pages as entirely separate pages, even though they are actually both contained within index.php

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-527574
Share on other sites

www.yoursite.com/index.php?content=main

 

I'm sorry, I'm still fairly new to new php and only have written a couple of scripts. But why is there a "?content=main". I understand what your saying and I've seen these types of urls on many websites. I've never knew what it was called or apparently, how it works; so I never knew what to search for to learn it.

 

I'm so noob D:! Thank You.  :) :) :)

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-527577
Share on other sites

Lets look at the two addresses I gave earlier to show how $_GET variables work:

 

www.yoursite.com/index.php?content=main

www.yoursite.com/index.php?content=about_us

 

The key here is the content=_______

 

Both of these links post at the same script - index.php. But we want index.php to be able to show two different pages. The two different pages are main, and about_us. So the first thing we want to do in the index.php script is to check to see which page it should be showing. The page it should be showing is in the url (after content=). So we need to 'get' the information from the URL. We can do that like this:

 

$page_name = $_GET['content'];

 

You can see that I have used $_GET['content'] here, and content is in the URL. So $_GET['content'] will be equal to whatever comes after 'www.yoursite.com/index.php?content='. So after executing the page above, $page_name will be equal to either 'main' or 'about_us'. From there we execute the code in our script based on the value of $page_name:

 

$page_name = $_GET['content'];
if($page_name == 'main')
{
   // print out the stuff that goes on the 'main' page
}
elseif($page_name == 'about_us')
{
   // print out the stuff that goes on the about_us page
}

 

You can make as many pages as you want this way - you just add a new 'elseif' statement to check for each page name. All the pages exist in the script 'index.php', but the content will change depending on the value after 'content=' in the URL.

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528266
Share on other sites

Place this code in index.php


<?php $page = $_GET["page"];
if (!$page) {
include "/home.php";  // THE DEFAULT PAGE A.K.A index.php
}

else if($page=="Home")                     { include "/home.php"; } // THE INDEX.PHP?PAGE=HOME PAGE A.K.A HOME.PHP

else if($page=="About")                     { include "/about.php"; } // THE INDEX.PHP?PAGE=ABOUT PAGE A.K.A ABOUT.PHP

else { echo "<b><h1>404 Error</h1></b>"; }  // ERROR IF THERE IS NO PAGE
?>

 

 

 

sorry if i didn't explain to you properly so...

 

<a href="index.php?page=Home">home</a> will show you Home.php

<a href="index.php?page=About">About</a> will show you About.php

 

 

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528396
Share on other sites

Thank You for explaining. So I'm guessing, since I don't really have time to try it out yet, is I put that code in the content div showed on the site. If the page is about_us; I just put <?php include(about.php) ?>. And same goes for the rest of the links.

 

Still 1 thing I didn't understand, forgive me if I missed it in your explanation, but for the links "<a href="about.php">About</a>; would it still be about.php? Or would I have to put something else in "href"?

 

Thank You.  :) :) :)

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528398
Share on other sites

Place this code in index.php


<?php $page = $_GET["page"];
if (!$page) {
include "/home.php";  // THE DEFAULT PAGE A.K.A index.php
}

else if($page=="Home")                     { include "/home.php"; } // THE INDEX.PHP?PAGE=HOME PAGE A.K.A HOME.PHP

else if($page=="About")                     { include "/about.php"; } // THE INDEX.PHP?PAGE=ABOUT PAGE A.K.A ABOUT.PHP

else { echo "<b><h1>404 Error</h1></b>"; }  // ERROR IF THERE IS NO PAGE
?>

 

 

 

sorry if i didn't explain to you properly so...

 

<a href="index.php?page=Home">home</a> will show you Home.php

<a href="index.php?page=About">About</a> will show you About.php

 

 

 

Oh you posted right when I did. Just one question I asked in previous post, can I post the code in my content div? So I wouldn't have to touch the top part of my page?

 

Thank You

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528399
Share on other sites

Still 1 thing I didn't understand, forgive me if I missed it in your explanation, but for the links "<a href="about.php">About</a>; would it still be about.php? Or would I have to put something else in "href"?

 

Thank You.  :) :) :)

 

Yeah,

index.php?page=about and about.php will show one thing, the content in about.php .

 

edit:

Oh you posted right when I did. Just one question I asked in previous post, can I post the code in my content div? So I wouldn't have to touch the top part of my page?

 

Thank You

Yep.

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528400
Share on other sites

Sorry for so many questions D:! But, how does the page know its name? Meaning how does the Home link know the page name is Home?

 

....
else if($page=="Home")                     { include "/home.php"; } 
....

 

' $page=="Home" ' means that if page=Home

' { include "/home.php"; } ' means to include the file home.php

 

therefore, if page=Home, include the file home.php

 

edit: if you're new to php and don't know that i just made spaces in the php code, this is what it could look like...

 

else if($page=="Home"){
include "/home.php"; 
} 

 

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528404
Share on other sites

Aye topic not solved :o! I was called out from work, so I tried it out. And I got loads of errors. Except for one link. It actually says 404 Not Found. If you go to the site: http://www.divnx.net/ and click any click, you will see all the errors. If you click under Guides/C++, it will say 404. None of the pages exist, so it should say 404 on all of them if I'm correct.

 

Here's the page code:

<?php

$page_name = $_GET['content'];

if($page_name == 'Home') { 
include("/index.php");
}
elseif($page_name == 'Register') { 
include("/create.php");
}
elseif($page_name == 'Site_Map') {
include("/map.php");
}
elseif($page_name == 'Members') {
include("/members.php");
}
elseif($page_name == 'C++') {
include("/guides/c++.php");
}
elseif($page_name == 'Java') {
include("/guides/java.php");
}
elseif($page_name == 'PHP') {
include("/guides/php.php");
}
elseif($page_name == 'CSS') {
include("/guides/css.php");
}
elseif($page_name == 'HTML') {
include("/guides/html.php");
}
elseif($page_name == 'Games') {
include("/games.php");
}
elseif($page_name == 'Settings') {
include("/account/settings.php");
}
elseif($page_name == 'Stuff') {
include("/account/stuff.php");
}
elseif($page_name == 'Profile') {
include("/account/profile.php");
}
elseif($page_name == 'Contact') {
include("/support/contact.php");
}
elseif($page_name == 'FAQ') {
include("/support/faq.php");
}
elseif($page_name == 'Staff') {
include("/support/staff.php");
}
elseif($page_name == 'Donate') {
include("/support/donate.php");
}
else {
echo "404 Not Found!";
}

?>

 

Thank You.  :o :o  :)

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528431
Share on other sites

<?php

 

$action = $_GET['action'];

 

switch($action){

case 1:

include('pages/1.php');

break;

case 2:

include('pages/2.php');

break;

case 3:

include('pages/3.php');

break;

case 4:

include('pages/4.php');

break;

case 5:

include('pages/5.php');

break;

case 6:

include('pages/6.php');

break;

case 7:

include('pages/7.php');

break;

case banipadd:

include('pages/banipadd.php');

break;

case banipadd1:

include('pages/banipadd1.php');

break;

case banuser:

include('pages/banuser.php');

break;

case banuserproc:

include('pages/banuserproc.php');

break;

}

 

 

?>

 

This is what I used on a site, and it worked fine.  the case needs to be the same as the page.

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528435
Share on other sites

if($page_name == 'Home') {

include("/index.php");

}

elseif($page_name == 'Register') {

include("/create.php");

 

These would be

 

<?php

 

$action = $_GET['content'];

 

  switch($action){

      case index:

        include('/index.php');

      break;

      case create:

        include('/create.php');

      break;

}

?>

Just keep going down the line for the other pages.

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528446
Share on other sites

Destinations:

 

Anything under Guides has "/guides/linkname.php"

http://www.divnx.net/guides/c++.php

 

Anything under Account has "/account/linkname.php"

http://www.divnx.net/account/settings.php

 

Anything under Help has "/support/linkname.php"

http://www.divnx.net/support/contact.php

 

Here's the code to index if you need it:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head>
<title>DivnX :: Home</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/divStyle.css" />
<script src="javascript/navigation.js" type="text/javascript"></script>
</head>

<body>

<div id="gradient">
      <div id="main">
             <div id="banner">
                  <img src="images/Header.png" alt="DivnX" />
             </div>
             <div id="logon">
                  <?php include("includes/Login.php"); ?>
             </div>
      </div>
</div>

<div id="content">
      <div id="nav">

<ul id="menu" style="text-align: center">
                  <li>Site
                        <ul>
                            <li><a href="index.php?content=Home">News</a></li>
                            <li><a href="index.php?content=Site_Map">Site Map</a></li>
                            <li><a href="board/index.php">Forums</a></li>
                            <li><a href="index.php?content=Members">Members</a></li>
                        </ul>
                  </li>

                  <li>Guides
                        <ul>
	<li><a href="index.php?content=C++">C++</a></li>
	<li><a href="index.php?content=Java">Java</a></li>
	<li><a href="index.php?content=PHP">PHP</a></li>
	<li><a href="index.php?content=CSS">CSS</a></li>
	<li><a href="index.php?content=HTML">HTML</a></li>
          </ul>
                  </li>

                  <li>Games
                        <ul>
	<li><a href="index.php?content=Games">Coming Soon!</a></li>
          </ul>
                  </li>

                  <li>Account
                        <ul>
	<li><a href="index.php?content=Settings">Settings</a></li>
	<li><a href="index.php?content=Stuff">My Stuff</a></li>
	<li><a href="index.php?content=Profile">Profile</a></li>
          </ul>
                  </li>

                  <li>Support
                        <ul>
	<li><a href="index.php?content=Contact">Contact Us</a></li>
	<li><a href="index.php?content=FAQ">FAQ</a></li>
	<li><a href="index.php?content=Staff">Staff</a></li>
	<li><a href="index.php?content=Donate">Donate!</a></li>
          </ul>
                  </li>
</ul>

      </div>

      <div id="stuff">
            <?php include("includes/Page.php"); ?>
      </div>

</div>

</body>
</html>

 

Thank You  :)

Link to comment
https://forums.phpfreaks.com/topic/102985-less-php-pages/#findComment-528461
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.