Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CF programmer need help with starting PHP

Status
Not open for further replies.

BiloMedia

Programmer
Oct 14, 2011
4
0
0
CA
Im new to Tek-Tips and I've searched all day to make something very simple work but just cant seem to get it.

I need to set a default variable in case one is not supplied in the url. In coldfusion I could set it as <cfparam name="url.page" default="home">

than I need to have a if/else statement to load proper page depending on what that variable is set to.

This is my code so far but it is not working, can someone please help


<?php

$page = $_POST['page'] ;

if ($page=="home")
include("components/home.html");
elseif ($page=="about")
include("components/about.html");
elseif ($page=="locations")
include("components/locations.html");
elseif ($page=="models")
include("components/models.html");
elseif ($page=="community")
include("components/community.html");
elseif ($page=="contact")
include("components/contact.html");
elseif ($page=="hunter")
include("components/hunter.html");
elseif ($page=="benjamin")
include("components/benjamin.html");
elseif ($page=="michael")
include("components/michael.html");
elseif ($page=="abbey")
include("components/abbey.html");
?>
 
Nevermind, I figured it out

not sure if this is best practise or not but here it is


<?php

$val = $_GET['page'];

if ($val=="home")
include("components/home.html");
elseif ($val=="about")
include("components/about.html"); etc.
 
Hi

That looks bad and its maintenance will shortly became a pain. I would prefer it like this :
PHP:
[teal]<?php[/teal]
[navy]$pagelist[/navy][teal]=[/teal][b]array[/b][teal]([/teal]
  [green][i]'home'[/i][/green][teal],[/teal][green][i]'about'[/i][/green][teal],[/teal][green][i]'locations'[/i][/green][teal],[/teal][green][i]'models'[/i][/green][teal],[/teal][green][i]'community'[/i][/green][teal],[/teal][green][i]'contact'[/i][/green][teal],[/teal][green][i]'hunter'[/i][/green][teal],[/teal]
  [green][i]'benjamin'[/i][/green][teal],[/teal][green][i]'michael'[/i][/green][teal],[/teal][green][i]'abbey'[/i][/green][teal],[/teal]
[teal]);[/teal]

[navy]$val[/navy][teal]=[/teal][navy]$_GET[/navy][teal][[/teal][green][i]'page'[/i][/green][teal]];[/teal]

[b]if[/b] [teal](![/teal][COLOR=darkgoldenrod]in_array[/color][teal]([/teal][navy]$val[/navy][teal],[/teal][navy]$pagelist[/navy][teal]))[/teal] [navy]$val[/navy][teal]=[/teal][navy]$pagelist[/navy][teal][[/teal][purple]0[/purple][teal]];[/teal]

[red]include[/red] [green][i]"components/$val.html"[/i][/green][teal];[/teal]
Note that the above supposes that the files' names and the GET parameters are identical.


Feherke.
 
I agree with feherke.

And for future reference GET holds the values passed in the querystring, POST normally holds values that come from submitted forms.

I'm sure its just the same in Coldfusion, so using the correct array would get you the correct value.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Very nice, thanks guys, I will implement this at once.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top