Where clause in mysql not working - php

I am working on a project using php codeigniter,i am using mysql select query to fetch all data from database where isactive field is equal to 1.but i am unable to do it.below is the function where i have writtten the query.
Code:
public function displayinfo3()
{
$user_id = $this->session->userdata('user_id');
if($user_id)
{
$this->load->library('datatables');
$this->datatables
->select("user_type.user_type as user_type,user.user_name as user_name,user.first_name as first_name,user.middle_name as middle_name,user.last_name as last_name,user.user_id as Actions")
->from("user")
->where('isactive'==1)
->join("user_type","user_type.user_type_id=user.user_type_id","left")
->edit_column("Actions",
"<center><div class='visible-md visible-lg hidden-sm hidden-xs'><div align='center'><div class='col-md-2'><form action='".base_url()."index.php/Registrationc/editpatient3' method='post'><input type='hidden' id='editp3' name='editp3' value='$1'><button title='EDIT PATIENT' class='tip btn btn-primary btn-xs' type='submit' value='Submit' ><i class='fa fa-edit'></i></button></form></div><div class='col-md-2'><a class=\"tip btn btn-danger btn-xs\" title='DELETE PATIENT' href='#' onclick=\"delete_patient('$1')\" ><i class=\"fa fa-trash-o\"></i></a></div></div></div></center>", "Actions");
// $this->datatables->unset_column('Sno.patient_id');<form action=""><div class='btn-group'>
//<a class=\"tip btn btn-primary btn-xs\" onclick=\"edit_patient('$1')\" title='EDIT MAPPING' href='#' return false;\"><i class=\"fa fa-edit\"></i></a>
echo $this->datatables->generate();
}
else
{
redirect(base_url('Registrationc/extra1'));
}
}

Replace ->where('isactive'==1) with ->where('isactive = 1')
The first passes the result of the comparison of 'isactive' with 1 to the function. As 'isactive' is not equal to 1 you just pass a false value to your where statement.
The second call passes the correct query string to be put in the where statement of the query.

You need to change the ->where('isactive'==1) to ->where('isactive = 1') and returns a bool result.

Related

Error Missing required parameters for [Route] after upgrading Laravel 5.8 to 6.0

After upgrading Laravel 5.8 to 6.0 I get the below error:
local.ERROR: Missing required parameters for [Route:
playlist-song.show] [URI: public/admin/playlist-song/{playlist_song}].
{"userId":1,"exception":"[object]
(Illuminate\Routing\Exceptions\UrlGenerationException(code:
0):Missing required parameters for [Route: playlist-song.show] [URI:
public/admin/playlist-song/{playlist_song}].at
/var/www/vendor/laravel/framework/src/Illuminate/Routing/Exceptions/UrlGenerationException.php:17)
Chrome Inspect Result:
I have tried to find error and update route using _ or - but still can't solve this error:
any idea to solve this issue?
Thank You in Advance...
===Update
I suspect error here:
$datatables = app('datatables')->of($model)
->addColumn('action', function ($model) {
return "<a href='". route('playlist-song.show', ['id' => $model->id]) ."' class='btn btn-success btn-sm'><i class='fa fa-eye'></i></a>" .
" <a href='". route('playlist-song.edit', ['id' => $model->id]) . "' class='btn btn-primary btn-sm'><i class='fas fa-pencil-alt'></i></a>" .
" <a href='#' onClick='modalDelete(".$model->id.")' class='btn btn-sm btn-danger'><i class='fas fa-trash'></i></a>";
});
Update the URL like below still can not solve the problem:
href='". {{ route('playlist-song.show', ['playlist-song' => $model])
}}."'
href="{{ route('playlist-song.show',
['playlist-song' => $model]) }}"
Your error tells you that your playlist_song parameter is missing. You will need to specify its value. A try would be
$datatables = app('datatables')->of($model)
->addColumn('action', function ($model) {
return "<a href='". route('playlist-song.show', ['playlist_song' => $model->id]) ."' class='btn btn-success btn-sm'><i class='fa fa-eye'></i></a>" .
" <a href='". route('playlist-song.edit', ['playlist_song' => $model->id]) . "' class='btn btn-primary btn-sm'><i class='fas fa-pencil-alt'></i></a>" .
" <a href='#' onClick='modalDelete(".$model->id.")' class='btn btn-sm btn-danger'><i class='fas fa-trash'></i></a>";
});
This specifies the parameter. If you still have problems, then let me know what your new error message/problem is.
you can do this by using view also as the following:
$datatables = app('datatables')->of($model)
->addColumn('action', function($model) {
return view('partial.action', compact('model'));
});
create your view where you want, and you can put inside it normal html code without concatenation and any other issue
the blade file :
<a href="{{ route('playlist-song.show', ['id' => $model->id]) }}" class='btn btn-success btn-sm'><i class='fa fa-eye'></i></a>
<a href="{{ route('playlist-song.edit', ['id' => $model->id]) }}" class='btn btn-primary btn-sm'><i class='fas fa-pencil-alt'></i></a>
<a href='#' onClick="modalDelete('{{$model->id}}')" class='btn btn-sm btn-danger'><i class='fas fa-trash'></i></a>

Issues getting a html link changed in controller of codeigniter

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>';

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

How can page through the results? laravel

How can page through the results? .It With ajax.
to bring the data to my view , I want to use paginate () and orderby (), but to put it is in the documentation , it generates error.
if I do so : $cita= Cita::orderBy(asc)->paginate(10), I did not bring the data. Excuse the English 'm Mexican .
Controller:
public function agenda(){
$cita = Cita::all();
return Response()->json(
$cita->toArray()
);
}
Script:
function Carga(){
var tablaDatos = $("#datos");
var route = "http://cmec.app/agenda";
$("#datos").empty();
$.get(route, function(res){
$(res).each(function(key, value){
tablaDatos.append(
"<tr><td>"+value.name+"</td><td>"+value.lastname+"</td><td>"+value.doctor+"</td><td>"+value.fecha+"</td><td>"+value.hora+"</td><td><div class='btn-group btn-group-sm' role='group'><button value="+value.id+" OnClick='Mostrar(this);' class='btn btn-danger'><i class='fa fa-trash-o'></i></button><button value="+value.id+" OnClick='Mostrar(this); ' class='btn btn-primary' data-toggle='modal' data-target='#editar-modal' ><i class='fa fa-pencil-square'></i></button></div></td></tr>");
});
});}
Vista:
<table class="table table-hover">
<thead>
<tr>
<th>Nombre</th>
<th>Apellidos</th>
<th>Doctor</th>
<th>Fecha</th>
<th>Hora</th>
<th>Acciones</th>
</tr>
</thead>
<tbody id="datos"></tbody>
</table>
Use following for you code
Controller
public function agenda(){
$cita = Cita::orderBy('asc')->paginate(10);
return response()->json($cita->toArray());
}
Script
function Carga(){
var tablaDatos = $("#datos");
var route = "http://cmec.app/agenda";
$("#datos").empty();
$.getJson(route, function(res){
$(res).each(function(key, value){
tablaDatos.append(
"<tr><td>"+value.name+"</td><td>"+value.lastname+"</td><td>"+value.doctor+"</td><td>"+value.fecha+"</td><td>"+value.hora+"</td><td><div class='btn-group btn-group-sm' role='group'><button value="+value.id+" OnClick='Mostrar(this);' class='btn btn-danger'><i class='fa fa-trash-o'></i></button><button value="+value.id+" OnClick='Mostrar(this); ' class='btn btn-primary' data-toggle='modal' data-target='#editar-modal' ><i class='fa fa-pencil-square'></i></button></div></td></tr>");
});
});
}
Error :
QueryException in Connection.php line 636:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'asc' in 'order clause' (SQL: select * from `Citas` order by `asc` asc limit 10 offset 0)

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