pagination is not working in codeigniter :( - php

I want to display 2 rows in view page as an output. Again when i click on to go to the next page it will display 2 next rows and so on ( All together I have 8 rows in
table). But when I run the following code it displays all 8 rows in the view-page along with pagination links. I tried whole day to find the actual reason for not
working it but my problem is still unsolved. I searched for the help on internet with related query but it was of no use. Finalyy I am here with my own words to explain
the problem. I've commented almost all the lines in my code. I am loading libraries in autoload for pagination and helper for form and url. I'll really be thankful if
somebody help me out. Thanks in advance.
<?php
class ManageUser extends CI_Controller { // creating class for the controller
function index()
{
if($this->session->userdata('logged_in')) // checking users under session if he is already logged in
{
$this->load->model('admin/user'); // loading model . I am not using Datamapper.
$result = $this->user->view_user(); // getting response from the model and storing it to result
$total_rows = count($result); // counting number of rows countered ( its 8 in in my database )
//echo $total_rows;
if($result)
{
$data['users'] = $result;
$config['base_url'] = "http://192.168.0.102/project/index.php/admin/manageUser"; // this is the address where I am pointing the view page url
$config['total_rows'] = $total_rows; // Total numbers of rows assigned to pagination-config
$config['per_page'] = '2'; // I want to display 2 rows in 1 page
$config['uri_segment'] = '2';
$this->pagination->initialize($config); // initilizing the pagination-config
//$data['pagination'] = $this->pagination->create_links();
//$this->load->vars($data);
$this->load->view('admin/manageUser',$data); // Loading the page
}
//$this->load->view('admin/manageUser',$data);
}
else
{
redirect('admin/login'); // incase of faliured session user will be redirected to the login-page.
}
}
}
?>
Following code is from my model named User
<?php
Class User extends CI_Model // extending the model
{
function __construct()
{
parent::__construct();
}
function view_user() // function which is loaded from controller
{
$query = $this -> db -> get('users'); //query to fetch all the information from the database
return $query->result(); // returning result to the Controller.
}
}
?>
And finally this is the view page I am using to display the content .
<!-- This is the view page -->
<?php if(isset($users)) { ?> <!-- Checking if user is set -->
<?php foreach($users as $user) { ?> <!-- running in a loop to accept all the value from the database and display it row wise -->
<td><?php echo $user -> us_display_name; ?></td> <!-- Displaying name -->
<td><?php echo $user -> us_first_name . " " . $user -> us_last_name; ?></td> <!-- Displaying first name and last name together -->
<td><?php echo $user -> us_email_id; ?></td> <!-- Displaying email-ID -->
<?php } } else { ?>
<tr> <td>No records found!</td>
<?php } ?>
<?php echo $this->pagination->create_links(); ?>

You are getting all the users from the Model. That's not how you do it.
Your controller should be something like this :
<?php
class ManageUser extends CI_Controller { // creating class for the controller
function index($offset)
{
if($this->session->userdata('logged_in')) // checking users under session if he is already logged in
{
$this->load->model('admin/user'); // loading model . I am not using Datamapper.
$perpage = 2;
$result = $this->user->view_user($offset,$perpage); // getting response from the model and storing it to result
$total_rows = $this->db->count_all('users');
if($result)
{
$data['users'] = $result;
$config['base_url'] = "http://192.168.0.102/project/index.php/admin/manageUser"; // this is the address where I am pointing the view page url
$config['total_rows'] = $total_rows; // Total numbers of rows assigned to pagination-config
$config['per_page'] = $perpage; // I want to display 2 rows in 1 page
$config['uri_segment'] = '2';
$this->pagination->initialize($config); // initilizing the pagination-config
//$data['pagination'] = $this->pagination->create_links();
//$this->load->vars($data);
$this->load->view('admin/manageUser',$data); // Loading the page
}
//$this->load->view('admin/manageUser',$data);
}
else
{
redirect('admin/login'); // incase of faliured session user will be redirected to the login-page.
}
}
}
?>
And in the model you need to limit the result
<?php
Class User extends CI_Model // extending the model
{
function __construct()
{
parent::__construct();
}
function view_user($offset,$limit) // function which is loaded from controller
{
$query = $this -> db -> get('users',$perpage,$offset); //You dont fetch all the data from the database
return $query->result(); // returning result to the Controller.
}
}
?>

Related

How to print count in view using Codeigniter

I am new to Codeigniter and I am facing an issue with printing count in admin dashboard.
The following code is what I am trying now. Please tell me what I am doing wrong.
my model
function todayorder(){
$sql=$this->db->query("select count(*) as count from orders where order_date_time >= CURDATE()");
return $sql->row();
}
controller
public function orderlist(){
if(isset($_SESSION['admin_id'])){
$data["row"]=$this->picShuModel->orderlist();
$this->load->view('admin/index',$data);
$query = $this->picShuModel->todayorder();
$data['count'] = $query->count;
$this->load->view('admin/index',$data);
}else{
$this->load->view('admin/login');
}
}
and my view* in the dashboard
<?php foreach($data as $count){echo $count;}?>
In your view you can directly use $count as a variable.
Change
<?php foreach($data as $count){echo $count;}?>
To
echo $count;
Update
public function orderlist(){
if(isset($_SESSION['admin_id'])){
$data["row"]=$this->picShuModel->orderlist();
$query = $this->picShuModel->todayorder();
$data['count'] = $query->count;
$this->load->view('admin/index',$data);
}else{
$this->load->view('admin/login');
}
}
This below line assigns count to array variable
$data['count'] = $query->count;
To access count in view page
<?php
echo $count ; // you have use element of data rather $data. this print the count value
?>
In controller you are loading view page twice, load view page once

not able to retrieve data from database using codeigniter

I am new to CodeIgniter. I have retrieved data from one table but I'm not able to retrieve the data from another table. The view part is not working. Due to some reason the PDF is displaying blank (using tcpdf). Can we fetch it as array? If yes, then how can we fetch it as array?Some of the data appears to be printed below the button that I press.It does not display on the pdf that is generated after the we click on the button.
code :
controller
index(){
$this->load->database();
//load the model
$this->load->model('Order_model');
//load the method of model
$data['h']=$this->Order_model->select();
//return the data in view
$this->load->view('includes/orderPdf', $data);
}
model
public function select()
{
//data is retrive from this query
$query = $this->db->get('master_user');
return $query;
}
view
<?php
foreach ($h->result() as $row)
{
?><tr>
<td><?php echo $row->mobile_no ?></td>
<td><?php echo $row->country ?></td>
</tr>
<?php }
?>
Best way to set up is probably similar to:
Controller:
$this->load->model('Order_model', 'Order');
$data['orders'] = $this->Order->getOrder();
Model:
public function getOrder() {
$q = $this->db->get('master_user');
return $q->num_rows() ? $q->result_array() : [];
}
View:
<?php foreach($orders as $order) { ?>
<tr>
<td><?=$order['mobile_no']?></td>
<td><?=$order['country']?></td>
</tr>
<?php } ?>
Now you can also add an if(empty($orders)) and put in "no orders to show" or similar.

I want to create paging on my content managed site

I'm trying to load a blog type page, I load the entries from my database with this controller
public function blog($page){
$this->load->model("model_get");
$this->load->view("site_header");
$this->load->view("site_nav");
$counter = $this->model_get->getBlogcount();
for($counter; $counter > 0; $counter --){
$data["results"] = $this->model_get->getBlog($counter);
$this->load->view("content_blog", $data);
}
}
$this->load->view("site_footer");
}
and this model
function getBlogcount(){
$result = $this->db->count_all("blog");
return $result;
}
I count the entries in the database where I call them out by their ID. But now I'm trying to create multiple pages that expand automatically everytime I enter a new entry. So lets say I have 27 entries, and want to have no more than 5 entries on a single page, how do I make it so that it creates the necessary 6 pages to show them without loading the other 3 empty entries and stuff.
I'm new to codeigniter and have always worked with ASP .NET, any help would be helpfull.
Thanks in advance!
p.s. english isn't my first language
CodeIgniter have his own pagination Class. Take a look here : http://ellislab.com/codeigniter/user-guide/libraries/pagination.html
You can try this firstly and adapt to your project :
public function blog($page = 0)
{
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = base_url('blog/'. $page);
$config['total_rows'] = $this->model_get->getBlogcount();
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data['results'] = $this->model_get->getBlog($config['per_page'], $page);
$this->load->view("content_blog", $data);
}
Edit your "getBlog" function model to get results with limit clause, like this :
function getBlog($limit, $start)
{
$results = $this->db->limit($limit, $start)->get('your_blog_table');
if ($results)
{
return $results->result();
}
return FALSE;
}
And use in your view this code to create your pagination links :
echo $this->pagination->create_links();

last inserted id/latest inserted id function codeigniter

Codes:
View
<html>
<body>
<?php echo form_open('samplecontroller/sample'); ?>
<input type="text" name="samplename"/>
<input type="submit" value="go"/>
<?php echo form_close(); ?>
</body>
</html>
Controller
<?php
Class samplecontroller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Sample_insert');
}
function index()
{
$this->load->view('sample.php');
}
function show($data)
{
$this->load->view('sampleresult',$data);
}
function sample()
{
if($result = $this->Sample_insert->insert_into_sample())
{
$this->show($result);
}
}
}
?>
Model
<?php
Class Sample_insert extends CI_Model
{
function insert_into_sample()
{
$sample = array(
'samplename' => $this->input->post('samplename'),
);
$this->db->insert('sample', $sample);
$latest_id = $this->db->insert_id();
return $latest_id;
}
}
?>
The flow is that, i have 2 views. The other one contains only 1 text input, after which goes to controller then controller passes it on to model then model gets the value within the text input then inserts to my sample table.
The sample table has 2 columns, id(PK,AI) and name
after it inserts, on my model. i added a line, return $latest_id. Then passes goes back to my controller then passes it off to another view that only has
print_r($data);
After all that, it displays an error.
Message: Undefined variable: data
Im not sure where the flow went wrong or if i made a syntax mistake. If someone knows/expert on insert_id(), could you pin point where exactly i am wrong? anyways, ive made my research and been getting results on the web how buggy insert_id is. Im not sure if its true or not but ive been redirected mostly to forums wherein insert_id returns null and some say its a bug. Im hoping it isnt.
As per your comment, You have to do some changes in your controller.
function show($data)
{
$this->load->view('sampleresult',array("data"=>$data));
}
You can get inserted id in view in the name of $data.
For ex:
echo $data;
Edit: As per your comment in answer, For multiple row insert in model, add below code
$id_arr = array();
foreach($sample_data as $sample)
{
$this->db->insert('sample', $sample);
$id_arr[] = $this->db->insert_id();
}
return $id_arr;
Now in your view, you will get ids in $data
foreach($data as $id)
{
echo $id;
}

Displaying data from database using Codeigniter

I am trying to get data from a database and display it using a model, controller, and view.
Here is my model
public function waitlist_view() {
$data = array();
$this->load->database();
$this->db->select('*');
$this->db->from('waitlist');
$query = $this->db->get();
return $query->row();
}
Here is my controller
public function waitlist() {
$data['title']="parents_viewlist";
//redirect if not logged in
if(($this->session->userdata('logged_in')!= 1) && ($this->session->userdata('type')!='parent')) {
redirect('login/index');
}
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
}
Here is my view
<div>
<?php echo $row->waitlist_id; ?>
<?php echo $row->ay_code; ?>
<?php echo $row->school_id; ?>
<?php echo $row->waitlist_status; ?>
</div>
It doesnt display anything on the page when I pull it up. Any help would be appreciated!
in your model use result() to get data :
public function waitlist_view() {
$this->load->database();
$query = $this->db->get('waitlist')->result();
return $query;
}
in your controller :
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
Now use loop on $row in your view to print data.
In your controller print the data returned from db as:
echo "<pre>";print_r($data);echo "</pre>";exit;
add this line just after $data['row'] = $this->parents_model->waitlist_view();
Let us know what result are you getting.

Categories