Postingan lainnya
Notice: undefined variable: data in c:\xampp\htdocs\latihan2\app\views\home\index.php on line 10
saya sedang belajar php mvc dan baru sampai pada materi model ketika sedang praktek muncul error sesuai dengan judul yaitu Notice: Undefined variable: data in C:\xampp\htdocs\latihan2\app\views\Home\index.php on line 10
kira kira salah nya dmna ya? mohon pencerahan nya thanks sblm nya
controllers/Home.php :
class Home extends Controller
{
function Index()
{
// $data['mhs'] = $this -> model('index_model') -> getData();
$data['nama'] = $this -> model('index_model') -> getData();
$this -> view('templates/header');
$this -> view('home/index', $data);
$this -> view('templates/footer');
}
}
core/Controller.php :
class Controller
{
public function view($view)
{
require_once '../app/views/' . $view . '.php';
}
public function model($model)
{
require_once '../app/model/' . $model . '.php';
return new $model;
}
}
model/index_model.php :
class Index_model
{
// private $dbh;
// private $stmnt;
// function __construct()
// {
// $dsn = 'mysql:host=localhost;dbname=phpmvc';
// try {
// $this -> dbh = new PDO($dsn, 'root','');
// } catch (PDOException $error){
// die($error->getMessage());
// }
// }
private $nama = 'Mohamad Diki Alfin';
function getData()
{
// $this -> stmnt = $this -> dbh -> prepare('SELECT * FROM mahasiswa');
// $this -> stmnt -> execute();
// return $this -> stmnt -> fetchAll(PDO::FETCH_ASSOC);
return $nama;
}
}
views/index.php
<div class="jumbotron">
<h1 class="display-4">Hello, world!</h1>
<p class="lead">This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
<hr class="my-4">
<p>It uses utility classes for typography and spacing to space content out within the larger container.</p>
<a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a>
</div>
<?php var_dump($data['nama']); ?>
0
Tanggapan
coba di print_r dulu itu variable data nya ada apa engga.
itu di view nya gak pakek looping?