Jump to content

IF a variable = an element in an array do this


hp1

Recommended Posts


I have a three column layout using CSS. In the left I have a menu, in the centre the main content and in the right a guestbook.The way I have structured index.php is to inlcude all the required files in <divs> based on

<?php	if($_GET

){	$page=$_GET

;	include "$page.php";}	}else{	include ('indexhome.php');	}	?>

My problem is that the guestbokk has links to cycle through the different pages and of course they end up trying to be included in the main div as 1.php or 2.php based on the above. I need a bit of code that basically ignores the include if $page = 0-9, i.e one of the page numbers. I want the centre div to maintain its original content regardless of what guestbook page number is clicked.I hope this is enough to explain my problem, i hope someone can help, let me know if you need any more code.cheers

Link to comment
Share on other sites


Try it like this, the if($_GET


) statement is not understood as you think it should by php:You must ask if the variable exist for the browser, this is what isset(variable) dose.

<?phpif (isset($_GET

)){	$page=$_GET

;	include ($page.php);}else{	include ('indexhome.php');}?>

I hope I helped you.

Link to comment
Share on other sites

There's several ways round this1. don't use "page" in the query for youre site, replace it with something like "p", "part", "section", "sec" etc.2.don't use "page" in the guestbook, replace it with something like "p", "gbpage" etc.3. Check for a numeric value and automatically include the guestbook, just note: THIS IS "UGLY":

<?phpif (isset($_GET['page'])){	$page=$_GET['page'];	if (is_int($page))		 include('guestbook.php');	else		include ( $page.'.php');}else{	include ('indexhome.php');}?>

Hope you find that usefull.(thibo1025 forgot some quotes also)

Link to comment
Share on other sites

Many thanks for the advice, I will have a tinker. I'm a novice of course, I thought the 'page' variable in the $_GET was a standard php code and had to be set to page.

Link to comment
Share on other sites


No, there is nothing standard like that. You can pretty much do whatever you want.

1. don't use "page" in the query for youre site, replace it with something like "p", "part", "section", "sec" etc.2.don't use "page" in the guestbook, replace it with something like "p", "gbpage" etc.
I would go with one of those, just use a different variable. The problem with trying to check what type data in $_GET or $_POST is, is that all of the data is a string. When the response comes back from the server, PHP doesn't process that response and start separating out numbers from strings, it just treats everything as string data. You can check if it is a numeric string, but everything will still be a string.Also, just so you know, there is a difference between these:$_GET

$_GET['page']The first one uses the value of a constant called page. If you have not defined a constant called page like this:define("page", 1);Then PHP issues a warning (I think) for an undefined constant and uses the string "page". The second example above just uses the string "page" and doesn't bother to look for a constant first. The vast majority of all cases people use like this are supposed to be a string, so you need to put quotes around them. There is also a way to use quotes even if the array is inside a string:echo "The value of page is {$_GET['page']}";surrounding it with {} will let you use the single quotes and avoid the undefined constant warning.
Link to comment
Share on other sites

Even if I change the variable 'page' to 'menupage' or something it now of course just loads indexhome.php as soon as I click on the guestbook page links becuase of the else statement.What I want to happen is when I click on the guestbbook page the guestbook loads in the right column and the centre column DOESNT CHANGE. Of course at present it is trying to load 1.php or 2.php which dont exist. So i need somethign that says if $page=='an integer' reload the existing centre column content. Is that possible. Does this help explain? Would it help if I included a link to my site (it very amatuer dont laugh!)?

Link to comment
Share on other sites

There are no stupid questions....A link would help.How is your guestbook setup?It shouldn't try to load 1.php etc if you've changed and using menupage or something like that.It sounds like the questbook links to index.php?page=2 instead of index.php?menupage=guestbook&page=2.You must make sure that the menupage=... is used where it's needed.I have done like this in some sites:

// In the file/part that loads the "section" (e.g. global)$MainQS = '?menupage='.$_GET['menupage'].'&';global $MainQS;// Load the "section'if (isset($_GET['menupage'])){	$page=$_GET['menupage'];		include ( $page.'.php');}else{	include ('indexhome.php');}...// In the section-files (Ex. guestbook.php)global $MainQS;//  This goes for every link that "goes to the same site"<a href="<?php echo $MainQS ?>???>Link</a>// Replace the tree questionmarks (???) with for example page=2 or page=<?php echo $pageNum?>//  Note even if this works if the link goes to another section, it doesn't look good,  you may want to add a second var.$NoSecQS = '?';global $NoSecQS;// If you declare NoSecQS before $MainQS you can do like this, if you want$MainQS = $NoSecQS.'menupage='.$_GET['menupage'].'&';// NOTE: If you have something after the ? in either $MainQS or $NoSecQS you MUST end them with an &

This may also be usefull if you have something that you want to persist true the visit and don't want to use cookies or sessions, for example an choice of layout template:

...$template = 'main';if (isset($_GET['template']) {	$template = $_GET['template'];	$NoSecQS .= 'template='.$template.'&';}...$css = 'main.css'if ($template == 'none')	$css = '';else if ($template == 'contrast')   $css = 'contrast.css';if ($css != '')	echo '<link ref="stylesheet" type="text/css" href="css/'.$css.'" />';...

That became more than I indented, but I hope you find it helpfull or at least interesting.. ;?)

Link to comment
Share on other sites

If you are willing to learn AJAX you could do this very easily, without having to refresh the whole page or use frames. You see if you had a main <div>, such as:

...<div id="guestbook">...</div>...

with AJAX you could replace the 'innerHTML' with page 2 of the messages.If you want to learn more about AJAX, the w3schools site has a tutorial on it... by they way thibo1025, you don't NEED to use isset(). I use...

if ($_GET['page'])

...that method, and it works every time, on all versions of PHP.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...