Reputation: 2029
currently learning CodeIgniter using version 3.0,PHP 5.5, MySQL on WAMP and Sublime Text 3 as my editor. I'm attempting to load a view but the page is blank with no error even though error_reporting is turned on. When I echo my array, it displays the data. Below are my table and code
Below are the code for my model, control and view in that order
MODEL (product.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Model {
function get_products() {
$this->db->select()->from('products')->order_by('name','desc');
$query=$this->db->get();
return $query->result_array();
}
}
CONTROLLER (products.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->model('product');
$data['products']=$this->product->get_products();
$this->load->view('pro_index', $data, TRUE);
//echo "<pre>"; print_r($data['products']); "</pre>";
}
}
VIEW (pro_index.php)
<!DOCTYPE html>
<html lang="">
<head>
<!-- Required meta tags always come first -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title Page</title>
</head>
<body>
<?php
if (isset($products)) {
foreach ($products as $row) {
echo "<div>"."<h2>".$row["name"]."</h2>"."</div>";
echo "what is this?";
}
}
?>
</body>
</html>
What could i possibly be doing wrong?
Upvotes: 0
Views: 122
Reputation: 340
You were returning the array data with "$data" to your view page and you are looping with "foreach ($products as $row)". You have to change the foreach loop $products to $data.
In model
$this->load->view('pro_index', $data, TRUE);
In view page
foreach ($products as $row)
Upvotes: 0
Reputation: 61
In your Controller, try removing the third parameter (TRUE).
This is used for returning data (such as JSON, etc.)
//this->load->view('pro_index', $data, TRUE);
this->load->view('pro_index', $data);
Upvotes: 1
Reputation: 1574
try this
public function index()
{
$this->load->model('product');
$this->data['products']=$this->product->get_products();
$this->load->view('pro_index', $this->data, TRUE);
}
Upvotes: 0