Pagination Not working in my Codeigniter - codeigniter

this is my controller file and its not working
controller name blog
model name blogmodel
view file name loggedin
function getvalues(){
$config['base_url'] = 'http://192.168.1.50/codeigni/index.php/blog/getvalues';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
$this->load->model('blogmodel');
$data['results']=$this->blogmodel->getall();
$this->load->view('Login/loggedin',$data);
}
}
this is my model file and model name 'blogmodel'
function getall(){
$query=$this->db->query("SELECT * FROM user_details");
return $query->result();
}
}
this is my view file i cannot access pagination
<p><?php echo $links; ?></p><?php
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>email</th>
</tr>";
foreach($results as $row){
echo "<tr>";
echo "<td>" .$row->id;
echo "<td>". $row->username;
echo "<td>". $row->password;
echo "<td>". $row->email;
echo "</tr>";
}
echo "</table>";
?>

Instead of doing echo $this->pagination->create_links();
do as below
$data["links"] = $this->pagination->create_links();

Related

How do i retrieve images from folder which is linked to an item id?

The main issue is with the model, i cant figure out how to join the two tables so that i can display an item with its image which are stored in different locations.
model
$this->db->select('bs_items.id, bs_items.description, bs_items.title,
bs_items.touch_count,bs_items.price');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$this->db->from('bs_items');
Controller:
this->load->model('Popular_car');
$this->load->model('Recent_car');
$data['popular_cars']= $this->Popular_car->popular_car();
$data['recent_cars']=$this->Recent_car->get_listing();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('pages/home_recent', $data);
$this->load->view('templates/footer');
Please update your question with some code you have done , or database structure. i'm giving you the answer based on what i understood using your given code snippet.
Model
function get_items()
{
$this->db->select('*');
$this->db->from('core_images');
$this->db->join('bs_items', 'core_images.img_parent_id =
bs_items.id');
$this->db->order_by('bs_items.touch_count', 'desc');
$query = $this->db->get();
return $query->result();
}
Controller
function get_allitems()
{
$this->load->model('model_name');
$data['items'] = $this->model_name->get_items();
$this->load->view('view_name', $data);
}
View
<?php foreach ($items as $row): ?>
<h1><?php echo $row->title;?></h1>
<p><?php echo $row->touch_count;?></p>
<p><?php echo $row->price;?></p>
<p><?php echo $row->description;?></p>
<img src="<?php echo base_url().'path_of_image/'.$row->image_from_db; ?>
<?php endforeach; ?>
$myString = $this->db->get_where('profile_table' , array('id' => $edit ) )->row()->file_name_galery;
// $json11 = '{ "1":"s1.jpg", "2":"s2.jpg", "3":"s4.jpg", "4":"s4.jpg", "5":"s5.jpg" }';
$array = json_decode($myString);
if (empty($array)) {
echo '<p class="tx-danger"> NO IMAGE..</p>';
}
foreach($array as $key => $photo) { <div class="column_galery text-center zoom_img_app">
<a onclick="selected_photo(' echo $photo; ')" data-toggle="modal" data-target="#edit_image_mod" >
<img class="" src="http://yourapp.com/uploads/profile_gallery/ echo $photo; " style="width:100%"> <span class="tx-danger">DELETE</span> </a>
</div> }

codeigniter how can i display data in view from this model

This is request_report function in my report_model how can code tabular data in my view passed by controller
function request_report($from, $to, $member_filter )
{
$this->db->select('requests.request_id,items.item_type,items.quantity,items.item_unit,items.item_name, requests.item_id, requests.quantity_requested, requests.quantity_delivered, requests.purpose');
$this->db->from('requests', 'items');
$this->db->join('items', 'items.item_id = requests.item_id');
$this->db->where('created_time >=', $from);
$this->db->where('created_time <=', $to);
if ($member_filter != 'null') $this->db->where('created_by', $member_filter);
$query = $this->db->get();
if ($query->num_rows() > 0 ) {
echo '<table id="requests" class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Category</th>
<th>Items</th>
<th>Quantity Requested</th>
<th>Quantity Available</th>
<th>Quantity Delivered</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>';
foreach ($query->result() as $row) {
echo '<tr>';
echo '<td>';
echo $row->request_id;
echo '</td>';
echo '<td>';
echo $this->get_item_type_name($row->item_type);
echo '</td>';
echo '<td>';
echo $row->item_name;
echo '</td>';
echo '<td>';
echo $row->quantity . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->quantity_requested . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->quantity_delivered . " " . $this->get_unit_measurement($row->item_unit);
echo '</td>';
echo '<td>';
echo $row->purpose;
echo '</td>';
echo '</tr>';
}
echo '</tbody>
</table>';
}
else{
return '<h4 style="text-align: center; padding: 37px;"> No Matching Requests Found </h4>';
}
}
and this is my view
<div class="col-sm-12">
<?php echo $this->report_model->request_report($from, $to, $member_filter)>
</div>
my question is how can pass data from model to view through controller, not just calling model directly in view, and i need the tabular data in model to be in view, am looking for any help thanks!!
my question is how can pass data from model to view through
controller, not just calling model directly in view, and i need the
tabular data in model to be in view, am looking for any help thanks!!
In your controller say method is index()
<?php
class Site extends CI_Controller{
function index()
{
// this you can also put in __construct
// if you want to share same model across other methods
$this->load->model('report_model');
// And call your model function like below
$data = $this->report_model->request_report('2015-12-10', '2016-03-10', 'some_user');
// load view
$this->load->view('bootstrap_view',array('model_output' => $data));
}
}
and in your view, say bootstrap_view.php
<div class="col-sm-12">
<?php echo $model_output;>
</div>
Also note you can make use of table class in codeigniter, where you can set template of your interest, for more details follow below link
https://www.codeigniter.com/user_guide/libraries/table.html

How to use pagination to display one item per page

I want to display one fact per page using pagination but it displays all facts in one page.
Here is the controller am using
$config = array();
$config["base_url"] = base_url() . "Admin/news/index";
$config["total_rows"] = $this->news_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 1;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->news_model->fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
And this is model
public function record_count() {
return $this->db->count_all("knowbank");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("knowbank");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Finally my view
<?php foreach ($news as $news_item): ?>
<div class="col1"><?php echo $news_item['title'] ?></div>
<img src="<?php echo base_url('image/' .$news_item['picture']);?>" style="max-width:210px; max-height:180px;"><br />
<p><?php echo $news_item['body'] ?></p>
<p>Category: <?php echo $news_item['category']?><br /><br />
Posted on: <?php echo $news_item['date']?>
</p>
View article<br /><br />
<?php endforeach ?>
<?php echo $links; ?>
Change the following and try, your page offset is the last segment of the url, so its 4
$config["per_page"] = 1;
$config["uri_segment"] = 4;
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

Codeigniter Paginaton Next button is not working

I am trying to display information from database. I have set $config['per_page'] to 2, in my view file I can see the information I want but when I click on the next button it doesn't change anything. The database values remain same and the current page remains the first page too.
Would you please kindly help me figure out the problem?
Thanks in Advance :)
Controller:
function index($id){
$this->load->library('pagination');
$config['base_url'] = site_url().'Student_fee_status/index/'.$id;
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$query = $this->db->get('');
$numrows=$query->num_rows();
$config['total_rows'] = $numrows;
$config['per_page'] = 2;
$config['uri_segment'] = '2';
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('Mod_student_fee_status');
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
$data['main_content']='view_student_fee_status';
$this->load->view('includes/template',$data);
}
My Model :
function fee_status($id,$perPage,$uri_segment) {
$this->db->select('*');
$this->db->from('studentpayment1');
$this->db->where('studentid', $id);
$getData = $this->db->get('', $perPage, $uri_segment);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
EDIT
When the page first loads the link looks like this- http://localhost/sundial/Student_fee_status/index/1006/
but when I click on the next page it looks like this- http://localhost/sundial/Student_fee_status/index/1006/2
My View File:
<h1>Payment Status</h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>S.N</th>
<th>Invoice ID</th>
<th>Transaction Description</th>
<th>Received Date</th>
<th>Debit</th>
<th>Credit</th>
<th>Balance</th>
</tr>
</thead>
<?php $i = $this->uri->segment(2) + 0; foreach ($records as $row){ $i++; ?>
<tbody>
<?php
$mydate= $row['period'];
$month = date("F",strtotime($mydate));
$year = date("Y",strtotime($mydate));
?>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td><a href="<?php echo base_url(); ?>student_fee_status/fee_types/<?php echo $row['paymentid']; ?>" rel="1" class="newWindow" >Total Fee For <?php echo $month ;?>, <?php echo $year ;?> </a></td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo $row['totalamount'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
<tr>
<td><?php echo $i; ?>.</td>
<td><?php echo $row['invoiceid'];?></td>
<td>Payment Received </td>
<td><?php echo $row['received_date'];?></td>
<td><?php echo "0";?></td>
<td><?php echo $row['amountpaid'];?></td>
<td>
<?php
$balance=$row['totalamount']-$row['amountpaid'];
if($balance>0){
echo "<font color=\"red\">$balance</font>";
}
else {
echo $balance;
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
You are telling the pagination library to use $config['uri_segment'] = '2'; - the second segment of your uri.
When this is your url: http://localhost/sundial/Student_fee_status/index/1006/ I am guessing this is your base_url: http://localhost/sundial/
In this case your segments are:
Student_fee_status - your controller
index - the controllers method you are calling
1006 - the argument you are calling the controllers method with
this should be the argument for pagination
Try this
$config['uri_segment'] = '4';
instead of
$config['uri_segment'] = '2';
edit:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$config['uri_segment']);
I think this line contains another error.
You pass your model the information which uri_segment is used by the pagination library. That should be 4 now. However, your model uses this value to specify an offset in your query. This means you always put an offset of 4 into your query. But I think what you really want to do is, pass the model the VALUE of the 4th uri_segment.
I would try this instead:
$data['records']= $this->Mod_student_fee_status->fee_status($id,$config['per_page'],$this->uri->segment($config['uri_segment']));

Pagination Codeigniter

I'm using Codeigniter..and I'm new to this. What I'm trying to do is to use pagination in my code but the problem is the pagination link is not shown at the bottom.
The controller:
function index() {
$this -> load -> library('pagination');
$config['base_url'] = 'Rfetch1_controller/index/';
$total = $this -> db -> count_all('info');
$per_page = 4;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$this -> pagination -> initialize($config);
$data['pagination'] = $this -> pagination -> create_links();
$data['list'] = $this -> Rfetch1_model -> get_s($config['per_page'], $this -> uri -> segment(3));
if ($data['list'] !== null) {
$this -> load -> view('Rfetch1_view', $data);
} else {
$this -> load -> view('noresult');
}
}
The model :
function get_s($num, $offset) {
$this -> db -> select('subject, id, problem,image'); // field name
$sql = $this -> db -> get('info', $num, $offset); // table name
if ($sql -> num_rows() > 0) {
foreach($sql -> result() as $row) {
$data[$row -> id] = $row -> subject;
}
return $data;
} else {
return null;
}
}
The view :
foreach($list as $id => $title): ?>
<? echo anchor('Rfetch1_controller/get_by_id/'.$id, $title); ?>
<?php endforeach;?>
<?php echo $this->pagination->create_links(); ?>
Why the pagination link is not showing?
You've already created links in your controller with this:
$data['pagination'] = $this->pagination->create_links();
so you just need to echo $pagination in your view:
<?php echo $pagination; ?>
You didn't load the pagination library in your view, just your controller - so you can't use the create_links() method in your view. But since you did use that method in your controller and pass it through to your view in the $data array, you can use that variable in your view.
$this->load->model('reciever');
$this->load->library('uri');
$this->load->library('pagination');
$config['base_url'] = base_url(). 'users_ci/users';
$config['total_rows'] = $this->reciever->getRows();
$config['per_page'] = 4;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config["num_links"] = round( $config["total_rows"] / $config["per_page"] );
$config['users']= $this->reciever->getUsers(4,$this->uri->segment(3));
$this->pagination->initialize($config);
$config['pages'] = $this->pagination->create_links();
$this->load->view('users',$config);
this is the function that i have in my controler!
and in the view i have this one
<div><?php echo $pages; ?></div>
with this you can use bootstrap also for the pagination view.
You must query your database exactly with the per page you want to saw.
Searching Articles in CodeIgniter
Step 1: Create model search function.
public function search($query)
{
$q=$this->db->from('articles')
->like('title',$query)
->get();
return $q->result();
}
Step 2: Create search function in controller.
public function search()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('query','Query','required');
if(! $this->form_validation->run())
$this->index();
$query=$this->input->post('query');
$this->load->model('articlesmodel','articles');
$articles=$this->articles->search($query);
$this->load->view('public/search_results',compact('articles'));
}
Step 3: Create search_results in view.
<?php include ('public_header.php');?>
<div class="container">
<h1>Serach Results</h1>
<table class="table">
<thead>
<tr>
<td>Sr No.</td>
<td>Article Title</td>
<td>Published On</td>
</tr>
</thead>
<tbody>
<tr>
<?php if(count($articles)):?>
<?php $count=$this->uri->segment(3,0);?>
<?php foreach($articles as $article ):?>
<td><?= ++$count?></td>
<td><?= $article->title?></td>
<td><?= "Date" ?></td>
</tr>
<?php endforeach;?>
<?php else: ?>
<tr>
<td colspan="3"> No Records Found.</td>
</tr>
<?php endif;?>
</tbody>
</table>
</div>
<?php include ('public_footer.php');?>

Resources