Pagination links not working - php

I am trying to show my table in pagination.Links are coming but when i click on a 2 or 3 or anything it goes to /view_expenses/view&per_page= this. and i ll get a 404 error.why its not showing the rest data?
This is my controller
class View_expenses extends CI_Controller
{
public function __construct()
{
parent::__construct();
$data['title']= 'View Expenses';
$this->load->view('header_view',$data);
$this->load->model('emp_expenses_model');
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->library('pagination');
}
public function index()
{
}
function view($offset=0){
$limit=5;
//$this->uri->segment(3);
$this->load->model('emp_expenses_model');
//$result['contents']=$this->emp_expenses_model->getRows($limit,$offset);
$result['countRows']=$this->emp_expenses_model->countRows();
$this->load->library('pagination');
$this->load->library('table');
$config=array(
'base_url' =>site_url ('/view_expenses/view'),
'total_rows' => $result['countRows'],
'per_page' => $limit,
'uri_segment' => 3,
'num_links' => 1,
);
//var_dump($config);
$this->db->limit(5);
$this->pagination->initialize($config);
$this->load->model('emp_expenses_model');
$this->data['view_expenses'] = $this->emp_expenses_model->get_all();
//$this->data['pagination'] = $this->pagination->create_links();
//var_dump($this->data['pagination']);die("jk");
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->library('pagination');
$this->load->view('view_expenses', $this->data);
/*$this->load->view('add_list', $this->data);*/
}
This is my model
function getRows($limit,$offset)
{
$query=$this->db->select('expenses_id,id,dropdown,modeofpayment,amount')
->from('emp_expenses')
->limit($limit,$offset);
$result=$query->get()->result_array();
return $result;
//var_dump($result);
}
function countRows()
{
//$query="select count(*) as count from emp_expenses";
$result = $this->db->count_all_results('emp_expenses');
//$result=$this->db->query($query);
return $result;
//var_dump('countRows');
}
This is My View
<table cellspacing="0" cellpadding="2" border="0" id="tbl" style="width:100%">
<tr style="background-color:#045c97">
<?php echo $this->pagination->create_links()?>
<td class="heading">Employee ID</td>
<td class="heading">Drop Down</td>
<td class="heading">Mode OF Payment</td>
<td class="heading">Amount</td>
<td class="heading">Edit</td>
<td class="heading">Delete</td>
</tr>
<?php
foreach ($view_expenses as $m){
$list_id = $m['id'];
//print_r($list_id);die('adad');
//$m['username']='';
?>
<tr style="text-align:center;">
<td><?php echo $m['id'] ?></td>
<td><?php echo $m['dropdown'] ?></td>
<td><?php echo $m['modeofpayment'] ?></td>
<td><?php echo $m['amount'] ?></td>
<td>Edit</td>
<td>
<?php
echo anchor('view_expenses/delete_expenses/'.$list_id, 'Delete', array('onClick' => "return confirm('Are you sure you want to delete?')"));
?>
</td>

you need to use ? not & to separate a query string from the rest of request

Your query is written completely wrong, you must put a question mark before everything else in the URL.
foo://example.com:8042/over/there?name=ferret&color=brown#nose
\_/ \______________/\_________/ \______________________/ \__/
| | | | |
scheme authority path query fragment
Notice, the "Question-Mark", or query operator must come before any queries, but if there are more than one they should be seperated by the "&".
Try this:
/view_expenses/view?per_page=
As to the links not showing you what you want, your code is not correct in any way. Here is a tutorial in which I found easy to understand:
http://www.phpfreaks.com/tutorial/basic-pagination

Related

Codeigniter blog: display post title in comments table

I am working on a blog application in Codeigniter 3.1.8 and Bootstrap 4.
There is a posts table and a comments table in the database. I have displayed all the comments in a Bootstrap 4 table. I want to display the title of the post each comment belongs to, instead of the post's id:
My Comments controller:
class Comments extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Static_model');
$this->load->model('Posts_model');
$this->load->model('Categories_model');
$this->load->model('Comments_model');
}
public function index() {
$data = $this->Static_model->get_static_data();
$data['categories'] = $this->Categories_model->get_categories();
$data['number_of_categories'] = $this->Categories_model->get_num_rows();
$data['posts'] = $this->Posts_model->get_all_posts();
$data['number_of_posts'] = $this->Posts_model->get_num_rows();
$data['comments'] = $this->Comments_model->get_all_comments();
$this->load->view('partials/header', $data);
$this->load->view('dashboard/comments');
$this->load->view('partials/footer');
}
}
In the Comments_model model I have:
public function get_all_comments(){
$this->db->select('comments.*');
$this->db->order_by('comments.id', 'DESC');
//$this->db->limit($limit, $offset);
$this->db->join('posts', 'posts.id = comments.post_id');
$query = $this->db->get('comments');
return $query->result();
}
In the view:
<tbody>
<?php foreach ($comments as $index => $comment): ?>
<tr id="<?php echo $comment->id; ?>">
<td><?php echo $index + 1; ?></td>
<td class="w-25"><?php echo $comment->comment; ?></td>
<td><?php echo $comment->name; ?></td>
<td><?php echo $posts['title']; ?></td>
<td><?php echo nice_date($comment->created_at, 'D, M d, Y'); ?></td>
<td>Aproved</td>
<td></td>
</tr>
<?php endforeach ?>
</tbody>
While <?php echo $posts->id; ?> displays the posts id, which i do not need in the view, the line results in an
Message: Undefined index: title error.
What is missing?
Hope this will help you :
get_all_comments method should be like this : add posts.title in select also
public function get_all_comments()
{
$this->db->select('comments.*,posts.title as post_title');
$this->db->order_by('comments.id', 'DESC');
//$this->db->limit($limit, $offset);
$this->db->join('posts', 'posts.id = comments.post_id');
$query = $this->db->get('comments');
return $query->result();
}
Replace it
<td><?php echo $posts['title']; ?></td>
with
<td><?php echo $comment->post_title; ?></td>

codeigniter delete row from table

i need same help from u all,
i have a code to delete rocord from table but it`s not working. my same one know.
this is my code
=> view
<table class="table table-striped">
<th class="success">NO</th>
<th class="success">TANGGAL</th>
<th class="success">JUDUL</th>
<th class="success">AKSI</th>
<?php
$count = 1;
foreach($hasil AS $row){
?>
<tr>
<td width="30px"><?=$count++;?></td>
<td width="110px"><?= $row['tanggal']; ?></td>
<td><?= $row['judul']; ?></td>
<td width="190px">
Edit
Hapus
</td>
</tr>
<?php } ?>
</table>
=> routes
$route['admin/headline/hapus'] = 'admin/amor_con_headline/hapus';
=> controler
public function hapus($id){
$this->load->model("admin_model/amor_model_headline");
$this->amor_model_headline->hapus_headline($id);
redirect('admin/dashboard');
}
=> model
function hapus_headline($id){
$this->db->where('id_headline',$id);
$this->db->DELETE('a_headline');
}
it's not work.
=> EDIT
it`s was solved by me.
i add this in my routes
$route['admin/headline/hapus/(:any)'] = 'admin/amor_con_headline/hapus/$1';
Try
MODEL
function hapus_headline($id){
$this->db->delete('table_name', array('column_name' => $id));
}
Or
function hapus_headline($id){
$var = array(
'column_name' => $id
);
$this->db->delete('table_name', $var);
}
Try this dude! using URI
=> Model
function hapus_headline(){
$this->db->where('id_headline', $this->uri->segment(3));
$this->db->delete('a_headline');
}
=> controller
public function delete()
{
//product id
$id_headline = $this->uri->segment(3);
$this->products_model->hapus_headline($id_headline);
redirect('admin/headline');
}
=> view
Hapus
I hope this might help you.

When I go to second page in codeigniter pagination, header file not adding, UI changes

When I click on 2 page in pagination link, it does not include header.php file on 2 page result, all css are in header.php, so how can I keep my header in all links in pagination.
dashboard.php
<?php if(!empty($userexpense)){ ?>
<table class="table table-responsive table-hover table-bordered">
<tr>
<th>Sr.No</th>
<th>Expense Detail</th>
<th>Expense Date</th>
<th>Amount</th>
</tr>
<?php foreach ($userexpense as $value) {?>
<tr>
<td><?php echo $value->expense_id; ?></td>
<td><?php echo $value->expense_detail; ?></td>
<td><?php echo date('d-m-Y',strtotime($value->expense_date)); ?></td>
<td><?php echo $value->amount; ?></td>
</tr>
<?php } ?>
<tr class="success">
<td colspan="2" style="visibility: hidden"></td>
<td><?php echo "Total"; ?></td>
<td><?php echo $userexpense_total; ?></td>
</tr>
</table>
<p><?php echo $links; ?></p>
<?php }else{ ?>
<div class="well well-lg">You have not yet expensed Anything For This Month</div>
<?php } ?>
mainController.php index()
$config = array();
$config["base_url"] = base_url() . "mainController/index";
$config["total_rows"] = $this->expense_model->record_count($session_data['id']);
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["userexpense"] = $this->expense_model->get_user_expense($config["per_page"], $page,$session_data['id']);
$data["links"] = $this->pagination->create_links();
$data['userexpense_total'] = $this->expense_model->get_user_expense_total($session_data['id']);
$data['title'] = "Dashboard";
$this->load->view('template/header', $data);
$this->load->view('dashboard', $data);
$this->load->view('template/footer');
Have you tried loading your header and footer via the main dashboard view, rather than inline like this? CI loads views as a final action. Try loading your header and footer into your $data array and passing those to your dashboard view, like this:
In mainController.php
$data['header'] = $this->load->view('template/header', true);
$data['footer'] = $this->load->view('template/footer', true);
$this->load->view('dashboard',$data);
You may need to get a little creative with the data you're passing in so things don't get mixed up. You could also load your header and footer directly in your dashboard.php View file like so (as it will have all your $data).
In dashboard.php
$this->load->view('template/header', true);
$this->load->view('template/footer', true);

Button in view doesn't call method in controller

i have a problem : I have a view that needs to call a method in my controller EmployeeController to go to the page to add an employee. But it don't work.. also with other views it doe
This is the view:
<body>
<form class="form-horizontal" id='employeeform' id='employeeform' action="<?php echo base_url();?>EmployeeController/addEmployee" method="post">
<div class="container">
<div class="row">
<div class="col-lg-12 col-sm-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Naam werknemer</th>
<th>Datum aanwerving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($employeelist); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $employeelist[$i]->employee_no; ?></td>
<td><?php echo $employeelist[$i]->employee_name; ?></td>
<td><?php echo $employeelist[$i]->hired_date; ?></td>
<td><?php echo $employeelist[$i]->salary; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Add employee" />
</div>
</div>
</div>
</form>
</body>
THIS IS THE CONTROLLER:
class EmployeeController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
//load the employee model
$this->load->model('employee_model');
}
//index function
function index() {
$employeelist = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeelist;
$this->load->view('employee/employee_add', $data);
}
function createEmployee() {
$this->form_validation->set_rules('employee_no', 'Employee No', 'trim|required|numeric');
$this->form_validation->set_rules('employee_name', 'Employee Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('hired_date', 'Hired Date', 'required');
$this->form_validation->set_rules('salary', 'Salary', 'required|numeric');
if ($this->form_validation->run() == FALSE) {
//fail validation
$this->load->view('employee/employee_add');
} else {
//pass validation
$data = array(
'employee_no' => $this->input->post('employeeno'),
'employee_name' => $this->input->post('employeename'),
'hired_date' => #date('Y-m-d', #strtotime($this->input->post('hireddate'))),
'salary' => $this->input->post('salary'),
);
//insert the form data into database
$this->db->insert('tbl_employee', $data);
//display success message
$employeeresult = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeeresult;
//load the department_view
$this->load->view('employee/employee_view', $data);
redirect('employee/employee_view');
}
}
function addEmployee() {
$this->load->view('employee/employee_add');
//set validation rules
}
}
?>
THE ERROR I GET IS :
The requested URL /BoMaTec_afstudeerproject/CodeIgniter-3.0.1/EmployeeController/createEmployee was not found on this server.
Seems like you don't have any router for that url, or the router is wrong. As per the error, it's trying to load a folder named EmployeeController and within that folder, another folder named createEmployee. Check your .htaccess file and your routers in the framework you are using.

delete and update specific row in codeigniter

I am new in codeigniter.In my view page I am showing the data from database in a table i have two buttons in table to edit and delete the row..I want to delete a specific row from database through id.
## view
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>last name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td><button name="edit">Edit</button></td>
<td><button name="delete">Delete</button></td>
</tr>
<?php } ?>
</tbody>
----------
## Controller ##
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message',$data);
}
public function register()
{
$this->load->view("register");
}
public function receive()
{
$fname= $this->input->post("fname");
$this->load->model('Model');
$this->Model->insertdata($fname);
// echo $this->input->post("fname");
// echo $this->input->post("lname");
// echo $this->input->post("password");
// echo $this->input->post("Email");
}
public function all_user(){
$this->load->model('Model');
$data['records'] = $this->Model->get_all_users();
$this->load->view("store_data",$data);
}
public function row_delete(){
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
----------
## Model ##
class Model extends CI_Model{
public function insertdata($fname)
{
$data = array
('fname'=> $this->input->post("fname"),
'lname' => $this->input->post("lname"),
'email' => $this->input->post("email"),
'password' => $this->input->post("password"),
);
$this->db->insert('register',$data);
}
public function get_all_users()
{
$query= $this->db->get('register');
return $query->result();
}
public function delete_row()
{$this->db->where('id', $id);
$this->db->delete('');
}
}
Try this in ur code
//view
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td>Delete</td> <td>Edit</td>
</tr>
<?php } ?>
</tbody>
make sure u pass the record id to the view and base_url is configured on config file
//controller
public function row_delete(){
if(isset($_GET['id'])){
$id=$_GET['id'];
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);}
}
//model
public function delete_row()
{
$this->db->where('id', $id);
$this->db->delete('register');
}
reference links here and here
Just pass the parameters to the where function and table name to delete function:
public function delete_row()
{
// Set where
$this->db->where('id', $id);
// Run query
return $this->db->delete('register');
}
Cheers

Categories