Issues getting a html link changed in controller of codeigniter - php

I was using ajax calls. when I click on the edit link it would pop up a modal with all the information. I now want to change it, so that it goes to an edit page instead. The part I need changed is the onclick on the edit link bellow in the code from edit_record to instead call a function in my controller called edit. Any help would be appreciated I spent quit a long time trying different ways and keep getting errors.
Here is the controller function code called ajax_list
public function ajax_list()
{
$list = $this->your_table->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $your_table) {
$no++;
$row = array();
$row[] = $your_table->name;
$row[] = $your_table->title;
$row[] = $your_table->body;
//add html for action
$row[] = '<a class="btn btn-sm btn-link " href="javascript:void()" title="Edit" onclick="edit_record('."'".$your_table->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm text-warning" href="javascript:void()" title="Hapus" onclick="delete_record('."'".$your_table->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->your_table->count_all(),
"recordsFiltered" => $this->your_table->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
Here is the code in my your_controller called edit
//loads a single record with the data dispalyed for editing
function edit($id)
{
$data['r']=$this->your_table->get_by_id($id);
$this->load->view('pages/edit', $data);
}
//updates data in the database from the edit function
function save_edit()
{
$id=$this->input->post('txtid');
$data=array(
'name'=>$this->input->post('txtname'),
'title'=>$this->input->post('txttitle'),
'body'=>$this->input->post('txtbody'));
$this->db->where('id', $id);
$this->db->update('your_table', $data);
redirect('your_controller/index');
}

If your controller is called "edit" and your method has the same name, just change to:
//add html for action
$row[] = '<a class="btn btn-sm btn-link " href="/edit/edit/'.$your->table->id.'" title="Edit"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm text-warning" href="javascript:void()" title="Hapus" onclick="delete_record('."'".$your_table->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';

Related

Laravel Yajra DataTable Error if I added more than 1 record

I've create datatatable with Yajra on Laravel, here I want to display data on the table, here is my source code :
$model = SaranaPrasaranaRuang::with('jenis_ruang')->where('dibuat_oleh',$this->user->id)->get();
$dTable = Datatables()->of($model);
$dTable = $dTable->addIndexColumn()
->editColumn('jenis_ruang',function($ruang){
$jenis_ruang = $ruang->jenis_ruang->jenis_ruang;
return $jenis_ruang;
})
->editColumn('nama_ruang',function($data){
return $data->nama_ruang;
})
->addColumn('standard_prasarana',function($data){
$btn = '<button class="btn btn-warning detail_prasarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Prasarana</button>';
return $btn;
})
->addColumn('standard_sarana',function($data){
$btn = '<button class="btn btn-warning detail_sarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Sarana</button>';
return $btn;
})
->addColumn('action',function($data){
$btn = "";
$btn .= '<i class="fa fa-pencil"></i> Edit ';
$btn .= '</i> Hapus ';
$btn .= '<button class="btn btn-info" onclick=detailRuang('.$data->id.')><i class="fa fa-eye"></i> Detail</button>';
return $btn;
})
->rawColumns(['standard_prasarana','standard_sarana','action']);
return $dTable->make(true);
My code above is working fine if my record only 1, but when I added one more record on my table , Datable give me response json like this :
"error": "Exception Message:\n\nUndefined index: "
I've try with eloquent(),query() but still didn't work
Anyone can help me out ?
Try this
$model = SaranaPrasaranaRuang::with('jenis_ruang')->where('dibuat_oleh',$this->user->id)->get();
return DataTables::of($model)
->editColumn('jenis_ruang',function($ruang){
$jenis_ruang = $ruang->jenis_ruang->jenis_ruang;
return $jenis_ruang;
})
->editColumn('nama_ruang',function($data){
return $data->nama_ruang;
})
->addColumn('standard_prasarana',function($data){
$btn = '<button class="btn btn-warning detail_prasarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Prasarana</button>';
return $btn;
})
->addColumn('standard_sarana',function($data){
$btn = '<button class="btn btn-warning detail_sarana" onclick=detailSarpras(this,'.$data->id_jenis_ruang.')>Lihat Standard Sarana</button>';
return $btn;
})
->addColumn('action',function($data){
$btn = '';
$btn .= '<i class="fa fa-pencil"></i> Edit ';
$btn .= '</i> Hapus ';
$btn .= '<button class="btn btn-info" onclick=detailRuang('.$data->id.')><i class="fa fa-eye"></i> Detail</button>';
return $btn;
})
->rawColumns(['standard_prasarana','standard_sarana','nama_ruang','jenis_ruang','action']);
->addIndexColumn()
->make(true);

OneToOne Relationship in DataTables

I am using datatables from https://datatables.net/examples/server_side/simple.html and I am able to display all the information to the tables.
Now I have a column which stores foreign key as shown below.
The other table which is being pointed by source_of_member is
so here i have these kind of relationship and in frontend datatables my data are visualized as
Howeber here instead of foreign keys in Member Source column I want those Name to appear from another table which matches the id
my member model is as:
class Member extends Model
{
protected $fillable = ['name','mobile_number','organization_name','source_of_member','relationship_manager','referred_by'];
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'id');
}
}
and my memberSource model is as:
class MemberSource extends Model
{
protected $fillable = ['name'];
public function member()
{
return $this->hasOne('App\Member', 'id');
}
}
what i tried is:
public function getdata()
{
$members = Member::all();
return Datatables::of($members)
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
}
i dont know how to do this any help would be greatly appreciated.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In Controller:
$members = Member::with('memberSource')->get();
return Datatables::of($members)
->addColumn('source_of_member', function ($member) {
$member_name = '';
if(!empty($member->memberSource->name)){
$member_name = $member->memberSource->name;
}
return $member_name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
With the relationships working properly, you can simply edit the column in the datatable:
$members = Member::with('memberSource');
return Datatables::of($members)
->editColumn('source_of_member', function ($member) {
return $member->memberSource->name;
})
->addColumn('action', function($member){
return '<a data-id="'.$member->id.'" href="#" data-toggle="modal" id="openShow" class="btn btn-info btn-xs"><i class="fas fa-eye"></i></a> ' .
'<i class="fas fa-edit"></i> ' .
'<i class="fas fa-trash-alt"></i>';
})
->make(true);
However, your relationship configuration seems wrong, because your foreign key is called source_of_member and therefore laravel can't automatically detect it.
In your Member model:
public function memberSource()
{
return $this->hasOne('App\MemberSource', 'source_of_member');
}
In your MemberSource model:
public function member()
{
return $this->belongsTo('App\Member', 'source_of_member');
}

Passing String value from a controller

I want to pass the arguments as String type. But it shows me an error and i can see that it was not passed as a string. Is there a way to pass by string. Following is my function in the controller. (Look on to tag's onclick method from there i need to pass this as a string)
public function show(Request $request)
{
$output="";
if ($request->ajax()){
$models=DB::table('models')->where ('modelName','LIKE','%'.$request->search.'%')
->orWhere('brandName','LIKE','%'.$request->search.'%')->get();
if($models){
foreach ($models as $key=>$model){
$Mid=$model->modelName;
$output.='<tr>'.
'<td>'.$model->countryMade.'</td>'.
'<td>
<a class=" btn btn-success btn-sm" onclick="EditModel('.$model->id.','.$model->modelName.')" >Edit </a>
<a onclick="DeleteModel('.$model->id.')" class=" btn btn-danger btn-sm" >Delete </a>
</td>'.
'</tr>';
}
}
}
return response($output);
}
You can do it as ,
return response($output->__toString());

laravel datatable CPU USAGE

This is my Controller:
public function anyData()
{
$myID = Auth::guard('cashier')->user()->id;
$players = User::where('parent_id','=', $myID)
->where('delete', '=', 1)
->orderBy(DB::raw('LENGTH(name),name'))
->get(['id', 'name', 'score', 'alarm', 'bonus_from_percentage']);
return Datatables::of($players)
->addColumn('actions4', function ($players) {
return
'
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#actions-modal" class="open-playerID btn btn-warning" data-toggle="modal"
data-target="#actions-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.SETTINGS').'"><span class="glyphicon glyphicon-cog"></span></a>
';
})
->addColumn('actions3', function ($players) {
return
'
<button type="button" class="'.changeClass($players->alarm).'"
value="OK" onclick="Enable('.$players->id.','.$players->alarm.')" id="enable"/><span class="glyphicon glyphicon-ok-circle"></span></button>
';
})
->addColumn('actions2', function ($players) {
return
'
<a href="#" data-toggle="modal" data-id="'.$players->id.'" data-target="#changeplayerpassword-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.CHANGEPLAYERPASS').'" class="playeridchangepass btn btn-warning">
<span class="glyphicon glyphicon-edit"></span></a>
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#delete-modal" class="open-delete btn btn-warning" data-toggle="modal"
data-target="#delete-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.DELETE').'" ><span class="glyphicon glyphicon-remove"></span></a>
';
})
->addColumn('actions', function ($players) {
return
'<div class="btn-group text-center">
<button onclick="changeValueIn(this);" class="btn btn-primary btn-sm" style="width: 50px;" data-score-id="' . AppHelper::ToEuroC($players->score) . '" data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#In">IN</button>
<button onclick="changeValue(this)"class="btn btn-danger btn-sm" data-score-id="' . AppHelper::ToEuroC($players->score) . '"data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#Out">OUT</button>
</div>
';
})
->addColumn('score', function ($players) {
return AppHelper::ToEuroC($players->score);
})
->addColumn('bonus_from_percentage', function ($players) {
return AppHelper::ToEuroC($players->bonus_from_percentage);
})
->addColumn('players', function ($players) {
return ($players->name) . ' ' .dikOn($players->id);
})
->make(true);
}
This script i run to refresh table each 5 seconds:
// REFRESH TABLE
function refreshptable() {
if (table != null)
table.ajax.reload(null, false);
setTimeout(refreshplayers
, 5000);
}
wen i upload on server my laravel project (i have active around 300 Users online)
CPU load going 200+
On my old PHP code (No Laravel) CPU load is 10-15
its something wrong with datatables or is laravel ?
i check the request is same no loops and all querys is same the only difference is laravel..
on TOP -C laravel use 3% my old code use 0.3% ..

How to update or delete a data when the ID/primary was auto increment with php CodeIgniter Framework?

I can't update or delete my data on a table when the id/primary was auto increment, I have used a jquery but doesn't work, the data still can't update or delete
For view
<a href="#modal_bagian" class="btn btn-default edit" data-toggle="modal" name="" id="edit_<?PHP echo $row->no_bagian; ?>">
<span class="glyphicon glyphicon-pencil"></span> Ubah
</a>
$(".edit").click(function() {
$("#save").hide();
$("#update").show();
$("#no_bagian").attr("disabled", true);
$("#form_bagian").attr("action", "<?PHP echo site_url(); ?>bagian/update");
var id = this.id.substr(5);
$("#no_bagian").val(id);
$("#no_bagian_id").val(id);
$("#nama_bagian").val($("#nama_bagian_" + id).val());
For model
public function update()
{
$sql = "UPDATE tbl_bagian
SET nama_bagian='".$this->get_nama_bagian()."' +1
WHERE no_bagian='".$this->get_no_bagian()."'";
$this->db->query($sql);
}
For controller
public function update()
{
$this->bagian_m->set_no_bagian($this->input->post("no_bagian_id"));
$this->bagian_m->set_nama_bagian($this->input->post("nama_bagian"));
$this->bagian_m->update();
$data["status"] = "update_success";
$this->load->view("bagian_v", $data);
}

Categories