AddcitedToLearn
AddcitedToLearn

Reputation: 398

PHP only getting the first element of a array

Hi i'm making a login and register script. I hava global array for my Config. But when i'm trying to make connection to my database. He can't get it from the array only the host. He won't go deeper inside it. Only thing i get is 127.0.0.1 for each thing i want to have so like i want to have the username i will get 127.0.0.1. I have no idea whats wrong but i think somethings wrong in my Config.php. Also this is my output on my screen. Someone know what i'm doing wrong ?

I only get the host array back. And when i want the username or db name back i get the host. enter image description here Here is al my code.

index.php

<?php
require_once 'core/init.php';


DB::getInstance();

Init.php

    <?php
session_start();

$GLOBALS['config'] = array(
    'mysql' => array(
                    'host'      => '127.0.0.1',
                    'username'  => 'root',
                    'password'  => '',
                    'db'        => 'login'

                    ),
    'remember' => array(
                    'cookie_name'   => 'hash',
                    'cookie_expiry' => '648000'
    ),

    'session' => array(
        'session_name' => 'user'
    )
);

spl_autoload_register(function($class){
    require_once 'classes/' . $class . '.php';
});

require_once '/functions/sanitize.php';

Config.php

<?php
class Config{
    public static function get($path = null){
        if($path){
        $config = $GLOBALS['config'];
        foreach($config as $key =>$value){
            if(isset($value)){
                $config1 = $value;
                foreach ($config1 as $key =>$list){
                    return  $list;
                }
            }

        }
          return false;
        }    

    }
}

DB.php

<?php

class DB{
    private static $_instance = null;
    private $_pdo, $_query, $_error = false, $_result, $_count = 0; 

    private function __construct(){
        try{
            $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
        }catch(PDOException $e){
            echo Config::get('mysql/host') . Config::get('mysql/db') . Config::get('mysql/db'); // Only for testing getting 127.0.0.1 back for each of them.

        }
    }


    public static function getInstance(){
        if(!isset(self::$_instance)){
            self::$_instance = new DB();
        }
        return self::$_instance;
    }
}

Upvotes: 0

Views: 105

Answers (2)

AddcitedToLearn
AddcitedToLearn

Reputation: 398

After a rewrite of my Config.php class it worked.

<?php
class Config{
    public static function get($path = null){
        if($path){
            $config = $GLOBALS['config'];
            $path = explode('/', $path);

            foreach($path as $bit){
                if(isset($config[$bit])){
                    $config = $config[$bit];
                }
            }   
            return $config;
        }
    }
}

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

private static $_istance = null;

Tyyyypoooooooooooo :) n missing

Edit

Typoooooooooo number 2

'msql' => array(

Should by mysql, that is why your config doesnt load.

Upvotes: 5

Related Questions