Dynamic data in codeigniter - php

I am new in codeigniter and i am working on ecommerce template. By default whenever index() has been hit, it's showing all products in different sections (in html) and I want to make this dynamic so should I use queries in index() for get different type of records or there is any other way? This is my code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
//echo "hello world";
$this->load->view('index');
}
}
?>

First need to create database and table as per your requirement.
Then you can create model for reference check this link : https://www.codeigniter.com/userguide3/general/models.html
<?php
class Blog_model extends CI_Model {
public function get_data_first()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
public function get_data_second()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
}
?>
After making model go to your controller and include it like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$this->load->model('my_model');
$data['first_list'] = $this->my_model->get_data_first();
$data['second_list'] = $this->my_model->get_data_second();
//echo "hello world";
$this->load->view('index', $data);
}
}
?>
Then use you param in index file like :
<html>
<head></head>
<body>
<?php
if($first_list){
foreach($first_list as $each){
echo $each->my_param;
}
}
?>
<?php
if($second_list){
foreach($second_list as $each){
echo $each->my_param2;
}
}
?>
</body>
</html>
I hope this helpful for you.

In Models folder write Home_model.php file for querying database.
Suppose you have a table called 'products'.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home_model extends CI_Model{
public function __construct()
{
parent::__construct();
}
public function getProducts()
{
$this->db->from('products');
$query=$this->db->get();
$out = $query->result_array();
return $out;
}
}
The function 'getProducts' will get you all the products from 'products' table.
Now in your controller load the 'database' library and the model.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('home_model');
$this->load->library('database');
}
public function index(){
$products = array();
$products = $this->home_model->getProducts();
$this->load->view('index',$products);
}
}
In index function function of controller you can call the 'getProduct' function of model.And can pass the data to view.
Documentation link for model.
enter link description here
I hope this helps.

Related

using codeigniter,admin wants to approve user's registration request. But itsn't happening properly

here is my controller page..
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ApproveUser extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('admin/Approve_Model');
}
public function index()
{
$this->load->model('admin/Approve_Model');
$result['data']=$this->Approve_Model->getView();
if(!empty($this->session->userdata('user_id')))
{
$this->load->view('admin/header');
$this->load->view('admin/approveUser',$result);
$this->load->view('admin/footer');
}
else{
redirect(site_url('Pages/index'));
}
}
while running the code, it is redirecting to my index page
Try this:
if(!isset($this->session->userdata('user_id')))
{
$this->load->view('admin/header');
$this->load->view('admin/approveUser',$result);
$this->load->view('admin/footer');
}
else
{
redirect(site_url('Pages/index'));
}

Codeigniter: Pass data from model to controller but didn't get in the view file

I was returning query->result() from my model to controller and in the controller I passed the $data to the view file but I am not getting any data in the view file.
Here is the show_model.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function get_post_data()
{
$query=$this->db->get('posts');
return $query->result();
}
}
Here is the show.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show extends CI_controller {
public function __construct()
{
parent::__construct();
}
function show_post_data(){
$this->load->model("Show_model");
$data['records'] = $this->Show_model->get_post_data();
$this->load->view('list_view',$data);
}
}
Here is the show_list.php view file:
<div>
<?php echo "<pre>"; print_r($records); exit; ?>
</div>
The name of your view is show_list and in the controller you are using list_view
simply replace the code in the controller for displaying the view with
function show_post_data(){
$this->load->model("Show_model");
$data['records'] = $this->Show_model->get_post_data();
$this->load->view('show_list',$data);
}

Codeigniter - How to open specific page depend screen size

I have code in controller Codeigniter:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent:: __construct();
$this->load->model('berita');
}
public function index()
{
$data['newsone'] = $this->berita->get_one_top_data();
if(width.screen > 1000px){ // i need the true code in here
$this->load->view('home_view_1',$data);
}
else{
$this->load->view('home_view_2',$data);
}
}
}
how can i open specific page depend on screen size...?

Can I load and use a model in MY_Controller using CI3

I'm using Codeigniter 3 and I need some data available to all methods. I will query the data from the database and then I need to display it on every page.
I have created a MY_Controller extending CI_Controller and saved it in /application/core/
But I am getting this error:
Fatal error: Call to undefined method Area_model::get_user_locations()
MY_Controller:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
$this->location_data = $this->area_model->get_user_locations($org_id);
}
}
Can i access models and the database from within MY_controller?
Area_model.php
// get all areas for an organisation
public function get_user_locations($org_id) {
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}
MY_Controller
not used to direct any function to __construct()
__construct to only load to any model
write other function to MY_Controller look like this
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
}
}
function functionname()
{
$this->location_data = $this->area_model->get_user_locations($org_id);
}
Area_model.php
public function get_user_locations($org_id)
{
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}

Passing common title to all view and models CodeIgniter

I have this controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('text');
}
public function index()
{
$this->home();
}
public function home()
{
$data['title']="Somesite";
$this->load->view("view_home", $data);
}
public function blog()
{
$data['title']="Somesite";
$this->load->view("view_blog", $data);
}
public function answers()
{
$data['title']="Somesite";
$this->load->view("view_answers", $data);
}
}
As you may see $data['title'] is same for all functions, how to make it simpler, to include at the beggining and not to write it in every function, repeat again, and then transfer to view.
Is there a way to tranfer it to function?
Before construct function add this:
public $data = array();
Then in the construct function write:
$this->data['title']="Somesite";
And finally before load view add this:
$data = $this->data + $data;
Now you have same $title everywhere.
Here si simple solution and elegant for transfering one variable to all views :)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
//Class-wide variable to store stats line
protected $title;
function __construct()
{
parent::__construct();
$data->title = "Some site";
$this->load->vars($data);
}
I'm using this method in every projects.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
//Global variable
public $outputData = array();
public $loggedInUser;
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->helper('url');
$this->load->view('users/users');
}
public function register() {
parent::__construct();
$this->load->helper('url');
$this->load->model('users_model');
//get country names
$countryList = $this->users_model->getCountries();
$this->outputData['countryList'] = $countryList;
$this->outputData['pageTitle'] = "User Registration";
$this->load->view('users/register',$this->outputData);
}
}
View file
<?php if(isset($pageTitle)) echo $pageTitle; ?>
<?php
if(isset($countryList)){
print_r($countryList);
}
?>

Categories