Jump to content
xisto Community
Sign in to follow this  
apple

Simple Page With Different Parts

Recommended Posts

I want to make a simple page in php. it has 3 steps.. like ,page.php there i say, lets go to second step.. and i want to create page.php?step=2 and there.. i say lets go to step3 and i create page.php?step=3How i can create like that.. anyhelp please

Share this post


Link to post
Share on other sites

try this

$step = $_GET['step'];// This gets the step you wantswitch ($step) {case 1:// Put your code for case 1 herebreak; // Needed to tell PHP to exit the switch statementcase 2:// Code.break;case 3:// Codebreak;default:// This code is ran if no step is run.  break;}

Share this post


Link to post
Share on other sites

Recently I made my website with the same approach. One php page that will display different content depending on what the viewers want. As I was on the process on developing it I found out that the php page will become cumbersome as the content it will display will increase. In your case, when the steps reaches step20 or more. So instead of using switch() statement and encode all the information needed to display in one php page with respect to each steps (steps 1 to 20 for example) in your case you could use one instruction that will do it all:

 

require("file");

 

the above function basically will insert the content of "file" as HTML. To understand how it works we will create an example. First let say that we have a file named one which is save in the same folder with your page.php

 

one

<h3>This is Step 1</h3><?PHP	print "\nPHP things to do in step 1";?>

above is the content of the file one. As you notice we use the <?PHP ?> php tag. Whatever text file require() will insert it consider it as an HTML texts not PHP thus it is neccessary to use the PHP tag when adding php code.

 

 

page.php

<html><head></head><body>	<?PHP		if(isset($_GET['step']))			require($_GET['step']);	?></body></html>

above is the page.php file. As you can see its pretty neat but it works fine and better than the swtich() statement.

 

 

Heres how it works:

For example we use the page.php as page.php?page=one. So what happened is the variable $_GET['step'] contains the string "one" which is basically the name of our file for the first step. In that case when we code require($_GET['step'); its just the same as require("one"); which will insert whatever content the file one. But be sure that one file is the same directory with the page.php file. But if it is not or if you want to use extensions like one.txt or one.inc use this technique:

 

assuming that we have a file one.inc which is saved in the folder myfolder/steps

 

page.php

<html><head></head><body>	<?PHP		$location = 'myfolder/steps/';		$extension = '.inc';	//I like to use inc as extension coz it could mean include file		if(isset($_GET['step'])){			$file = $location . $_GET['step'] . $extension;			require($file);		}	?></body></html>

The final HTML code output of your page.php if we use it like this page.php?page=one is

 

<html><head></head><body><h3>This is step 1</h3>PHP things to do in step 1</body></html>

Basically becuase of the require() function whatever the content of one.inc it is inserted in the area where we called the funtionc require().

 

With this technique you can have as many steps you want to take. You just have to create the files two.inc, three.inc ... twenty.inc. The best thing about this is each steps instructions and data are saved in individual file which means mentaining and debugging is easy and not cumbersome. As you can see there are few codes in the page.php as opposed to swtich() statement in which you will crease a list of case value:.

Edited by saga (see edit history)

Share this post


Link to post
Share on other sites

I prefer the switch solution, sometimes we get so many files that it is very difficult to remember the relation between all files.Using the switch solution you can put the different cases in different functions and so this is a lot easier to handle.If you want to use the include solution, think well of your naming strategy so you know the relationship between the files. The included files should be in a different directory.Good luck !!!! :P

Share this post


Link to post
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
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.