• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Sorry....php question

<?
if ($page == "2") {
include("yourfile.php");
} elseif ($page == "3") {
include("anotherfile.php");
}
?>
 
To give it a name, I'd call it query string navagation, but I doubt you'd get any matches if you searched for it.

It's easy to do, and there are a million ways to do it.

The way I'd to it would be to have content in a mySQL table with an ID, and use the query string to pull the content by that ID.

That might be a little too advanced for some people so here is another option.

Create a template. No content. Make pages with your content and name them however you want to name them, by numbers or by name.

Then in your template where you want your content have:
PHP:
<?
if ($page) {
include "path/to/your/content/$page";
}
else { include "path/to/default/content"; }
?>
 
Originally posted by bigperm


Create a template. No content. Make pages with your content and name them however you want to name them, by numbers or by name.

Then in your template where you want your content have:
PHP:
<?
if ($page) {
include "path/to/your/content/$page";
}
else { include "path/to/default/content"; }
?>
1) Can the content be *.shtml?
2) What does else { include "path/to/default/content"; }
do?
 
Originally posted by The Red Guy
1) Can the content be *.shtml?
2) What does else { include "path/to/default/content"; }
do?
1) No. Not unless you modified your htaccess to parse shtml pages as php for some reason. PHP pages should be named *.php to be parsed.
2)That would be the path to what you want to be displayed if the file is called without and query strings.
 
PHP:
<?
if ($page) {
include "path/to/your/content/$page";
}
else { include "path/to/default/content"; }
?>
1) Then can I use php include instead?
2) path/to/your/content/$page - Must I replace the $page with my .php page?
3) Do you know where I can find this tutorial?
 
Originally posted by The Red Guy
PHP:
<?
if ($page) {
include "path/to/your/content/$page";
}
else { include "path/to/default/content"; }
?>
1) Then can I use php include instead?
2) path/to/your/content/$page - Must I replace the $page with my .php page?
3) Do you know where I can find this tutorial?
1) That is php include.
2)No. $page is a variable that is defined when you access the page like you said in your first post... index.php?page=whatever.ext
3)http://php.net :)
 
Originally posted by bigperm
1) That is php include.
2)No. $page is a variable that is defined when you access the page like you said in your first post... index.php?page=whatever.ext
3)http://php.net :)
1)How do I link to that particular page?
2) Do you have that specific link to that tutorial? I've tried to find it at php.net but to no avail.
 
Originally posted by The Red Guy
1)How do I link to that particular page?
2) Do you have that specific link to that tutorial? I've tried to find it at php.net but to no avail.
1)Which page are you talking about?
2)That link was a joke. There isn't a tutorial that I know of. I gave you the address of the homepage of the language.
 
OK, say you have a file called john.php, and in that file you have
<?
if ($page == "2") {
include("yourfile.php");
} elseif ($page == "3") {
include("anotherfile.php");
}
?>
then when you go to www.test.com/john.php?page=2
it will show yourfile.php, but say you go to www.test.com/john.php?page=3
then it will show anotherfile.php

to change the john.php?page= bit you must edit
if ($page == "2") and change the page bit. like say you change the script above to
<?
if ($fred == "2") {
include("yourfile.php");
} elseif ($fred == "3") //notice i also changed the page bit here
{
include("anotherfile.php");
}
?>
and saved that file called john.php
then you went to www.test.com/john.php?fred=2
then it would show yourfile.php

but another bit of the code you can change is
if ($fred == "2")
like you can change that to
<?
if ($fred == "page2") {
include("yourfile.php");
} elseif ($fred == "page3") {
include("anotherfile.php");
}
?>
and again save that as john.php
when you go to www.test.com/john.php?fred=page2
it will go to yourfile.php,
but if you go to www.test.com/john.php?fred=page3
it will go to anotherfile.php

Understand? if not i will find a good tutorial to explain all this.
 
you can add more easily, to add more to the following script
<?
if ($fred == "page2") {
include("yourfile.php");
} elseif ($fred == "page3") {
include("anotherfile.php");
}
?>
you would just add more of

elseif ($fred == "page3") {
include("anotherfile1.php");
}

so to have 5 pages of content (4 different ones, and the main one) you would use
<?
if ($fred == "page2") {
include("yourfile.php");
} elseif ($fred == "page3") {
include("anotherfile.php");
} elseif ($fred == "page4") {
include("anotherfile2.php");
} elseif ($fred == "page5") {
include("anotherfile3.php");
}
?>
 
Originally posted by AlieXai
> But why hard code the file names if you don't have to?

Security
If that's an issue, but store your content files below your httpdocs (or whatever) folder, use .htaccess to prevent unwanted lookers or you can use a DB. There's still no reason to hard code them.
 
Originally posted by Moonman
OK, say you have a file called john.php, and in that file you have
<?
if ($page == "2") {
include("yourfile.php");
} elseif ($page == "3") {
include("anotherfile.php");
}
?>
then when you go to www.test.com/john.php?page=2
it will show yourfile.php, but say you go to www.test.com/john.php?page=3
then it will show anotherfile.php

to change the john.php?page= bit you must edit
if ($page == "2") and change the page bit. like say you change the script above to
<?
if ($fred == "2") {
include("yourfile.php");
} elseif ($fred == "3") //notice i also changed the page bit here
{
include("anotherfile.php");
}
?>
and saved that file called john.php
then you went to www.test.com/john.php?fred=2
then it would show yourfile.php

but another bit of the code you can change is
if ($fred == "2")
like you can change that to
<?
if ($fred == "page2") {
include("yourfile.php");
} elseif ($fred == "page3") {
include("anotherfile.php");
}
?>
and again save that as john.php
when you go to www.test.com/john.php?fred=page2
it will go to yourfile.php,
but if you go to www.test.com/john.php?fred=page3
it will go to anotherfile.php

Understand? if not i will find a good tutorial to explain all this.
1) Can I make the john.php as the front page instead of the john.php?page=2 ?
2) How do I link to that particular file if I need to? Let's say I have frontpage.php , how do I make a hyperlink?By linking to john.php?page=2 or yourfile.php directly?
 
Back
Top