Monday 17 October 2011

PHP Question (easy points for a pro)?

Okay I have my personal website hosted using apache and PHP 5 on my PC.

I have it so that when you click on a link in the menu, it uses the GET function to get a certain html page. This allows me to change my template on my index page without having to change every single page.

For example:

%26lt;?php

include(%26quot;/pages/%26quot; . $_GET['id'] . %26quot;.html%26quot;);

?%26gt;



I am still learning PHP, and need some help with this:

If the id is invalid, and doesn't have an html file in %26quot;pages%26quot;, how can I have it redirect to another page instead of displaying a PHP error? I think its something like %26quot;if doesnotexist%26quot; or something.



Sorry if I made that too complicating, but i'm not good at writing in a %26quot;techy%26quot; way.

ALSO: Please don't suggest that I start using databases instead of my html pages, I prefer it the way it is.PHP Question (easy points for a pro)?if (file_exists (%26quot;/pages/{$_GET['id']}.html%26quot;))

{

include(%26quot;/pages/{$_GET['id']}.html%26quot;);

} // if

else

{

include(%26quot;otherFile.html%26quot;);

} // else
PHP Question (easy points for a pro)?
you will need an if statement todo this something like.

string goto = $_GET['id'];



if ( goto != %26quot;%26quot; )

{

include(%26quot;/pages/%26quot; . $_GET['id'] . %26quot;.html%26quot;);

}else

{

//goto the redirect page

}



this is based on the presumtion that get will return %26quot;%26quot; if there is no id
PHP Question (easy points for a pro)?
Maybe something like include($_GET['id'] == %26quot;%26quot; ? %26quot;/pages/otherpage.html%26quot; : %26quot;/pages/%26quot;$_GET['id'].%26quot;.html%26quot;);



or if the id is populated but invalid then



include(file_exists( %26quot;/pages/%26quot;$_GET['id'].%26quot;.html%26quot;) ? %26quot;/pages/%26quot;$_GET['id'].%26quot;.html%26quot; : %26quot;/pages/otherpage.html%26quot;);





an inline if statement



Hope that helps