TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   PHP conditional help (http://www.talkphp.com/absolute-beginners/115-php-conditional-help.html)

Ogden2k 09-17-2005 11:10 PM

PHP conditional help
 
Hi, I need to have a conditional where it basically does this:

if page address is x than show this

if page address is xy than show this

etc... How would I go about doing this?

Tekime 09-18-2005 02:10 AM

I'm not completely sure I know what you mean by page address. But for example, if you have:

www.example.com/index.php?page=x

and

www.example.com/index.php?page=xy

Then you can create a conditional statement like:

if((!empty($_REQUEST['page'])) && ($_REQUEST['page'] == 'x'))
{
// Show page x
}
elseif((!empty($_REQUEST['page'])) && ($_REQUEST['page'] == 'x'))
{
// Show page xy
}

But more likely, you'll want to accept multiple values for 'page' and pull them from a database or file.

Then you'd want to simply check for page, validate page with whatever parameters you require, then load page from DB or disk, and display.

if(!empty($_REQUEST['page']))
{
// Do whatever with $_REQUEST['page']
}
else
{
// Default behavior
}

jaswinder_rana 09-18-2005 07:19 PM

still if there are 2 or more choices to be made i always prefer switch
PHP Code:

$page = (isset($_REQUEST['page']))?$_REQUEST['page']:'';
switch(
$page)
{
 case 
'x':
    
//do this
   
break;
 case 
'xy':
   
//do this
  
break;
 default:
  
//do this
  
break;


this is easier to handle than if. i do not know how much execution time difference is in if and switch, however.
and also i prefer to be precise,, user $_GET for querystrings and user $_POST for post

i know $_REQUEST provides a wider range for script, as both methods can be used to do same thing, but, in that case post data can always be overridden by get data, unless get comes first and then post (which i presume is default). i also think cookies are included in $_REQUEST (no t sure), and if they are then again its the same problem

hope this helps

Ogden2k 05-31-2006 10:57 PM

Essentially what I'm trying to do is this:
  • site/
    • show data-a
  • site/1/
    • show data-a plus data-b
  • site/2/
    • show data- plus data-c
I hope this clears it up a little bit.

bluesaga 09-07-2007 09:52 AM

What you are looking for, my friend is Mod Rewrite, this is not PHP, however it is DIRECTLY related, a lot of applications use this facility.

To do what you want to do you would have something like:
Code:

        RewriteEngine on
        RewriteRule ^site/?$ site.php [L]
        RewriteRule ^site/([^\/]+)/$ site.php?szSite=$1 [L]

Now then if we break that down:

Code:

RewriteEngine on
Enables Mod Rewrite.

Code:

        RewriteRule ^site/?$ site.php [L]
RewriteRule is the standard prefix, the next part is a standard regular expression pattern, the next bit, is the location of the page to direct the user to. The "[L]" means "last rule", which you should use on all one line mod rewrites, it is only used for much more complicated rules.

Code:

        RewriteRule ^site/([^\/]+)/$ site.php?szSite=$1 [L]
Again RewriteRule is the standard prefix, the next part is a standard regular expression, everything within the regular expression that is located within ()'s can be used within the rule to direct the user to, in numerical order they are found. So for the next part it redirects the user to site.php?szSite=$1 The $1 looks back for what is located within the ()'s.

Now lets add an example:
I try to go to www.domain.com/site/abc/ the rewrite rule will allow me to use www.domain.com/site.php?szSite=abc without me knowing that i am actually using that page, the browser will only ever display www.domain.com/site/abc/ and the user/search engine can not find out what url they are actually viewing unless they are told, or you redirect them there mistakenly.


All times are GMT. The time now is 07:52 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0