Jump to content


PHP Include/Navigation Question


4 replies to this topic

#1 Runeshai

    Young Padawan

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Location:Vancouver, BC
  • Interests:Computers, cooking, music (http://is.gd/rJV8), video & graphics, web design, reading (http://is.gd/rr7o), writing, drawing, watching movies (faves: The 13th Warrior, the Mexico trilogy, Braveheart, Grindhouse, The Descent, Lemony Snicket's Series of Unfortunate Events, Cashback, Lord of the Rings trilogy, the Holy Trilogy [Star Wars IV-VI]). I believe in thought, believing in things, and being progressive, creative and productive. Check out my blog at http://www.animivirtus.com/blake/.

Posted 07 February 2009 - 09:28 PM

I'm setting up a quick portfolio site and I'm having a bit of a problem. I've got all the pages, images and files ready (except the resume and demo reel), and the pages all work (I've tested them locally and online), but I can't get this bit of PHP code to work right.

I used to use this bit of code here:
<?php if (file_exists("$p.php")) { include("$p.php"); } else { include("welcome.php"); }?>
which basically tells the server that if the file in question exists, include it in the content section (or wherever the above code was placed in the page). If it didn't, simply put the page 'welcome.php' in that content area as a means of telling the user an error had occurred. This worked great for a while, but now it doesn't seem to anymore. I'm curious if it has something to do with advances made in the language, which is possible I suppose, but I don't know, and I don't actually know the language, so it's really hard to troubleshoot. Anyway...if you've got scripting experience, you'll notice the above code calls for a variable called 'p.php', which was defined in the site's links like so:
<a href="?p=pages/home">home</a>
Again, it used to work, and I've tried it for this site, but it's just not working right. I've tried cutting out the if statement and just flat including the pages and that works, everything works correctly except for the include-in-content-area and link-to-using-variable parts (basically, the php part of the page is the faulty part).

So far, I've tried variations on the above code, using a switch function and linking it that way using this article, but nothing seems to work. I've researched at both the w3schools.com and the php.net websites, and still no solution. So posting on forums is my next step.

I haven't done any serious web work in a year or two, and this project is the first that needs to actually be live, usable and working soon, so I need this to work. I don't know PHP in any real way except the research I've done into this problem and the little contact I've had after teaching myself HTML and CSS for the last few years.
That said, help will definitely be appreciated, just be clear, because I'll probably need a good explanation of whatever the answer is if it's at all complicated.

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 February 2009 - 10:37 PM

I'm assuming something on your server has changed, and changed for the good. I'm assuming previously they had register_globals turned on in the php.ini, or they were using PHP 4 and have since upgraded to PHP 5 (which has register_globals off by default).

Either way, you can read up on the issue here:
http://php.net/register_globals

Simple solution, use $_GET['p'] rather then just $p.

#3 Runeshai

    Young Padawan

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Location:Vancouver, BC
  • Interests:Computers, cooking, music (http://is.gd/rJV8), video &amp; graphics, web design, reading (http://is.gd/rr7o), writing, drawing, watching movies (faves: The 13th Warrior, the Mexico trilogy, Braveheart, Grindhouse, The Descent, Lemony Snicket's Series of Unfortunate Events, Cashback, Lord of the Rings trilogy, the Holy Trilogy [Star Wars IV-VI]). I believe in thought, believing in things, and being progressive, creative and productive. Check out my blog at http://www.animivirtus.com/blake/.

Posted 07 February 2009 - 11:37 PM

Alright...I did a little bit of research and can't seem to find something that's what I'm looking for using the $_GET function. I'm still not sure how I'd use it for my purpose. I've attached a screenshot of the layout I'm using, hoping maybe it'll be helpful for you to see that in case you misunderstood something. Basically, I want my lefthand navigation to be links that'll, when clicked, include the page that's clicked on. (Reel would include the reel page, etc.). I want the content section to be the place where those pages are included, obviously. Therefore, there's probably got to be some code that goes in the content section defining it as the location to include the pages when they're clicked, and there's got to be some code in the links themselves defining them as variables to be included in the even that they're clicked. The code in the links and the putting together of the two halves is the part I'm having trouble finding and implementing. Again, I've got no real experience or knowledge with PHP, so it's quite possible I've seen a tutorial and just didn't understand what was going on, and missed its relevance to my project. In any case, thanks for the response, but I still need some help.

Attached Files



#4 Jonne

    Young Padawan

  • Members
  • Pip
  • 8 posts
  • Gender:Male
  • Location:Finland
  • Interests:Floorball, PHP, Discovering new things!

Posted 08 February 2009 - 02:39 PM

Here is short explain of using $_GET,

You place following code to top of the index.php

$p = $_GET['p'];

Then you just use $p as normally you would use, but now it will have the string from the url inside of it.

And the domain -> index.php?p=Welcome

I hope this helped!

EDIT:: As I readed more deep your last post, I noticed you would like to also research function called 'case' in PHP.

It is mayby easiest in situations like this.. I try to explain it simple.

We will need also another function called Switch to this. Switch will look up the case which is absolute same than the string.

switch($p){

   default :
	   include('nav.php');
	break; 

	case "Home":
	   include('home.php');
	break;

	case "Reel":
	   include('reel.php');
	break;

}

Switch will take the $p string, one we just made up in the top of the index.php, compares it case names and if it founds exactly same then it will run code inside of it.

Case is the cases we will use i.ex. Reel -> Case "Reel": and it will be closed always with Break; , simple...

So you will place this Switch code part to where you want your content.

And you just use urls like index.php?p=Home , Reel or just index.php witch will run code from Default.

Edited by Jonne, 08 February 2009 - 02:48 PM.


#5 Runeshai

    Young Padawan

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Location:Vancouver, BC
  • Interests:Computers, cooking, music (http://is.gd/rJV8), video &amp; graphics, web design, reading (http://is.gd/rr7o), writing, drawing, watching movies (faves: The 13th Warrior, the Mexico trilogy, Braveheart, Grindhouse, The Descent, Lemony Snicket's Series of Unfortunate Events, Cashback, Lord of the Rings trilogy, the Holy Trilogy [Star Wars IV-VI]). I believe in thought, believing in things, and being progressive, creative and productive. Check out my blog at http://www.animivirtus.com/blake/.

Posted 08 February 2009 - 05:21 PM

Awesome. Believe it or not, that part in the beginning about putting the $p = $_GET['p']; code at the beginning of the page was all I was missing. I had everything already set up right, that was the bit I missed in all the tutorials I'd found while looking this up. Thanks for the help, it's working great now!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users