codeigniter delete row from table - php

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.

Related

CodeIgniter : display two query result with one return

I am trying to display 'Category' database and this part is quite easy. The hard part is I need to count the subcategory from the 'Category' database and displaying them with one table.
Category | Sub Category
------------------------
Cat A | 5
Cat B | 7
here is my Model:
function category() {
$query = $this->db->get('category');
$result = $query->result_array();
foreach($query->row() as $q) {
$this->db->where('subcat_id', $q['cat_id']);
$query2 = $this->db->get('subcat');
if($query2) {
return true;
} else {
return false;
}
here is my Controller:
function dispaly_category() {
$data['category'] = $this->mymodel->category();
$this->load->view('view', $data);
}
here is my View:
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo (count subcat for the above category); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
i just post an answer with the assumption you've one Table only (you don't need a separate table for subcategories, but in case you really need your second table you should be able to derive this based on my answer)
Your Model
function category()
{
$query = $this->db
->select("c.*, count(cc.id) AS countSubCategories")
->from("category c")
->join("category cc", "cc.parent_id = c.cat_id","left")
->group_by("c.id")
->get();
return $query->result();
}
Your Controller
function display_category()
{
$arrViewData = array(
'category' => $this->mymodel->category()
);
$this->load->view('view', $arrViewData);
}
your view
<table>
<thead>
<th>Category</th>
<th>Subcategory</th>
</thead>
<tbody>
<?php foreach($category as $c) : ?>
<tr>
<td><?php echo $c->category_name; ?></td>
<td><?php echo $c->countSubCategories; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Codeigniter: How to avoid displaying duplicated values or overwrite a duplicated value in PHP foreach loop in a table?

I'm currently working on codeigniter. I want to display a value that is not been duplicated or overwrite a duplicated value from MySQL database into the datatable of PHP foreach loop.
Here is the picture my 2 tables in database:
Table "employees" & table "times"
Here is the relationship of my 2 tables:
Relation of table "employees" & table "times"
Here is my controller (home.php):
public function goPresent() { // attendance present page
$this->load->model('Model_attendance');
$query = $this->Model_attendance->getEmployees();
$data['EMPLOYEES'] = null;
if ($query) {
$data['EMPLOYEES'] = $query;
}
$this->load->view('imports/header');
$this->load->view('imports/menu');
$this->load->view('attend', $data);
}
Here is my model (model_attendance.php):
class Model_attendance extends CI_Model {
public $userID;
public $username;
public $password;
public $totime;
public $absence_type;
public $date;
public $hours;
public function getEmployees() {
$this->db->select("*");
$this->db->from('times');
$this->db->join('employees', 'employees.empnum = times.userID');
$query = $this->db->get();
return $query->result();
}
}
Here is my view (attend.php):
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
foreach($EMPLOYEES as $employee){?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php } ?>
</tbody>
</table>
on which basis you want unique values, just add this line in your query....
$this->db->group_by("column name which needs to be unique");
<?php
$customers = $this->db->group_by('customer_id')->get_where('registered_table' , array(
'chat_group_id' => $group_id , 'year' => $reg_year
))->result_array();
foreach($customers as $row):?>.....
the example code above will find duplicated customer_id and avoids printing out duplicated values as you intended. Here i used group_by('customer-id')

Update specific database record in codeigniter

Good day, I have some trouble making an update specific record on a selected row from the database. I want to display the data after clicking on the edit icon from the selected data from the table display table.
Controller:
//Show Users/Members page
public function users_page() {
$data['users'] = $this->user->get_users();
$this->load->view('user_page' , $data);
}
Model:
// Retrieve user data from database
public function get_users() {
$this->db->select('id, email, first_name, last_name, date_registered');
$this->db->from('auth_users');
$query = $this->db->get();
$result = $query->result();
foreach ($result as $key => $value) {
// var_dump($key);
$data[$key] = array(
'id' => $value->id,
'firstname' => $value->first_name,
'lastname' => $value->last_name,
'datereg' => $value->date_registered,
'email' => $value->email
);
}
return $data;
}
View:
<table class="table table-condensed">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Date Registered</th>
<th>Action</th>
</tr>
<?php foreach($users as $key => $value) {?>
<tr>
<td><?php echo $value['firstname']; ?></td>
<td><?php echo $value['lastname']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><?php echo $value['datereg']; ?></td>
<td><i class="material-icons" title="Click to update">mode_edit</i> |
<i class="material-icons" title="Click to remove">delete</i></td>
</tr>
<?php } ?>
</thead>
<tbody>
</tbody>
</table>
create a new method in your controller, something like this (not exactly like this as I don't know the classes you're already using)
public function show_user($id) {
$data['users'] = $this->user->get_user($id);
$this->load->view('user_page' , $data);
}
create another method in your model, again something like this
public function get_user($id) {
$this->db->select('id, email, first_name, last_name, date_registered');
$this->db->from('auth_users');
$this->db->where('id', $id);
$query = $this->db->get();
$result = $query->result();
$var_dump($result); --or write a view an pass the result to the view
create a form taking the result from the previous (This I cannot write for you)
create an update method in your controller and model (just like you just did to query the information, but now to update the information from this record)

i need to insert employee and service id in a table

I need to insert employee id and service id into a new table addservice.i wrote a function add in my controller page where i get my employee id from the session and service id from the service table from the uri.
In controller
public function add()
{
$this->load->library('session');
$id = $this->uri->segment(4);
$data_to_store = array('employee_id'=>$taxdetails['total'],
'service_id'=>$id);
$this->selected_model->store_employee($data_to_store);
$data['main_content'] = 'admin/selected/list';
$this->load->view('includes/template', $data);
}
in model:
this is my model file where wrote the code to insert the employee id and service id.
public function store_employee($data)
{
$insert = $this->db->insert('addservice', $data);
return $insert;
}
in view page
<div class="container top">
<ul class="breadcrumb">
<li>
<a href="http://localhost/elfanto/elfanto_billing/admin/addservice">
Admin </a>
<span class="divider">/</span>
</li>
<li class="active">
Service </li>
</ul>
<div class="row">
<div class="span12 columns">
<div >
<?php
$attributes = array('class' => 'form-inline reset-margin', 'id' => 'myform');
$options_manufacture = array(0 => "all");
foreach ($category as $row)
{
$options_manufacture[$row['id']] = $row['name'];
}
//save the columns names in a array that we will use as filter
$options_products = array();
foreach ($service as $array) {
foreach ($array as $key => $value) {
$options_products[$key] = $key;
}
break;
}
?>
</div>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="header">Service id</th>
<th class="yellow header headerSortDown">Service name </th>
<th class="green header">Service catogary</th>
<th class="red header">Service tax</th>
<th class="red header">Service length</th>
<th class="red header">Service price</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($service as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['service_name'].'</td>';
echo '<td>'.$row['category'].'</td>';
echo '<td>'.$row['service_tax'].'</td>';
echo '<td>'.$row['service_length'].'</td>';
echo '<td>'.$row['service_price'].'</td>';
echo '<td class="crud-actions">
Add service
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
Continue as chair renter
<?php echo '<div class="pagination">'.$this->pagination->create_links().'</div>'; ?>
</div>
</div>
this code doesnt work for. i am not able to resolve my problem can someone correct my code.
In your url try adding index.php after projectname http://localhost/elfanto/index.php/elfanto_billing/admin/addservice and the link does not match the functions.
Example
http://localhost/project/index.php/controller/add/
First I would recommend auto load the session library to get
To get any set session userdata you need to do some thing like but do not know how you set your sessions.
$this->session->userdata('total')
Controller Function
public function add() {
$this->selected_model->store_employee();
$data['main_content'] = 'admin/selected/list';
$this->load->view('includes/template', $data);
}
Model Function
public function store_employee()
{
$data = array(
'employee_id' => $this->session->userdata('total'),
'service_id' => $this->uri->segment(4)
);
$this->db->insert('addservice', $data);
}
if session set in array()
$taxdetails = $this->session->userdata('some_array_name');
$taxdetails['total']
Function example 2
public function store_employee()
{
$taxdetails = $this->session->userdata('some_array_name');
$data = array(
'employee_id' => $taxdetails['total'],
'service_id' => $this->uri->segment(4)
);
$this->db->insert('addservice', $data);
}

Pagination links not working

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

Categories