PHP MVC data error

general-dev
scripts

#1

I have found a mini MVC on Github which im now building on top of for my own system but right now i am experiencing errors when trying to display data on the view pages.

The URL goes like this -> https://domain.com/user/profile/admin

Unfortunately I get this error which i'm really confused about.

Array ( [:username] => admin ) SELECT * FROM jh_users WHERE username = :username; 

Warning: Invalid argument supplied for foreach() in /customers/a/d/5/domain.com/httpd.www/app/core/Controller.php on line 36 Warning: Invalid argument supplied for foreach() in /customers/a/d/5/domain.com/httpd.www/app/helpers/view.php on line 18

Current version is PHP 7.1.16 - Warning: Invalid argument supplied for foreach() in /customers/a/d/5/domain.com/httpd.www/app/helpers/view.php on line 18

Here my code for the view.php:

function extend_view($paths, $data)
{
    // Set each index of data to its named variable.
    foreach($data as $key => $value) {
        $$key = $value;
    }

    foreach ($paths as $path) {
        include (VIEWS_PATH . $path . '.php');
    }
}

Here is my controller code:

<?php if (!defined('BASE_PATH')) exit('No direct script access allowed');

class User extends Controller
{
    public $user;

    public function __construct()
    {
        $this->load_helper(['view']);
        $this->user = $this->model('Users'); // Instantiate a new user object.
    }
    
    public function index($param1 = '', $param2 = '')
    {
        $this->view('home');
    }

    // Show our User Profile report
    public function profile($user_id)
    {
       // Get the user by id. (much cleaner)
        $data = $this->user->getUserByUserId($user_id);

        // Load the View passing to it the Users information as $data.
        $this->view('user/profile', $data);     
    }
}

I checked all my db connections which are fine and successful and tried it on a different host but no luck! Can anyone see what is wrong?

Thanks


#2

This seems a bit suspicious :

foreach($data as $key => $value) {
    $$key = $value;

Or did you perhaps edit your code while creating the question?


#3

i removed the two $$ but error still occurs


#4

To be honest I'm not sure I really want to get into this but I'll give it a try.

Start by seeing what your user data looks like:

$data = $this->user->getUserByUserId($user_id);
var_dump($data); die();

#5

i get this:
Array ( [:username] => admin ) SELECT * FROM jh_users WHERE username = :username;NULL

Thanks


#6

Is that an error message? Is that a complete copy? Because it makes no sense to somehow have the sql catted onto array data. That actually looks more like query data. I fear you will have to dig into the $this->user->getUserByUserId() code to see what is going on.

One thing to be aware of, there are literally hundreds (if not thousands) of definitions of "php mvc". So that by itself does not tell us anything. You might consider revealing what " mini MVC on Github" actually means.


#7

Sorry! I have found the bug! It was not really even a bug! In my database there was no username column so the php had no where to look!

Sorry for wasting your time but thanks for your quick response! - Something so simple that has taken 5 hours to fix!

Thanks


#8

Glad you got it working. It certainly didn't waste my time as no one forced me to respond. That fact that you responded quickly is a good thing.


#9

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.