Page 1 of 1

Query string navigation

Posted: Sun Aug 10, 2003 8:48 pm
by 187skillz
Hi..
I'm new to php, I read the phpcomplete beginner's tutorial and it made so much sense, so big up to the Author.
I'm building my site using php and I was wondering how to do a proper query string navigation..
I read some other tutorial that said:

Change your link too

Code: Select all

<a href="?section=about">about</a>

Create an "about.php" page
And then:

Code: Select all

<?php
<?php
if($section=="about") include("about.php") ;
elseif($section=="music") include("music.php") ; 
?>
?>
OK I'm a lil confused, where does the php (the second one)code go? does it go to the "about.php" page?
I'm new, just getting my feet wet with php, I read some tuts but I'm reading it now/building my site at the same time, sorry if this question is dumb, getting it has been hard until phpcomplete tutorial.

Thanks!

Posted: Mon Aug 11, 2003 1:45 am
by JAM

Code: Select all

// in the folders, where the link is clicked, index page 
<a href="?section=about">about</a>
// same as
<a href="index.php?section=about">about</a>

// in about.php
<a href="about.php?section=about">about</a>
You should also try the last link in my signature. Jason has made some pretty good examples there. (Regarding _POST and _GET)

Posted: Mon Aug 11, 2003 2:46 am
by qartis
But to answer your first question, yes, the "about page" code would be put in about.php.

Posted: Mon Aug 11, 2003 3:10 am
by toms100
just incase register_globals is off (should be)

Code: Select all

<?php
if($_GET['section']=="about") include("about.php") ; 
elseif($_GET['section']=="music") include("music.php") ; 
?>

Posted: Mon Aug 11, 2003 7:16 am
by 187skillz
Ok thanks to everyone that's answered, pls take a look at this online version
Beta
why is it that when I click on "more" it leads back to the home.php instead of about.php?

thanks for your time!

Posted: Mon Aug 11, 2003 3:59 pm
by qartis
Because the HTML link is leading to home.php?section=about , not about.php

Posted: Mon Aug 11, 2003 7:22 pm
by McGruff
This:

home.php?section=about

.. runs the home.php script and declares

Code: Select all

<?php
$section = 'about'; 
?>
.. in the global scope of the script.

As mentioned above, this will only work with register globals on. A var defined in a url is a $_GET var: register globals on will automatically declare these in the global scope of the script (along with $_POST, $_SESSION etc). That can be a security risk. With register globals off, you get the $section var from the $_GET superglobal array as: $_GET['section'].

If you want that to open a new page ("About My Site"?) then, according to the code you posted, home.php would need to if/else on $section (or rather $_GET['section']), including the appropriate file to build the page - which it doesn't at present.

It's better if your home page is in an "index.php" file rather than "home.php": http://www.yoursite.com/index.php will always be called by default simply by typing in the "http://www.yoursite.com" domain - but other filenames won't.

Posted: Wed Aug 13, 2003 10:25 am
by 187skillz
Thanks Mcgruff, appreciate your time!

Posted: Wed Aug 13, 2003 3:47 pm
by qartis
It's better if your home page is in an "index.php" file rather than "home.php": http://www.yoursite.com/index.php will always be called by default simply by typing in the "http://www.yoursite.com" domain - but other filenames won't.
That all depends on how you've set up Apache--DirectoryIndex for the specific directory could very well be set to the file 'I_am.great', which would even be parsed by php :)

It's all in the server. To follow conventional naming convention, index.php is the standard, though I've seen some pretty wacky extensions (.sex, .file, .gay, .dot, .php.php, .asdf, et al.)