How to create pagination with Codeigniter? - php

I want to create pagination in a search bar in CodeIgniter, but I don't get actual pagination.
Controller code:
public function compatabilty(){
$this->load->model('user_model');
$this->load->model('admin_model');
$data['result'] = $this->user_model->get_compatibility();
$this->load->view('compatabilty',$data);
}
public function compatabilty_search()
{
$submit = $this->input->post('searchcompatible');
if(isset($submit)){
$new = $this->input->post('search_text');
$datas['result'] =$this->admin_model->fetch_data($new);
$this->load->view('compatabilty_search',$datas);
}
}
Model:
function fetch_data($new)
{
$this->db->select("*");
$this->db->from("au_compatible_products");
if($new != '')
{
$this->db->like('Device', $new);
}
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
return $query->result();
}
View:
<div class="search-result" >
<ul><font>Serch Results for</font>
<?php foreach ($result as $key ) { ?>
<li><?php echo $key->Model_Number;?> </li>
<?php } ?>
</ul>
</div>

Related

Codeigniter - controller can't send data to view

I got an error like this :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: tasks
controller
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task()->result();
$this->load->view('pages/all', $data);
}
view
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) ) {
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
Model
public function show_task() {
return $this->db->get('task');
}
what's wrong with my code?
Controller
public function show() {
$this->$data['tasks'] = array();
$this->$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
View
//Here, you can also use for loop to display data.
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
EDIT =>
Model
public function show_task() {
$query = $this->db->get('task');
if( $query->num_rows() > 0 ) //Always check you are getting result or not.
{
return $query->result_array(); //result_array() returns the query result as a pure array
}
else
{
return array();
}
}
Here you are returning return $this->db->get('task'); directly. So you are not getting anythingin View.
First you have to assign $this->db->get('task') to variable e.g. $query and pass result array to controller return $query->result_array();.
Reference => https://www.codeigniter.com/userguide3/database/results.html#result-arrays
Controller:
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
Model:
public function show_task() {
return $this->db->get('task')->result();
}
View Page:
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>

why not load view when i call a function? codeigniter

I have a problem in codeigniter.
I call a funtion in controller Articles from the view.
<li><a href="index.php/articles/Category/1" ><?php echo $item['CATEGORY'] ?></a></li>
The function
function getCategories() {
$this->db->select('ID_CAT,CATEGORY');
$this->db->from('CATEGORY');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
function Category($id_cat) {
if (!$id_cat) {
$id_cat = 0;
}
$this->load->model('graf/home_model');
$data = array();
$data['categories'] = $this->home_model->getCategories();
$filtro_cat = $id_cat;
$data['article'] = $this->home_model->getArticles($filtro_cat);
$this->load->view('graf/articles', $data, TRUE);
}
home_model
function getArticles($filtro_cat) {
$this->db->select('*');
$this->db->from('ARTICLES');
if ($filtro_cat != 0) {
$this->db->where("ID_CAT=".$filtro_cat);
}
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
When i select the button the url changes to http://localhost/Graf/index.php/articles/Category/1 but does not load
http://localhost/Graf/index.php/articles/
Why?
When you set third argument of load->view() to true it returns string value and does not output anything
Try changing:
$this->load->view('graf/articles', $data, TRUE);
to
$this->load->view('graf/articles', $data);
set the routes.php in folder config, add this code;
$route['articles/Category/(.*)'] = 'articles/$1';
and change the link
<li><a href="<?php echo site_url('index.php/articles/'.$item['ID_CAT']); ?>" ><?php echo $item['CATEGORY'] ?></a></li>
and change your controller to this
$this->load->view('graf/articles', $data);

Can't send correct data with view in codeIgniter

I'm trying to build a easy booking system in codeIgniter and using a database with a table called conference_rooms. I'm calling for this in my Booking_Model.phplike this:
public function __construct()
{
$this->load->database();
}
public function get_room() {
$query = $this->db->get('conference_rooms');
return $query->result_array();
}
}
To display it I'm using my Booking.php class looking like this:
public function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms'])) {
show_404();
}
$data['title'] = $data['conference_rooms']['title'];
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
And my view.php:
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>
It won't find $room. What am I doing wrong?
UPDATE:
Basically changed the whole code, my view class now looks like this (changed to index in my Booking controller)
public function index() {
$this->load->helper('form');
$this->load->view('bootstrap/header');
$this->load->model('Booking_Model');
$rooms = $this->Booking_Model->get();
$rooms_form_options = array();
foreach ($rooms as $id => $room) {
$rooms_form_options[$id] = $room->title;
}
$this->load->model('Package_Model');
$packages = $this->Package_Model->get();
$packages_form_options = array();
foreach ($packages as $id => $package) {
$packages_form_options[$id] = $package->package_name;
}
$this->load->view('booking', array(
'rooms_form_options' => $rooms_form_options,
'packages_form_options' => $packages_form_options,
));
$this->load->view('bootstrap/footer');
}
And my booking.php;
<div>
<?php echo form_label('Conference Room', 'id') ; ?>
<?php echo form_dropdown('id', $rooms_form_options, set_value('id')); ?>
</div>
<div>
<?php echo form_label('Package type', 'package_id') ; ?>
<?php echo form_dropdown('package_id', $packages_form_options, set_value('package_id')); ?>
</div>
<div>
<?php echo form_label('Antal deltagare', 'number_people') ; ?>
<?php echo form_input('number_people', set_value('number_people')) ; ?>
</div>
<div>
<?php echo form_submit('preview', 'Book'); ?>
</div>
Use this
In Model
<?
function __construct()
{
$this->load->database();
}
function get_room() {
$query = $this->db->get('conference_rooms');
$result = $query->result_array();
return $result;
}
?>
In controller
function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms']))
{
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
?>
in view
<h3><?php echo $conference_rooms[0]['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms[0]['seats']; ?>
</div>
cz of $conference_rooms[0] pointing 0 is we passing data with Objective array. so we need to point data. check with print_r
MODEL
You need to row_array(); to return an array
public function get_room() {
$query = $this->db->get('conference_rooms');
$rowcount = $query->num_rows();
if( $rowcount > 0 ){
return $row = $query->row_array();
} else {
return FALSE;
}
}
CONTROLLER
public function view()
{
$conference_rooms = $this->booking_model->get_room();// asing your array to variable
if (empty($conference_rooms)) {
show_404();
} else {
$data['conference_rooms']=$conference_rooms;// pass your variable to data array to pass into view
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
VIEW
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>

Get categorised result taking arguments present inside loop codeigniter

I have got two tables 'category' and 'organization' . Under my view i want to list all those organizations which work under respective categories of category table . What i did is shown below :
*Here's my controller : *
function organization()
{
$data['category'] = $this->category_model->get_all_category();
$data['organization'] = $this->organization_model->get_categorised_organization();
$data['title'] = "Welcome to the organization page";
$this->load->view('organization_index',$data);
}
Here's the model :
function get_categorised_organization() function get_categorised_organization() {
$category = $this->category_model->get_all_category();
$i = 0;
foreach ($category as $c):
$sql = "SELECT * FROM ss_organization where org_working_area='$c->category_name'";
$query = $this->db->query($sql);
$result[] = $query->result();
endforeach;
return $result;
} {
$category = $this->category_model->get_all_category();
$i = 0;
foreach ($category as $c):
$sql = "SELECT * FROM ss_organization where org_working_area='$c->category_name'";
$query = $this->db->query($sql);
$result[] = $query->result();
endforeach;
return $result;
}
and here's the view
<?php foreach($category as $c): ?>
<div class="categorybox">
<h2><?php echo $c->category_name;?></h2><hr>
<ul>
<?php //print_r($organization); die();?>
<?php foreach($organization as $o):?>
<?php foreach($o as $p): ?>
<li><?php echo $p->org_name;?></li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach;?>
Now what i'm getting is same organizations over different categories .. How can i get respective organizations working under the respective category
I make my version of this, and please just adapt to your code
Controller:
function organization()
{
$this->load->model('myModel');
$data['category'] = $this->myModel->getAllCategory();
$data['title'] = "Welcome to the organization page";
$this->load->view('organization_index',$data);
}
Model:
public function getAllCategory()
{
$query = $this->db->get('category');
if($this->db->_error_number() > 0)
{
return false;
}
else
{
return $query->result_array();
}
}
function getOrganizationByCategory($category_id) {
$this->db->where('category_id = ' . (int)$category_id);
$query = $this->db->get('ss_organization');
if($this->db->_error_number() > 0)
{
return false;
}
else
{
return $query->result_array();
}
}
View:
<?php foreach($category as $c): ?>
<div class="categorybox">
<h2><?php echo $c['name'];?></h2><hr>
<ul>
// here you call your function to display organizations by category
<?php $organization = $this->myModel->getOrganizationByCategory($c['id']);?>
<?php foreach($organization as $o):?>
<li><?php echo $o['name'];?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>

i want to use for-each on my code igniter but i am using conditione of (id) and my other conditione

on the view i want to use foreach but with the condition of the id i get from mypost
model
public function get_rows($id= FALSE)
{
if ($id === FALSE)
{
$query = $this->db->get('table1');
return $query->result_array();
$config['per_page'];
$this->uri->segment(3);
}
$query = $this->db->get_where('table1', array('id'=> $id,'type'=> 2));
return $query->row_array();
}
Controller
public function index($id){
$data['rows'] = $this->model->get_rows($id);
if (empty($data['rows']))
{
show_404();
}
$data['id'] = $data['rows']['id'];
$this->load->view('view', $data);
}
View
<?php foreach ($rows as $row): ?>
<li><?php echo $row->title; ?></li>
<?php endforeach; ?>
Here i want to use for each on my view but with the condition of(ID) i get from the post and the type i set my self
thanks in advance...
rafiq__
In view use $row['title']; not $row->title;
View
<?php foreach ($rows as $row): ?>
<li><?php echo $row['title']; ?></li>
<?php endforeach; ?>
and may be you need to change return $query->row_array(); to return $query->result_array(); in your model.
Model
public function get_rows($id = 0){
if ($id > 0){
$this->db->where(array('id'=> $id,'type'=> 2));
}
return $this->db->get('table1')->result_array();
}

Categories