codeigniter 3.1.3 pagination shows same record - codeigniter

i don't understand why this is happening but same row on every page reload shows. I'm new to codeigniter and know basics of php and programming. Thank you for help in advance.
model:
class Clanovi_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_clanovi($limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get('clan');
return $query->result();
}
public function broji_clanove() {
return $this->db->count_all('clan');
}
}
controller:
class Clanovi extends CI_Controller {
public function index() {
$this->load->model('Clanovi_model');
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = 'http://localhost/codeigniter5/index.php/clanovi';
$config['total_rows'] = $this->Clanovi_model->broji_clanove();
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['clanovi'] = $this->Clanovi_model->get_clanovi($config['per_page'], $this->uri->segment(3));
$data['paginacija'] = $this->pagination->create_links();
$this->load->view('zaglavlje');
$this->load->view('clanovi', $data);
$this->load->view('podnozje');
}
}
view:
<h2>Članovi</h2>
<hr>
<table class="table table-striped">
<tr>
<th>Ime</th>
<th>Prezime</th>
<th>Adresa</th>
<th>Korisničko ime</th>
<th>Lozinka</th>
</tr>
<?php foreach ($clanovi as $clan): ?>
<tr>
<td><?php echo $clan->ime; ?></td>
<td><?php echo $clan->prezime; ?></td>
<td><?php echo $clan->adresa; ?></td>
<td><?php echo $clan->korisnicko_ime; ?></td>
<td><?php echo $clan->lozinka; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $paginacija; ?>
routes:
$route['clanovi/(:num)'] = 'clanovi/index/$1';
$route['clanovi'] = 'clanovi';
screenshot1
screenshot2

Found solution by looking what $this->uri->segment(3); does, and changed value to 2.
$data['clanovi'] = $this->Clanovi_model->get_clanovi($config['per_page'], $this->uri->segment(2));

Related

its showing variable undefined

This is My Controller page
class Cart extends CI_Controller{
public function index()
{
$getALlCat = $this->Product_model-> getAllCategory();
$data = array();
//Retrieve cart Data
$data['cartItems'] = $this->cart->contents();
//print_r($data);exit;
//$this->load->view('cart/index' ,$data);
///$this->load->helper('url');
/ /$this->load->view('cart/header',['getAllCate'=>$getALlCat], $data); //including header and
footer
$this->load->view('cart/cartbody',['getAllCate'=>$getALlCat], $data);
$this->load->view('common/footer'); //including header and footer
}
}
**This is View page**
<tbody>
<?php if($this->cart->total_items() > 0){ foreach($cartItems as $item){ ?>
<tr>
<td><img src="<?php echo $item['product_image']; ?>" width="50"/></td>
<td><?php echo $item['product_name']; ?></td>
<td><?php echo '$'.$item["product_price"].' USD'; ?></td>
<td><input type="number" class="form-control text-center"></td>
<td><?php echo '$'.$item["subtotal"].' USD'; ?></td>
<td>Are you sue</td>
</tr>
<?php } }else{ ?>
<td colspan="6"><p>Your cart is empty.....</p></td>
<?php } ?>
</tbody>
**Error Showing like this**
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cartItems
Filename: cart/cartbody.php
Line Number: 217
Backtrace:
File: C:\xampp\htdocs\shop\application\views\cart\cartbody.php
Line: 217
Function: _error_handler
File: C:\xampp\htdocs\shop\application\controllers\Cart.php
Line: 33
Function: view
File: C:\xampp\htdocs\shop\index.php
Line: 315
Function: require_once
You can store multiple values as an array key of $data and then send only $data to view and use those key to retrieve values. I've rectified your code below and it should work as your needs. I've mentioned comments wherever necessary.
public function index() {
$data = array();
$data['getALlCate'] = $this->Product_model-> getAllCategory(); // store it in an array instead (I've changed it to getALlCate from getALlCat, so you don't have to change anything in your view)
//Retrieve cart Data
$data['cartItems'] = $this->cart->contents();
//$this->load->view('cart/header', $data); // including header and footer
$this->load->view('cart/cartbody', $data);
$this->load->view('common/footer'); //including header and footer
}
}
// you can retrieve data in view by accessing $cartItems and $getALlCate variables.
Hope it helps you.
Try following
$this->load->view('cart/cartbody',['getAllCate'=>$getALlCat, 'cartItems'=> $this->cart->contents()]);

upload image from rest client (android) to database with codeigniter

i want to upload the image profile and the user id from my android application to database. i tried this method and it work in local but when i test it with (POSTMAN) a rest client it doesn't work. i think because i call a view.
this is my code please help me.
my controller
class Users extends REST_Controller {
function __construct()
{ parent::__construct();
$this->load->model('Users_model','',TRUE);}public function index_get()
{
$this->load->view('users');
}
public function save_image_post()
{
$id = $_POST["id"];
$url = $this->do_upload();
$this->Users_model->save_image($id, $url);
//$this->Users_model->save_image($url);
}
private function do_upload()
{
$type = explode('.', $_FILES["pic"]["name"]);
$type = strtolower($type[count($type)-1]);
$url = "./images/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg", "jpeg", "gif", "png")))
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
return $url;
return "";
}
}
this is my model:
public function save_image($id,$url)
{
$this->db->set('id', $id);
$this->db->set('image_user', $url);
$this->db->insert('users');
}
this is my view:
<!DOCTYPE html><html lang="en"><head></head><body>
<?php echo form_open_multipart('api/users/save_image/'); ?>
<table class="table">
<tr>
<td>Id</td>
<td><?php echo form_input('id'); ?></td>
</tr>
<tr>
<td>Image</td>
<td><?php echo form_upload('pic'); ?></td>
</tr>
<tr><td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table></body></html>

A PHP Error was encountered Severity: Notice Message: Undefined property: Site::$site_model

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Site::$site_model
Filename: controllers/site.php
Line Number: 13
Site Controller site.php
Fatal error: Call to a member function delete_row() on a non-object in
C:\xampp\htdocs\login\application\controllers\site.php on line 13
My controller->Site.php
<?php
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
}
function delete(){
$this->site_model->delete_row();
$this->index();
}
function members_area(){
$this->load->model('patient_model');
$data['records'] = $this->patient_model->getAll();
$this->load->view('members_area', $data);
}
function add(){
$data['main_content'] = 'patient_form';
$this->load->view('includes/template',$data);
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true){
echo 'Your don\'t have permission to access this page. Login';
die();
}
}
}
My model -> site_model.php
class Site_model extends CI_Model{
function get_records(){
$query = $this->db->get('patient');
return $query->result();
}
function delete_row(){
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('patient');
}
function create_member(){
$new_member_insert_data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'contact' => $this->input->post('contact'),
'remarks' => $this->input->post('remarks')
);
$insert = $this->db->insert('patient', $new_member_insert_data);
return $insert;
}
}
My view-> members_area.php
<?php $this->load->view('includes/header');?>
<div id="main">
Welcome Back, <u><?php echo $this->session->userdata('username'); ?>!</u>
<h3>Vision Eye Medical Center Patient Information</h3>
<div id="overflow">
<table class="features-table">
<thead>
<tr>
<td>Patient Name</td>
<td>Email</td>
<td>Address</td>
<td>Contact</td>
<td>Remarks</td>
<td>Action</td>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tfoot>
<tbody>
<?php foreach($records as $row): ?>
<tr>
<td><?php echo $row->fname;?>
<?php echo $row->lname;?>
</td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->address;?></td>
<td><?php echo $row->contact;?></td>
<td><?php echo $row->remarks;?></td>
<td>View|<?php echo anchor("site/delete/$row->id",'delete');?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php echo form_open();?>
<?php echo anchor('patient/add', 'Add new Data'); ?><?php echo anchor('login/logout', 'Logout'); ?><br>
<?php echo form_close();?>
</div>
<?php $this->load->view('includes/footer');?>
It seems you are not loading your "site model" anywhere in code you can do following:
autoload it in autoload file.
load your model either in constructor of controller, or load it whenever needed by using this line $this->load->model('site_model');
More information can be found here.
You need to load model in your controller.
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
$this->load->model('site_model'); //Load site_model
........
In somecase it happens when you don't extract your form data to the model. Try extract($_POST) in your model and don't forget to load the model properly.
P.s.:this was the solution for my error

not getting array values from view codeigniter

my model function is
public function getUser()
{
$this->db->select(array('customer_name','customer_address','phone_no','fax','email_id','contact_person_name','mobile_no'));
$this->db->from('add_customer');
$this->db->where(array('status'=>1));
$res = $this->db->get();
if($res->num_rows() > 0)
{
$rows = $res->result_array();
return $rows;
}else{
return false;
}
}
function in controller
public function search_customer()
{
//$this->load->helper('url');
$data['baseurl'] = base_url();
$this->load->view('templates/header');
$data['details'] = $this->Customer_model->getUser();
$this->load->view('master/customer',$data);
}
data extracting from my view is like this
<?php for($i=0;$i<count($details);$i++){ ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $details[$i]['customer_name']; ?></td>
<td><?php echo $details[$i]['customer_address']; ?></td>
<td><?php echo $details[$i]['phone_no']; ?></td>
<td><?php echo $details[$i]['email_id']; ?></td>
<td><?php echo $details[$i]['contact_person_name']; ?></td>
<?php }?>
and this one results in an error like this
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: details
Filename: master/Customer.php
Line Number: 69
anybody please tell me why this error is getting
try to load your model in your controller like
$this->load->model('Customer_model');
without loading your model you con't get the details from get_user function
Model:
public function getUser()
{
$this->db->select('customer_name','customer_address','phone_no',
'fax','email_id','contact_person_name','mobile_no');
$this->db->from('add_customer');
$this->db->where('status',1);
return $this->db->get()->result();
}
Controller:
public function search_customer()
{
$data['baseurl'] = base_url();
//Load the view in variable as data, but do not render
//($header is accessible in 'master/customer' view)
$data['header'] = $this->load->view('templates/header','',true);
//Be sure to load Customer_model
$data['details'] = $this->Customer_model->getUser();
$this->load->view('master/customer',$data);
}
View:
<?php if(count($details)): ?>
<?php foreach($details as $detail): ?>
<tr>
<td><?php echo $detail->customer_name; ?></td>
<td><?php echo $detail->customer_address; ?></td>
<td><?php echo $detail->phone_no; ?></td>
<td><?php echo $detail->email_id; ?></td>
<td><?php echo $detail->contact_person_name; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
try using $data instead of $details in your master/Customer.

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']));

Resources