2

溢出社区有史以来第一个帖子!

目前我正在做一个项目,并开始使用 PHP 来动态插入页面。

在这个时间点我有...

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;


这意味着似乎非常长,并且通过 for 循环更好地实现,但是我是 PHP 的新手,完全不确定我将如何实现这个想法。

也欢迎任何其他不使用 for 循环的建议!

多谢你们!

4

8 回答 8

6

您可以使用 php 的 file_exists() fn 检查该文件是否存在

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

处理其他部分以满足您的需要。

于 2013-11-06T13:50:22.803 回答
0

也许这个简单的解决方案对您有好处:

include('./views/enrolment/mainstream/_'.$route.'.php');
include('./views/partials/_footer.php');
于 2013-11-06T13:51:19.447 回答
0

要大大减少您的代码:

include('./views/enrolment/mainstream/_step' . $route . '.php');
include('./views/partials/_footer.php');
于 2013-11-06T13:51:35.767 回答
0

为什么不使用$route变量来加载文件?

include("./views/enrolment/mainstream/_{$route}.php");
include('./views/partials/_footer.php') ;
于 2013-11-06T13:51:52.337 回答
0

就像@praveen 说的那样使用:

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

但是,您正在复制:

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

对于每个查询,为什么不在页面中将其作为静态查询?

于 2013-11-06T13:52:08.790 回答
0

最好检查一下$route价值

if (in_array($route, range(1, 7))){
    include('./views/enrolment/mainstream/_'.$route.'.php');
    include('./views/partials/_footer.php') ;
} else {
    //...error code
}
于 2013-11-06T13:57:29.003 回答
-1

怎么样

$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;
  }
}
于 2013-11-06T13:51:51.447 回答
-1
for($i = 0; $i < 7; $i++){
    if($route == "step" . $i){
        include('./views/enrolment/mainstream/_step' .$i . '.php');
        include('./views/partials/_footer.php');
        break;
    }
}
于 2013-11-06T13:52:10.143 回答