2016-02-12 13 views
0

我有一段代碼看起來沒有工作,因爲它在讀出數組時出錯。User :: getvar給出未定義的偏移量

即時通訊使用此功能得到用戶的數組變量:當我使用例如

static function getVar($v) { 
    if(in_array($v, user::$struc)) { 
    return self::$user[user::$struc[$v]]; 
    } 
    else die("getVar: ".$v." > is unkown in struc"); 
} 

現在我得到一個錯誤:

user::getVar('Klantnummer'); 

它給了我一張告示:

注意:第79行的C:\ xampp \ htdocs \ Snackbar \ System \ Modules \ User.php中的未定義偏移量:0 79行是t his:return self :: $ user [user :: $ struc [$ v]];

我已經把上的var_dump登錄,看它是否把數據放入數組,如下圖所示即是工作的罰款:

array(9) { 
    ["Klantnummer"]=> string(1) "2" 
    ["LoginNaam"]=> string(5) "Thimo" 
    ["Wachtwoord"]=> string(128) "SPOOFED OUT 
    ["Voornaam"]=> string(5) "Thimo" 
    ["Achternaam"]=> string(7) "Franken" 
    ["Adres"]=> string(10) "Poortugaal" 
    ["Huisnummer"]=> string(2) "11" 
    ["Plaats"]=> string(10) "Poortugaal" 
    ["Postcode"]=> string(6) "3176VE" 
} 

現在這裏是$struc數組:

static $struc = array(
    "Klantnummer" => 0, 
    "LoginNaam"  => 1, 
    "Wachtwoord" => 2, 
    "Voornaam"  => 3, 
    "Achternaam" => 4, 
    "Adres"   => 5, 
    "Huisnummer" => 6, 
    "Plaats"  => 7, 
    "Postcode"  => 8 
); 

任何人都知道什麼可能是錯的?

的完整代碼:

<?php 
class user { 


static $struc = array(
    "Klantnummer" => 0, 
    "LoginNaam"  => 1, 
    "Wachtwoord" => 2, 
    "Voornaam"  => 3, 
    "Achternaam" => 4, 
    "Adres"   => 5, 
    "Huisnummer" => 6, 
    "Plaats"  => 7, 
    "Postcode"  => 8 
); 

static $ses = "loggedin"; 
static $hash = "whirlpool"; 
static $user = array(); 

static $usda = "userdat"; 

static function login($username, $password) { 
    $host =  'localhost'; 
    $database = 'Snackbar'; 
    $user =  'root'; 
    $password = ''; 

    try { 
     $conn = new PDO('mysql:host='.$host.';dbname='.$database, $user, $password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } 
    catch(PDOException $e) { 
     $msg = "Regelnummer: ".$e->getLine()."<br/> 
       Bestand: ".$e->getFile()."<br/> 
       Foutmelding: ".$e->getMessage().""; 

     trigger_error($msg); 
    } 


    $HashPW = hash('whirlpool', $_POST['Password']); 
    $Hashed = strtoupper($HashPW); 

    $Login = $conn->prepare("SELECT * FROM Klanten WHERE LoginNaam = :Naam AND Wachtwoord=:Wachtwoord"); 

    $Login->bindParam(':Naam', $_POST['Name'], PDO::PARAM_STR, 45); 
    $Login->bindParam(':Wachtwoord', $Hashed, PDO::PARAM_STR, 129); 

    $Login->execute(); 


    if($Login->rowCount() > 0) { 
     self::$user = $Login->fetch(PDO::FETCH_ASSOC); 
     $_SESSION[self::$ses] = true; 
     $_SESSION[self::$usda] = self::$user; 
     var_dump(self::$user); 
     print("Succesvol ingelogd"); 

    }  
    else 
    { 
     $_SESSION[self::$ses] = false; 
     $_SESSION[self::$usda] = false; 
     print("Verkeerde login naam en/of wachtwoord"); 
    } 
} 
static function logOut() { 
    $_SESSION[self::$ses] = false; 
    $_SESSION[self::$usda] = false; 

    self::$user = null; 
} 
static function loggedIn() { 
    return $_SESSION[self::$ses]; 
} 
static function getVar($v) { 
    if(in_array($v, user::$struc)) { 
     return self::$user[user::$struc[$v]]; 
    } 
    else die("getVar: ".$v." > is unkown in struc"); 
} 
} 

回答

1

你的方法getVar()有錯誤。 self::$user不存在,這使得self::$user[user::$struc[$v]]未定義。您真正想訪問的是self::$struct[$v]

static function getVar($v) { 
    if(in_array($v, self::$struc)) { 
    return self::$struc[$v]; 
    } 
    else die("getVar: ".$v." > is unkown in struc"); 
} 

查看http://sandbox.onlinephpfunctions.com/code/e4f3a3ada495ec97e16f55b4132b99c9c1f4b4f9的工作示例。

+0

static $ user = array();在我的代碼之上,這就是當用戶登錄時數據被保存到的地方。return self :: $ user [users :: $ struc [$ v]];所以這應該得到匹配$ user的數據,並且也在$ struc中。所以只需將self :: $ struc [$ v]加入就行。我需要$ user數組的數據 –

+0

請發佈您的完整代碼。 – maxhb

+0

maxhb,你有什麼想法是錯的? –