I see these sites with PHP files that have for example here it is ?do=. I wanted to make an array of documents names:

$navigation[0] = "index.php";
$navigation[1] = "page1.php";
$navigation[2] = "page2.php";

So on and so forth. I'm probably not making any sense, but as I said in the title, I am new to PHP. I wanted to make something like:

index.php?nav=index.php // index.php?nav=page1.php // index.php?nav=page2.php

I'm not really sure how I would accomplish that. I would rather learn from what I know and not just look up an example script. Please can you help?

Recommended Answers

All 12 Replies

Could I just make a variable and set it to a string value of "?nav=" and then place the variable value of whichever in the array or something like that?

what you are trying to achieve while doing this?
I think you just trying to complicate the (might be) easy thing..
post here what you want to do.
If you want to construct the url like "http://www.website.com/index.php?p=varname
This var values are available in the $_get variable.
The way to get those values in the page -
if(isset($_GET) && $_GET){
//do something with them
}
or explain more and post your code here

<?php
	
	$default = "home.php";
	
	// Array of allowed files
	$allowed = array(
					 'index',
					 'contact',
					 'about',
					 );
	
	// If P variable is set then continue with the script, otherwise include the $default page
	if( isset( $_POST["p"] ) || isset( $_GET["P"] ))
	  {
		  // Checks to see if the P variable has a value, if so it is set to the $page variable
		  $page = isset( $_GET["P"] ) ? $_GET["P"] : $_POST["P"];
		  
		  // Checks to see if the value of $page is in the $allowed array.  The trim removes the extra spaces that might exist in the array
		  if( in_array( trim ( $page ), $allowed ))
		    {
				// Sets the value of $file to the file name you are requesting and appends the ".php" extension
				$file = $page . ".php";
				
				// If the file exists, then include the file
				if( file_exists( $file )))
				  {
					  include( $file );
				}
				
				// If the file is in the array, but doesn't exist... include the $default file
				else
				  {
					  include( $default );
				}
			}
			
		// If the file doesn't exist in the array, include the $default file
		else {
			  include( $default );
		}
	  }
	
	// If no request for a page is made, include the $default file
	else {
		  include( $default );
	}
	
	?>

This is an example of what I was looking for. You have helped me actually figure out exactly what I wanted to do and I thank you tremendously.

Keep doing your best:)
If you think if you are satisfied mark the thread as solved

I get this error code now.

33554432 bytes exhausted (tried to allocate 4864 bytes) in /home/lunatiq/public_html/header.php on line 45

I researched online and it says I should increase the limit in php to a higher value, but is this the only way to solve this problem? And what exactly is happening?

Oh ok... I believe the problem is in the array. How do I define a value limit for the array?

Note this extra comma in the array, whats that for.
should be -

// Array of allowed files
	$allowed = array('index', 'contact','about');

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4864 bytes) in /home/lunatiq/public_html/index.php on line 3


Arg now I am getting this error for line 3

$default = "home.php";

This code can be written efficiently by this way -

$file = "home.php";
	
	// Array of allowed files
	$allowed = array('index', 'contact','about');
	if( (isset( $_POST["p"] ) && $_POST["p"] !='') || (isset( $_GET["p"] ) && $_GET[["p"] !='')) 
  	{
		  $page = $_REQUEST["P"];
		  if( in_array( trim($page), $allowed ))
		    {
				$file = $page . ".php";
			}
	
	  }
  include( $file );
	?>

This is the efficient way for your code -

<?php
	
	$file = "home.php";
	
	// Array of allowed files
	$allowed = array('index', 'contact','about');
	if( (isset( $_POST["p"] ) && $_POST["p"] !='') || (isset( $_GET["p"] ) && $_GET[["p"] !='')) 
  	{
		  $page = $_REQUEST["P"];
		  if( in_array( trim($page), $allowed ))
		    {
				$file = $page . ".php";
			}
	
	  }
  include( $file );
	?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.