2017-02-06 54 views
1

我是編程新手,所以我按照我認爲的方式編寫邏輯。這對你們中的一些人來說可能看起來很愚蠢,但是我正在學習過程中。我試圖使用php ifelseif條件來顯示基於url變量的信息。它工作正常時,我只有一個條件,但我想知道如何使這個工作與多個條件在同一頁面上沒有得到未定義的索引錯誤?或者,也許我的邏輯有缺陷?從php中抓取多個變量php

這裏我想要做的事:

  1. 點擊一個鏈接以獲得可變

  2. 在url

    搶變量

  3. 使用變量來顯示特定的東西儀表盤

Picture of Dashboard

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; ?> 
+3

我相信你'返回true;'將導致PHP退出並停止處理其他任何東西。它用於將函數的值返回給調用方法,但由於您沒有使用函數,因此可以將其刪除。 – Andy

+0

當我不''返回true;'我得到未定義的索引從'if語句'沒有被讀取。 –

+0

我假設因爲'$ qualificationsetuptable = $ _GET ['setupqualifications'];'實際上在第一個if語句中,而且你也沒有測試你的$ _GET索引,比如'$ _GET ['viewcompanystructure']'是設置在第一位 – Scuzzy

回答

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; 
} 
+0

我剛剛閱讀關於switch語句我沒有想到它。它是一個「漂亮」的解決方案,但我不能將你標記爲答案,因爲它在我的代碼中不起作用。 –

+0

這很好,我建議確保啓用錯誤報告的所有方面,通常情況下,使用PHP獲得白屏非常罕見,除非出現錯誤。 – Scuzzy