我是编程的新手,所以我按照我认为适合我的方式编写逻辑。对你们中的一些人来说,这可能看起来很愚蠢,但在学习过程中。我正在尝试使用php if
和elseif
条件来显示基于url变量的信息。当我只有一个条件时它工作正常但是我想知道如何在同一页面上使用多个条件而不会得到未定义的索引错误?或许我的逻辑有缺陷?
这是我正在尝试做的事情:
点击链接获取变量
在网址中抓取变量
使用变量显示仪表板上的特定内容
leftsidemenu.php
<a href='home.php?viewcompanystructure=companystructure&company=<?php echo(rand(100000000,100000000000));?>'>Company Structure</a>
<a href='home.php?setupqualifications=qualificationssetup&company=<?php echo(rand(100000000,100000000000));?>'>
Qualifications Setup
</a>
<a href='home.php?viewprojectsclients=projectsclients&company=<?php echo(rand(100000000,100000000000));?>'>
Projects/Client Setup</a>
dashboard.php
<?php
$companytable = $_GET['viewcompanystructure'] ;
if($companytable == 'companystructure') :
include 'tables/companytable.php';
return true;
?>
<?php
$qualificationsetuptable = $_GET['setupqualifications'] ;
elseif($qualificationsetuptable == 'qualificationssetup') :
include 'tables/qualificationsetuptable.php'
?>
<?php
$projectclientstable = $_GET['viewprojectsclients'] ;
elseif($projectclientstable == 'projectsclients'):
include 'tables/projectclientstable.php'
?>
<?php else: ?>
Display something else here.
<?php endif; ?>
答案 0 :(得分:0)
如果您所做的只是选择要使用的内容,我建议您替换代码布局。
给定网址home.php?view=companystructure
...
switch( ( isset( $_GET['view'] ) === true ) ? $_GET['view'] : null )
{
case 'companystructure':
include('tables/companytable.php');
break;
case 'setupqualifications'
include('tables/qualificationsetuptable');
break;
case 'viewprojectsclients':
include('tables/projectclientstable.php');
break;
default:
include('dosomethingelse.php');
break;
}