[英]Grabbing multiple variables from a url in php
I'm new to programming so I write logic the way I think it works for me. 我是编程的新手,所以我按照我认为适合我的方式编写逻辑。 It may looks stupid to some of you but im in a learning process. 对你们中的一些人来说,这可能看起来很愚蠢,但在学习过程中。 I'm trying to use php if
and elseif
conditions to display information based on the url variable. 我正在尝试使用php if
和elseif
条件来显示基于url变量的信息。 It works fine when I only have one condition however I was wondering how can I make this work with multiple conditions on the same page without getting undefined index errors? 当我只有一个条件时它工作正常但是我想知道如何在同一页面上使用多个条件而不会得到未定义的索引错误? Or maybe my logic is flawed? 或许我的逻辑有缺陷?
Here what i'm trying to do: 这是我想要做的:
Click on a link to obtain variable 单击链接以获取变量
Grab variable in the url 在网址中抓取变量
Use variable to display something specific on the dashboard 使用变量显示仪表板上的特定内容
leftsidemenu.php 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 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; ?>
If all you're doing is selecting which include to use, I'd like to suggest a alternative to your code layout. 如果你所做的只是选择要使用的包含,我想建议你的代码布局的替代方案。
for the given url home.php?view=companystructure
... 对于给定的网址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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.