Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-17-2004, 05:08 PM   #1 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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!
gamehead200 is offline   Reply With Quote
Old 10-17-2004, 07:22 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-18-2004, 04:38 AM   #3 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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!');
}

?>
gamehead200 is offline   Reply With Quote
Old 10-18-2004, 04:43 AM   #4 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Something like that, but I'd run the var through basename() first for paranoia.

-r
idx is offline   Reply With Quote
Old 10-18-2004, 05:05 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-19-2004, 03:15 PM   #6 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
Wait, how can I put in a default file to open when I open index.php with no variable in the URL?
gamehead200 is offline   Reply With Quote
Old 10-19-2004, 03:19 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
in my example, the default would be the 'else' area.
__________________
Mike
sde is offline   Reply With Quote
Old 10-19-2004, 04:34 PM   #8 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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? :/
gamehead200 is offline   Reply With Quote
Old 10-19-2004, 04:41 PM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-19-2004, 04:44 PM   #10 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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>");
}

?>
gamehead200 is offline   Reply With Quote
Old 10-19-2004, 05:26 PM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-20-2004, 04:09 PM   #12 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
Here's the final product... Let me know what you think:

http://www.astromike.com
gamehead200 is offline   Reply With Quote
Old 10-20-2004, 04:16 PM   #13 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
nice work. it looks a lot cleaner than your old site =)
__________________
Mike
sde is offline   Reply With Quote
Old 10-21-2004, 05:13 PM   #14 (permalink)
DavH27
PHP Pilgrim
 
DavH27's Avatar
 
Join Date: Aug 2004
Location: London
Posts: 170
DavH27 is on a distinguished road
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
DavH27 is offline   Reply With Quote
Old 10-21-2004, 05:57 PM   #15 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
hi dav .. long time no see =)
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:05 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting