|
 |
|
 |
10-17-2004, 05:08 PM
|
#1 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
index.php?page=1
Hey everyone!
Is there a way to make it so that when you click on a link, it will send you to whatever page name is in the link?
What I mean is, how do people make pages like index.php?page=1 ? I've been wondering for such a long time! 
|
|
|
10-17-2004, 07:22 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
well a variable in the url is just that, a variable. what you do with that information is up to you. here is a little example of how you can changed content based on the querystring page=1
PHP Code:
<?
if($page == 1){
echo "this is page 1";
}elseif($page == 2){
echo "this is page 2";
}else{
echo "invalid page";
}
?>
if you have more specific questions, feel free to ask.
__________________
Mike
|
|
|
10-18-2004, 04:38 AM
|
#3 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Something like this?
PHP Code:
<?php
$p = $_GET['p'];
$p = "css/$p.txt";
if( file_exists($p) )
{
include($p);
}
else
{
echo('AN ERROR HAS OCCURRED!');
}
?>
|
|
|
10-18-2004, 04:43 AM
|
#4 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
Something like that, but I'd run the var through basename() first for paranoia.
-r
|
|
|
10-18-2004, 05:05 AM
|
#5 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
i thought about the include thing too, .. but it seemed silly to put the filename in the query string instead of just calling that file itself.
also, great point idx. 
__________________
Mike
|
|
|
10-19-2004, 03:15 PM
|
#6 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Wait, how can I put in a default file to open when I open index.php with no variable in the URL?
|
|
|
10-19-2004, 03:19 PM
|
#7 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
in my example, the default would be the 'else' area.
__________________
Mike
|
|
|
10-19-2004, 04:34 PM
|
#8 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
OK, this code is really starting to piss me off:
PHP Code:
<?php
$p = $_GET['p'];
$page = "pages/$p.txt";
if( !isset($page) )
$page = "pages/Home.txt";
if( file_exists($page) )
include "$page";
else
print("<tr><td class=\"base\" colspan=\"2\">AN ERROR HAS OCCURRED!</td></tr>");
?>
Everytime I run index.php without a variable, the default page outputs the error message in there... Shouldn't this work? :/
|
|
|
10-19-2004, 04:41 PM
|
#9 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
well with that code, $page will always have a value because you are assigning it "pages/.txt" even if there is no $_GET['p'] variable defined.
try this:
PHP Code:
<?php
$p = $_GET['p'];
if( !isset($p) ){
$page = "pages/Home.txt";
}else{
$page = "pages/$p.txt";
}
if( file_exists($page) ){
include($page);
}else{
print("<tr><td class=\"base\" colspan=\"2\">AN ERROR HAS OCCURRED!</td></tr>");
}
?>
__________________
Mike
|
|
|
10-19-2004, 04:44 PM
|
#10 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Yeah, I figured that out just before you replied... This also works:
PHP Code:
<?php
$p = $_GET['p'];
$page = "pages/$p.txt";
if( !$p) )
$page = "pages/Home.txt";
if( file_exists($page) ){
include "$page";
}else{
print("<tr><td class=\"base\" colspan=\"2\">AN ERROR HAS OCCURRED!</td></tr>");
}
?>
|
|
|
10-19-2004, 05:26 PM
|
#11 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
if you really wanna have fun, use the ternary operator
PHP Code:
<?
$page = "pages/".(($_GET['p'])?$_GET['p']:"Home").".txt";
if( file_exists($page) ){
include "$page";
}else{
print("<tr><td class=\"base\" colspan=\"2\">AN ERROR HAS OCCURRED!</td></tr>");
}
?>
__________________
Mike
|
|
|
10-20-2004, 04:16 PM
|
#13 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
nice work. it looks a lot cleaner than your old site =)
__________________
Mike
|
|
|
10-21-2004, 05:13 PM
|
#14 (permalink)
|
|
PHP Pilgrim
Join Date: Aug 2004
Location: London
Posts: 170
|
Quote:
Originally posted by idx
Something like that, but I'd run the var through basename() first for paranoia.
-r
|
What's one of those, then?
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
|
|
|
10-21-2004, 05:57 PM
|
#15 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,505
|
hi dav .. long time no see =)
__________________
Mike
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:05 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|