how to use limit in codeigniter controller - php

I am doing ajax scrolling pagination. Controller is like below
<?php
require APPPATH . '/controllers/user/Usercontroller.php';
class PaginationController extends Usercontroller{
public function __construct(){
parent::__construct();
$this->load->model("Usermodel","",true);
}
private $perPage = 5;
public function index(){
$tutorId=$this->session->userdata('cp_userid');
$categorycourses=$this->Usermodel->getcourses($tutorId);
$count = $categorycourses->num_rows();
$data['details']=$categorycourses;
$this->load->view("user/pagination_view",$data,true);
}
}
?>
$count variable contains 5 data, I want to send 3 for first page and remaining for second page. How to do this.

Try this...
Model:
function getcourses($tutorId, $limit = null) {
$this->db->select('*');
$this->db->from('courses');
$this->db->where("tutorId", $tutorId);
//$this->db->order_by("name", "asc");
if($limit!=''){
$this->db->limit($limit);
}
$query = $this->db->get();
$courses = array();
foreach ($query->result() as $row)
array_push($courses, $row);
return $courses;
}
Controller:
public function index(){
$tutorId = $this->session->userdata('cp_userid');
$limit = 3;
$categorycourses = $this->Usermodel->getcourses($tutorId, $limit);
//$categorycourses=$this->Usermodel->getcourses($tutorId);
$count = $categorycourses->num_rows();
$data['details'] = $categorycourses;
$this->load->view("user/pagination_view", $data, true);
}

Related

codeigniter 3: does my code follow MVC rule

I am new to codeigniter 3 and try to convert my PHP project to MVC by this Framework
Controller file
class home extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('frontend/M_Headers');
}
public function index()
{
$data['slide_image'] = $this->M_Headers->get_all_slide();
$this->load->view('frontend/headers',$data);
}
}
Model file
class M_Headers extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_all_slide()
{
$query = $this->db->get('tbl_slide');
return $query->result();
}
}
View file
<div class="slider">
<ul class="rslides" id="slider">
<?php
if(count($slide_image) > 0)
{
foreach($slide_image as $value):
$get_image=$value->sl_image;
if($get_image != ''):
$image_properties = array(
'src' => 'assets/images/slide/'.$get_image,
'alt' => '',
);
?>
<li><?php echo img($image_properties); ?></li>
<?php else: ?>
<li>No Slide</li>
<?php
endif;
endforeach;
}
?>
</ul>
</div>
did am i right that loop result in View or should i do it in
Controller?
which query should i use between active record or bind (where id=?) or they had it own benefit in difference situation?
The loop in the view is correct. Because it is "view logic" (repeating li's) you can use into the view.
Your Model can be better. I personally like to use the model as a representation of the item from the database. Like this:
class M_Headers extends CI_Model
{
private var $tablename = "tbl_slide";
var $id;
var $sl_image;
public function __construct()
{
parent::__construct();
}
public function get_all_slide()
{
$slides = array();
$query = $this->db->get($this->tablename);
foreach($query->result() as $row)
{
$item = new self();
$item->id = $row->id;
$item->sl_image = $row->sl_image;
$slides[] = $item;
}
return $slides;
}
public function get_slide($id)
{
$this->db->where("id", $id);
$query = $this->db->get($this->tablename);
$results = $query->result();
if(isset($results[0]))
{
$row = $results[0];
$this->id = $row->id;
$this->sl_image = $row->sl_image;
return true;
}
return false;
}
}
You can even improve this to make a method that loads the row into the model:
class M_Headers extends CI_Model
{
private var $tablename = "tbl_slide";
var $id;
var $sl_image;
public function __construct()
{
parent::__construct();
}
private function load_with_record($row)
{
$this->id = $row->id;
$this->sl_image = $row->sl_image;
}
public function get_all_slide()
{
$slides = array();
$query = $this->db->get($this->tablename);
foreach($query->result() as $row)
{
$item = new self();
$item->load_with_row($row);
$slides[] = $item;
}
return $slides;
}
public function get_slide($id)
{
$this->db->where("id", $id);
$query = $this->db->get($this->tablename);
$results = $query->result();
if(isset($results[0]))
{
$row = $results[0];
$this->load_with_row($row);
return true;
}
return false;
}
}

run multiple select quires in one page using codeigniter

Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}

Load multiple models within same function of controller

I have two functions in my model as
class Jobseeker_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function result_getall($id)
{
$this->db->select('*');
$this->db->from('tbl_jobseeker');
$this->db->where('tbl_jobseeker.User_id',$id);
$this->db->join('tbl_work_exp', 'tbl_jobseeker.User_id = tbl_work_exp.User_id','left');
$query = $this->db->get();
return $query->row();
}
public function select($id)
{
$this->db->select('*');
$this->db->from('tbl_qualification');
$this->db->where('tbl_qualification.User_id',$id);
$query = $this->db->get();
return $query->result();
}
}
And in my controller I have a function as
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$res['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data,$res);
}
It is not possible to display the view page.. I could pass two variables into my view page.right?
You can pass your any number of variables/arrays using a single array.
In Controller:
public function display() {
$id = $this->session->userdata('user_id');
$data['var1'] = $this->jobseeker_model->result_getall($id);
$data['var2'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
In View:
`$var1` and `$var2` will be available.
You can pass your two variable using single srray
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}
Views
foreach($a as $data){
// your code
}
echo $row->column_name;
Try this
public function display()
{
$id = $this->session->userdata('user_id');
$data['row'] = $this->jobseeker_model->result_getall($id);
$data['a'] = $this->jobseeker_model->select($id);
$this->load->view('jobseeker_display.php', $data);
}

Codeigniter subdomain wildcard get account every method

I'm using Codeigniter with dynamic subdomains, but in each method of my controllers I need to get the account of the dynamic subdomain. I'm looking for a way to get the domain and add to the $data without to it every method like:
<?php
class Dashboard extends CI_Controller {
function index()
{
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
$this->db->from('accounts')->where('subdomain', $subdomain_name);
$query = $this->db->get();
$account = $query->row();
$data['account_id'] = $account->id;
$data['account_name'] = $account->name;
$this->load->view('index', $data);
}
function clients()
{
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
$this->db->from('accounts')->where('subdomain', $subdomain_name);
$query = $this->db->get();
$account = $query->row();
$data['account_id'] = $account->id;
$data['account_name'] = $account->name;
$this->load->view('clients', $data);
}
}
Do it once within a Class Constructor and then you can access the same variables from all of the other methods.
As per docs:
"Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. Constructors can't return a value, but they can do some default work."
<?php
class Dashboard extends CI_Controller {
public $data = array();
public function __construct()
{
parent::__construct();
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain_name = $subdomain_arr[0];
$this->db->from('accounts')->where('subdomain', $subdomain_name);
$query = $this->db->get();
$account = $query->row();
$this->data['account_id'] = $account->id;
$this->data['account_name'] = $account->name;
}
public function index()
{
$data = $this->data; // contains your values from the constructor above
$data['title'] = "My Index"; // also use your $data array as normal
$this->load->view('index', $data);
}
public function clients()
{
$data = $this->data; // contains your values from the constructor above
$this->load->view('clients', $data);
}
}
NOTE: Even though CodeIgniter functions default to "public", it's best practice to declare them as such. See: Public functions vs Functions in CodeIgniter

Paginating query results with Codeigniter

I'm new to Codeigniter and PHP in general, so bear with me.
I'm attempting to use the codeigniter pagination class to paginate the results returned from a query. The code below works fine with static queries that don't require any parameters passing. However, it seems to break down when attempting to pass a variable as a parameter in the URL e.g.
localhost/index.php/termsbyletter/index/a
where 'a' is the $letter variable passed to the controller/model.
PHP isn't outputting any errors and the query performs as expected, as does the record_count function within the model. The result is that all of the query results are displayed, but all on the same page, which stays the same when the pagination links are clicked.
Also, in the model, is there a more efficient way of returning the row count than running the query twice? I've read on here that this is necessary, and I haven't had any success trying to pass this value any other way.
Here is my controller:
<?php
class Termsbyletter extends CI_Controller
{
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("terms_by_letter");
$this->load->library("pagination");
}
public function index($letter) {
$config = array();
$config["base_url"] = base_url() . 'index.php/termsbyletter/index/' . $letter;
$config["total_rows"] = $this->terms_by_letter->record_count($letter);
$config["per_page"] = 3;
$config["uri_segment"] = 5;
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data["results"] = $this->terms_by_letter->term($config["per_page"], $page, $letter);
echo $this->pagination->create_links();
$this->load->view("/templates/header");
$this->load->view("/terms/index", $data);
$this->load->view("/templates/footer");
}
}
and the model:
class Terms_by_letter extends CI_Model
{
public function __construct() {
parent::__construct();
$this->load->database();
}
public function record_count($letter) {
$query = $this->db->query("SELECT * FROM news WHERE LEFT(slug, 1) = '$letter'");
return $query->num_rows();
}
public function term($limit, $start, $letter) {
$this->db->limit($limit, $start);
$query = $this->db->query("SELECT * FROM news WHERE LEFT(slug, 1) = '$letter'");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
and I'm using something like this to output the results in the view:
<?php
foreach($results as $data) {
echo $data->slug "<br>";
}
?>
<p><?php echo $links; ?></p>
You're trying to mix active records with normal CI queries and that won't work. Change the query to:
$query = $this->db->query("SELECT * FROM news WHERE LEFT(slug, 1) = '$letter' LIMIT $start, $limit");
Either that or go the active record route entirely with:
$this->db->limit($limit, $start);
$this->db->where('LEFT(slug,1)',$letter);
$this->db->get('news');
As far as I know there is no way to return all the results you need with a single query, the controller needs total records to figure out the pagination, then it needs to select just the records for one page.
You could however just write the query once and change the parameters a bit.
public function term($limit, $start, $letter) {
if($limit > 0)
{
$this->db->limit($limit, $start);
}
$this->db->where('LEFT(slug,1)',$letter);
$this->db->get('news');
Then in your controller you'd get the count like this:
$config["total_rows"] = $this->terms_by_letter->term(0,0,$letter);

Categories