How should I add my model into my controller to display the values in drop down ?
The error I get: Invalid argument supplied for foreach()
I didn't use any framework. Just native php. I am new in MVC and PHP please help!
My Controller:
<?php
class Index extends Controller {
function __construct(){
parent::__construct();
}
function index(){
$landArray = $this->model->fetchData();
$this->view->render('index/index');
var_dump($landArray); // display all row in database
}
}
Model:
<?php
class Index_Model extends Model {
function __construct(){
parent::__construct();
}
function fetchData(){
$selectIsland = $this->connection->prepare("SELECT island_id, island
from island" );
$selectIsland->setFetchMode(PDO::FETCH_ASSOC);
$selectIsland->execute();
$islandResult = $selectIsland->fetchAll();
return $islandResult;
}
}
View:
<select>
<option value="">--- Select Island---</option>
<?php
foreach($islandResult as $row){
echo '<option value="'.$row['island_id'].'">'.$row['island'].'</option>';
endforeach
}?>
</select>
This is my library View and render method.
<?php
class View {
function __construct(){
}
public function render($name, $noInclude = false, $landArray){
if($noInclude == true){
require 'views/'.$name.'.php';
}else{
require 'views/header.php';
require 'views/'.$name.'.php';
require 'views/foother.php';
}
}
}
}
You never called fetchData() method of your model. In MVC controller controls everything. in your controller you have to call methods from model. then pass those to your view file.
<?php
class Index extends Controller {
function __construct(){
parent::__construct();
}
function index(){
$landArray = $this->model->fetchData();
$this->view->render('index/index', false, array('islandResult' => $landArray)); // as we are sending param to view and render accept that as 3rd param so we need to specify the 2nd param too!
}
}
Model:
<?php
class Index_Model extends Model {
function __construct(){
parent::__construct();
}
function fetchData(){
$selectIsland = $this->connection->prepare("SELECT island_id, island
from island" );
$selectIsland->execute();
$islandResult = $selectIsland->fetchAll();
return $islandResult; //if you need something to pass to view from db first you have to pass it to controller from model
}
}
View:
<select>
<option value="">--- Select Island---</option>
<?php
foreach($islandResult as $row){
echo '<option value="'.$row['island_id'].'">'.$row['island'].'</option>';
}
?>
</select>
View/Render Library Method:
class View {
function __construct(){
}
public function render($name, $noInclude = false, $arrayParam = array()){ //look closely...here i've made 3rd parameter as default argument so that your other codes which don't need to send param to view works smoothly.
if(count($arrayParam) > 0){
extract($arrayParam);
}
if($noInclude == true){
require 'views/'.$name.'.php';
}else{
require 'views/header.php';
require 'views/'.$name.'.php';
require 'views/foother.php';
}
}
}
Now you'll be able to use parameters in view which you are sending from controller. whatever you pass as array key in controller can be used as variable in view now.
N.B: check my comments from code.
Related
I am new to CodeIgniter and experiencing the following error after connecting the db . It is really great if some one can help me. I checked previous answers in the StackOverflow but none helped me.
Below is the controller code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends MY_Controller {
public function index() {
$this->load->view('home');
}
public function adminRegister() {
//loading teh queries model
$this->load->model('queries');
//Calling the getRoles function inside the roles
$roles = $this->queries->getRoles();
print_r($roles);
exit();
$this->load->view('register');
}
public function login() {
echo 'login';
}
}
Model code
<?php
//Details of queries whicha re used to interact with teh database
class Queries extends CI_Model {
//Created a function with the name getRoles
public function getRoles() {
//Fetch the reocrds from the table
$roles = $this->db->get('tbl_roles');
if ($roles->num_rows() > 0) {
return $roles->results();
}
}
}
?>
Currently not using the data coming from controller. Any help will be highly appreciated. Thanks in advance.
There is no results it should be result
Refer CI Doc
class Queries extends CI_Model {
//Created a function with the name getRoles
public function getRoles() {
//Fetch the reocrds from the table
$roles = $this->db->get('tbl_roles');
if ($roles->num_rows() > 0) {
return $roles->result();
}
}
}
I have a custom library and need to pass some variables under specific method to my controller, then views.
Library code
Class Data {
function dviews($sitedata) {
$site1 = "www.site1.com";
$site2 = "site2.com";
}
}
My controller file
class Webs extends CI_Controller {
public function index()
{
$this->load->library('Data');
$data = $this->Data->dviews();
$this->load->view('pages/websites', $data);
}
}
and I pass the $data array using print_r on views.
is that correct or there is another way to get the list of these vars from library file and pass them?
You are setting variable inside method but returning nothing, so
Modify
Class Data {
function dviews($sitedata) {
$site1 = "www.site1.com";
$site2 = "site2.com";
}
}
To
Class Data {
function dviews($sitedata) {
return array( 'site1' => "www.site1.com", 'site2' => "site2.com");
}
}
Controller
class Webs extends CI_Controller {
public function index()
{
$this->load->library('Data');
$data = $this->Data->dviews();
// now print_r($data); will have array which you returned
$this->load->view('pages/websites', $data);
}
}
and in your view, pages/websites.php you can access like below
<?php echo $site1; ?>
<?php echo $site2; ?>
contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.
In my controller I want to declare a global variable which will always fetch all areas from my db
currently I'm passing $data['dropdowns']
in all methods in my class
{
$data['dropdowns']=loading some other model method
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=loading some other model metod
$this->load->view('commons/header',$data);
}
the thing is I want to now send $data['area'] to all the views without having to declare it again and again in each method
$data['area']= $this->area_model->get_all_locations();
You want to add global variable , but as per my suggest to use global function to use any where to using to send parameter, so please check below my code.
Note : please load your model in application/config/autoload.php file
This is simple demo :
controller
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name');
$this->load->view('commons/header',$data);
}
{
$data['dropdowns']=$this->your_model_name->get_records('table_name','select field like id, name,user_name');
$this->load->view('commons/header',$data);
}
Your model
function get_records($table_name,$field_name)
{
$this->db->select("$field_name");
$this->db->from("$table_name");
$query=$this->db->get();
return $query->result_array();
}
create a base_controller and placed in application/core
class base_controller extends CI_Controller
{
public $area = array();
function __construct()
{
// Call the Controller constructor
parent::__construct();
$this->get_area();
}
public function get_area() {
$this->load->model('area_model');
$this->area= $this->area_model->get_all_locations();
}
}
now $this->area is available in all controller which extends base_controller and all common functionality you can put here
class homepage extends base_controller{
function __construct()
{
// Call the Controller constructor
parent::__construct();
}
public function index()
{
$data = $this->area; // call this wherever u need
$this->load->view('commons/header',$data);
}
}
importantly $this->area; one can use directly inside view
Create a helper for your custom functions
Eg: custom_helper.php then load your custom helper in autoload.php
In custom_helper.php, create a function for getting area.
if (!function_exists('get_area')) {
function get_area() {
$CI = & get_instance();
$area= $CI->area_model->get_all_locations();
return $area;
}
}
You can call get_area() in your views without declaring in controllers..
I have this simple PHP code where I have created a new model with a celebrity name, a controller that accesses the celebrity name from the model, and a view to display the name. For some reason using the path below I can't display the name accessing the controller. I can't find out what I am missing or doing wrong.
Thanks for the help!
http://localhost/ci/Starcontroller/guess
Controller->
<?php
class Starcontroller extends CI_Controller{
function __construct() {parent::__construct();}
function guess()
{
$this->load->model('starmodel');
$newstarr = $this->starmodel->lookup();
$this->load->view('starview',$newstarr);
}
}
?>
Model->
<?php
class starmodel extends CI_Model{
function __construct()
{
parent::__construct();
}
public function lookup(){
$celebrity = "Anna";
return $celebrity;
}
}
?>
View ->
<html>
<head></head>
<body>
<?php
echo $newstarr;
?>
</body>
</html>
First initial alphabet of Class name and File name of controller and model are in capital letter's check your model class name
Then in controller change to this
<?php
class Starcontroller extends CI_Controller {
function __construct() {
parent::__construct();
// Would load model in here and other helpers and libraries.
// So can access through all functions on controller.
// $this->load->model('starmodel');
}
function guess() {
$this->load->model('starmodel');
$newstarr['data'] = $this->starmodel->lookup();
$this->load->view('starview',$newstarr);
}
}
In view
<?php echo $data ?>
From controller, you would like to pass an associative array of data to the view.
So change this part:
$newstarr = $this->starmodel->lookup();
$this->load->view('starview',$newstarr);
To this:
$data['newstarr'] = $this->starmodel->lookup();
$this->load->view('starview',$data);
Rest of your code may stay the same.