Jump to content


Photo

Nested PHP switch statement problems


  • Please log in to reply
12 replies to this topic

#1 onekoolman

onekoolman
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 29 May 2006 - 04:10 PM

Using the code below, I am trying to figure out a way to have a default of all the switch statements, but if I put a default at the end of them all, if the statement isnt switch page4, it shows up on page 1, page 2, and page 3.

Is there a way to have all the statements, but default only displays when there is NO index.php?thing=case

So if its [a href=\"http://www.domain.com\" target=\"_blank\"]http://www.domain.com[/a] it shows default, and if its [a href=\"http://www.domain.com/index.php\" target=\"_blank\"]http://www.domain.com/index.php[/a] it still shows it.

Yet if its [a href=\"http://www.domain.com/index.php?page1=p1\" target=\"_blank\"]http://www.domain.com/index.php?page1=p1[/a] it shows that page, but not the default.

There's a reason behind why I want it that way for my site, the example below shows it as not needed, but thats the way I need it.

<?php 

switch($_GET['page1']) { 

   case "p1": 
      include("includes/page1.php"); 
   break; 

}

switch($_GET['page2']) { 

   case "p2": 
      include("includes/page2.php"); 
   break; 

}

switch($_GET['page3']) {  

   case "p3": 
      include("includes/page3.php"); 
   break; 

}

switch($_GET['page4']) { 

   case "p4": 
      include("includes/page4.php"); 
   break; 

}

?>


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 29 May 2006 - 04:48 PM

Sorry, but your code makes little sense. Why are your url passed variables setup like this?

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 29 May 2006 - 04:48 PM

Wow, pretty confusing. What do you want?
Does it display more than 1 page at the same time? Do you want the default to be displayed when ALL of them fails or whenever any of them fails?
~ D Kuang

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 May 2006 - 04:53 PM

Rethink how you handle the URL parameters. You probably want a URL similar to
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] [a href=\"http://www.example.com?p=1\" target=\"_blank\"]http://www.example.com?p=1[/a] or [a href=\"http://www.example.com?p=2\" target=\"_blank\"]http://www.example.com?p=2[/a][/quote]
Then you can have something like:
<?php
if (isset($_GET['p']) && ctype_digit($_GET['p']) && strlen($_GET['p']) == 1) {
   $page_to_inc = 'include/page' . $_GET['p'] . '.php';
   if (file_exists($page_to_inc)) include($page_to_include);
}
?>
The first "if" statement makes sure that whatever is entered is a one charater number (i.e. 0 - 9). If you have more than 10 pages to include this way, just change third condition appropriately.

The second "if" makes sure that the file exists before trying to include it.

These two checks should be enough to keep malicious users from highjacking your code.

Ken


#5 onekoolman

onekoolman
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 29 May 2006 - 05:23 PM

Like I said the example is setup as an example theres a reason behind it, but I don't know how to explain it, I will setup an example site somewhere..

What im saying is if I add a default code at the bottom, if your in the switch at the bottom which is #4 that default shows if theres no case, and when theres a case it doesnt. But if its a request for a switch above that one that default also displays, which is what I want to eliminate. I only want default to display if there is no request by index.php.

as soon as ?page shows up I dont want default.

Full working example..

[a href=\"http://www.scrapbooking4ever.com/\" target=\"_blank\"]http://www.scrapbooking4ever.com/[/a]

#6 homchz

homchz
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationLee, New Hampshire

Posted 29 May 2006 - 05:41 PM

I do not understand what would make it need to be this confusing.

Why noe just have a switch for the page, then if there is more info that is needed on that certian page then switch again within the called page.


www.domain.com/index.php?page=page1&content=p1
or
www.domain.com/index.php?page=page2$content=p3

this way you can have all your pages, and any amount of content inside.

You just need to think back one more step and have one page that calls all of your numbered pages and it should work fine.

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 May 2006 - 06:55 PM

I modified my original suggestion to fit your constraints. Here is the code:
<?php
<html>
<head>
    <title></title>
</head>

<body>
<?php 
$params_ok = false;
if ((isset($_GET['t']) && ctype_digit($_GET['t']) && strlen($_GET['t']) == 1)
        && (isset($_GET['p']) && ctype_digit($_GET['p']) && strlen($_GET['p']) == 1)) {
    $include_file = 'includes_' . $_GET['t'] . '/page' . $_GET['p'] . '.php';
    if (file_exists($include_file)) {
        $params_ok = true;
        include($include_file);
    }
}
if (!$params_ok)
{
?>
This is default main:
<P>
<a href="index.php?t=1&p=1">Page 1 Type 1</a>
<P>
<a href="index.php?t=2&p=1">Page 1 Type 2</a>
<P>
<a href="index.php?t=3&p=1">Page 1 Type 3</a>
<P>
<a href="index.php?t=4&p=1">Page 1 Type 4</a>
<? } ?>
</body>
</html>
See it in action: [a href=\"http://www.rbnsn.com/phpfreaks/multi_test/index.php\" target=\"_blank\"]http://www.rbnsn.com/phpfreaks/multi_test/index.php[/a]

Ken

#8 onekoolman

onekoolman
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 29 May 2006 - 07:24 PM

My site is pre-setup using the first example. So it's kinda hard to put in a new system, and I wish this was an easier fix, cause all I want is a master default for all the switch statements..



#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 May 2006 - 07:42 PM

What do you mean "pre set-up"? Did you write it that way or did someone else? If you wrote the code and it doesn't work correctly, it might be easier to throw it out and start again.

Ken

#10 onekoolman

onekoolman
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 29 May 2006 - 07:54 PM

The site is running, on that code, its modified for the site, the reason why is cause itll take hours to get it remodified, and I dont have that time, the entire site works fine, but the hompage doesnt show up right.
Cause it doesnt have a default. the most I could do was make a case for ?page=main, but thats not loaded when you go directly to the site. Making another page could work, but that would also take hours because the links are based on index.php. So I dont know I was looking for the kind-of fix that I described, if its not possible np, I will just live with it for a while. Thanks.

#11 onekoolman

onekoolman
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 29 May 2006 - 08:23 PM

*Solved*

I just made an if/else array.

Finally got the errors out.

Thanks for the reply's


#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 May 2006 - 08:38 PM

Please post you're working code in case someone else has the same problem then they can learn from your solution.

Ken

#13 onekoolman

onekoolman
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 29 May 2006 - 08:49 PM

I still actually have a small problem. I want the includes to come from a different directory not the one the script runs in..


In this code I want my directory before the $GET function, I tried but its just giving errors:

if (isset($_GET['page1']) && in_array($_GET['page1'],$page1)) {
 include /MYDIRECTIORY/($_GET['page1'].".php");


The Remaining Codes: To call a page you would use [a href=\"http://www.domain.com/index.php?page1=type1\" target=\"_blank\"]http://www.domain.com/index.php?page1=type1[/a]
The page its calling would reside in the same directory, named type1.php

<?php
$page1 = array("type1","type2","type3");
$page2 = array("type1","type2","type3");
$page3 = array("type1","type2","type3");
$page4 = array("type1","type2","type3");

if (isset($_GET['page1']) && in_array($_GET['page1'],$page1)) {
 include ($_GET['page1'].".php");
}
elseif (isset($_GET['page2']) && in_array($_GET['page2'],$page2)) {
 include($_GET['page2'].".php");
}
elseif (isset($_GET['page3']) && in_array($_GET['page3'],$page3)) {
 include($_GET['page3'].".php");
}
elseif (isset($_GET['page4']) && in_array($_GET['page4'],$page4)) {
 include($_GET['page4'].".php");
}
else {
 include("includes/main.php");
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users