Jump to content

Archived

This topic is now archived and is closed to further replies.

onekoolman

Nested PHP switch statement problems

Recommended Posts

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.

[code]<?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;

}

?>[/code]

Share this post


Link to post
Share on other sites
Sorry, but your code makes little sense. Why are your url passed variables setup like this?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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:
[code]<?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);
}
?>[/code]
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I modified my original suggestion to fit your constraints. Here is the code:
[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>[/code]
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

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Please post you're working code in case someone else has the same problem then they can learn from your solution.

Ken

Share this post


Link to post
Share on other sites
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:

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


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

[code]<?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");
}
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

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.