i need to insert employee and service id in a table - php

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);
}

Related

To get a link from database

Hi Im currently using codeigniter, Below i have attached the complete code. As i run this Im getting data from database but i want URL field to be link.How to do that.
This is my controller:
public function ajaxsearch()
{
if(is_null($this->input->get('id')))
{
$this->load->view('input_view');
}
else
{
$this->load->model('Infomodel');
$data['Infotable']=$this->Infomodel->Infotable($this->input->get('id'));
$this->load->view('output_view',$data);
}
}
Below is the model:
class Infomodel extends CI_Model
{
function Infotable($search)
{
$query = $this
->db
->select('*')
->from('Info_table')
->like('NAME',$search)
->or_like('URL',$search)
->get();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
}
and two views: one is input_view
And another is output_view:
if(!empty($Infotable ))
{
$output = '';
$outputdata = '';
$outputtail ='';
$output .= '<div class="container">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Url</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
';
foreach ($Infotable as $objects)
{
$outputdata .= '
<tr>
<td >'.$objects->id.'</td>
<td >'.$objects->name.'</td>
<td>'.$objects->url.'</td>
<td>'.$objects->tags.'</td>
</tr>
';
// echo $outputdata;
}
$outputtail .= '
</tbody>
</table>
</div>
</div> ';
echo $output;
echo $outputdata;
echo $outputtail;
}
else
{
echo 'Data Not Found';
}
Thanks in advance for the help.
if you mean that you want the url to be clickable by the user you can simply change this line:
<td>'.$objects->url.'</td>
to something like this:
<td>'.$objects->url.'</td>
This will create a link in the table cell that will send you to the link
Try using tag inside the table data.
<td> <a heref="'.$objects->url.'">'.$objects->url.'</a></td>

404 error when I try to update my form with id 1, but its not happen to another id

i have db with id 1,2,4 (img-1)
but if I try to update id_bank = 1, i got 404 error (img-2)
but that update work with another id (img-3)
this application work fine yesterday, but I got this error this morning, I dont know why, I really need help :(
Update:
I use codeigniter framework,
this is my controller code
function form($id_bank = false)
{
$this->id_bank = $id_bank;
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$data['page_title'] = lang('bank_form');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => lang('home'),
'href' => base_url()
);
$data['breadcrumbs'][] = array(
'text' => lang('bank_form'),
'href' => base_url('bank')
);
//default values are empty if the provinsi is new
$data['id_bank'] = '';
$data['nama_bank'] = '';
$this->form_validation->set_rules('nama_bank', 'lang:nama_bank', 'trim|required|max_length[64]');
//$this->form_validation->set_rules('id_categories', 'lang:id_categories', 'trim|required|max_length[64]');
// validate the form
if ($this->form_validation->run() == FALSE)
{
$this->view('bank/form', $data);
}
else
{
if (!$this->auth->hasPermission('modify', 'bank'))
{
$this->session->set_flashdata('error', lang('error_acces'));
redirect('bank');
}
$this->load->helper('text');
$save['id_bank'] = $id_bank;
$save['nama_bank'] = $this->input->post('nama_bank');
$this->Model_bank->save_provinsi($save);
$this->session->set_flashdata('message', lang('message_provinsi_saved'));
//go back to the category list
redirect('bank');
}
}
this is on my view
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th><?php echo lang('no');?></th>
<th><?php echo sort_url('nama_bank', 'nama_bank', $order_by, $sort_order, $code);?></th>
<th><?php echo sort_url('gambar', 'image', $order_by, $sort_order, $code);?></th>
<th class="text-center"><?php echo lang('action');?></th>
</tr>
</thead>
<tbody>
<?php echo (count($type) < 1)?'<tr><td style="text-align:center;" colspan="5">'.lang('no_type').'</td></tr>':''?>
<?php
$pages = ($page-1)*10;
foreach ($type as $merks):
$pages+=1;
?>
<tr>
<td style="width:10px;"><?php echo $pages; ?></td>
<td style=""><?php echo $merks->nama_bank; ?></td>
<td style="text-align:center"> <span><img width="50px" height="50px" src='<?php echo $this->config->item('url_image').'photo/bank/'.$merks->image?>'></span></td>
<td style="width:10%;" class="text-center">
<i class="fa fa-pencil"></i>
<?php if ($this->auth->hasPermission('delete', 'bank')) { ?>
<i class="fa fa-trash-o"></i>
<?php } ?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
this on my routes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "dashboard";

Codeigniter DATABASE Trouble

The thing is when i use this code
in my model :
public function get_all_subject_tasks(){
$this->db->select('*');
$this->db->from('task');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("task_id", "desc");
$query_result=$this->db->get();
$result=$query_result->result();
return $result;
}
in my controller:
public function subjects($t,$action=""){
$data=array();
$data['subject_tasks']=$this->Schoolmodel->get_all_subject_tasks($t);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
in my php page:
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } ?>
</table>
</div>
I get all of the tasks that are in the database, without any subject filter.
So my question is
How do i make the page echo the tasks based on which subject they are in?
Also here is how i have setup my database structure
subject_id
task_id
task_name
task_desc
user_id
Where subject_id is the id of the subject where the task is inserted in.
<?php
// your model
public function getTask($subject_id)
{
$result = $this->db
->where("subject_id", $subject_id) // !!!
->where("user_id", $this->session->userdata('user_id')) // !!!
->order_by("task_id", "desc")
->get('tasks');
return $result->num_rows() > 0 ?
$result->result() : false;
}
// your class
class School extends CI_Controller {
public function __construct() {
parent::__construct();
if($this->session->userdata('logged_in')==FALSE){
redirect('User');
}
$this->load->database();
$this->load->model('Schoolmodel');
$this->load->library('form_validation');
}
public function subjects($subject_id,$action=""){
$data=array();
$data['subject_tasks'] = $this->Schoolmodel->getTask($subject_id);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
}
?>
// your view
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php if(false !== $subject_tasks) { foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } } ?>
</table>
</div>
This is not going to work:
$this->db->where("subject_id",$subject_id AND "user_id",$this->session->userdata('user_id'));
From Active Record documentations: Multiple calls to where will result in AND:
$this->db->where("subject_id", $subject_id);
$this->db->where("user_id", $this->session->userdata('user_id'));

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.

I need to insert a array value in the data base

I am using codeigniter framework.I have a array of id that is to be saved in service_id column in my table. I need to store a array of id in my table. my table structure is as given below.
CREATE TABLE IF NOT EXISTS `addservice` (
`employee_id` int(10) NOT NULL,
`service_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I wrote a controller for this like:
class service extends CI_Controller
{
public function services()
{
$id = $this->session->userdata('employee');
$id_array = json_encode($this->input->post("services"));
$data_to_store=array('employee_id'=>$id,'service_id'=>$id_array));
$this->add_service_model->save($data_to_store);
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}
}
I used json_encode function. I dont get any error but i am getting a white screen. I wrote a view file where i post my id as check box.In this view file i am posting the service_id as a array and post them to my controller. My view file are:
<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">
<input type="checkbox" value="'.$row['id'].'" name="services[]"/>
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
my model file
class mymodel extends CI_Model{
public function get_addservices()
{
$this->db->select('*');
$this->db->from('addservice');
$query = $this->db->get();
return $query->result_array();
}
public function save($data)
{
$insert = $this->db->insert('addservice', $data);
return $insert;
}
}
here in this file, i used a function save to save data to my table
Remove json_encode from input post and use for loop to get value of array
Controller
public function services()
$id = $this->session->userdata('employee');
$id_array = $this->input->post("services");
for ($i = 0; $i < count($id_array); $i++) {// add for loop here
if(isset($id_array[$i])&& $id_array[$i]!=""){
$data_to_store = array('employee_id' => $id, 'service_id' => $id_array[$i]);
$this->add_service_model->save($data_to_store);
}
}
$data['addservice'] = $this->add_service_model->get_addservices();
$data['main_content'] = 'admin/service_limitation/newview';
$this->load->view('includes/template', $data);
}

Categories