2

Overflow community first post ever!

Currently i'm working on a project and have started to use PHP to dynamic insert pages.

At this point in time I have...

switch ($route) {
  case 'step1':
        include('./views/enrolment/mainstream/_step1.php');
        include('./views/partials/_footer.php') ;
        break;
    case 'step2':
        include('./views/enrolment/mainstream/_step2.php');
        include('./views/partials/_footer.php') ;
        break;
    case 'step3':
        include('./views/enrolment/mainstream/_step3.php');
        include('./views/partials/_footer.php');
        break;
    case 'step4':
        include('./views/enrolment/mainstream/_step4.php');
        include('./views/partials/_footer.php');
        break;
    case 'step5':
        include('./views/enrolment/mainstream/_step5.php');
        include('./views/partials/_footer.php');
        break;
    case 'step6':
        include('./views/enrolment/mainstream/_step6.php');
        include('./views/partials/_footer.php');
        break;
    case 'step7':
        include('./views/enrolment/mainstream/_step7.php');
        include('./views/partials/_footer.php');
        break;


This to means seems horrifically long and is better achieved through a for loop, however i'm a newbie to PHP and am entirely unsure of how I would go about implementing this idea.

Any other sugguestions without a the use of a for loop is also welcome!

Thanks guys!

1
  • The only difference between each step is the number in "stepX"? Then... have you heard of variables?
    – deceze
    Nov 6, 2013 at 13:49

8 Answers 8

6

You can check if that file exists using php's file_exists() fn

if(file_exists('./views/enrolment/mainstream/_'.$routes.'.php')) {
    include('./views/enrolment/mainstream/_'.$routes.'.php');
    include('./views/partials/_footer.php') ;
}

Handle the else part to suit your need.

1
  • Absolutely brilliant answers people and so fast! All your answers worked equally and as effectively as one another! But i had to choose one, thank you all in advance for your help!
    – Raptus
    Nov 6, 2013 at 14:09
0

Maybe this easy solution is good for you:

include('./views/enrolment/mainstream/_'.$route.'.php');
include('./views/partials/_footer.php');
1
  • A copy&paste answer? LOL
    – arkascha
    Nov 6, 2013 at 13:51
0

To reduce your code considerably:

include('./views/enrolment/mainstream/_step' . $route . '.php');
include('./views/partials/_footer.php');
0

Why dont use $route as variable to load file?

include("./views/enrolment/mainstream/_{$route}.php");
include('./views/partials/_footer.php') ;
0

Just like @praveen said use:

include('./views/enrolment/mainstream/_'.$routes.'.php');

However you are duplicating:

include('./views/partials/_footer.php');

for each query, why not just have that in the page as static?

0

it is better to check the $route value

if (in_array($route, range(1, 7))){
    include('./views/enrolment/mainstream/_'.$route.'.php');
    include('./views/partials/_footer.php') ;
} else {
    //...error code
}
-1

How about

$i = 1;
$steps = 7;

for($i = 1; $i <= $steps; $i++) {
  if($route === 'step' . $i) {
     include('./views/enrolment/mainstream/_step' . $i . '.php');
     include('./views/partials/_footer.php');
     break;
  }
}
1
  • I think the title of the question is not so right, he will not need a for to do what he want, so this code isn't needed, because you will check 7 times if($route === 'step' . $i) { Mar 18, 2015 at 15:53
-1
for($i = 0; $i < 7; $i++){
    if($route == "step" . $i){
        include('./views/enrolment/mainstream/_step' .$i . '.php');
        include('./views/partials/_footer.php');
        break;
    }
}
1
  • I think the title of the question is not so right, he will not need a for to do what he want, so this code isn't needed, because you will check 7 times if($route === 'step' . $i) { Mar 18, 2015 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.