CodeIgniter deleting with db doesnt work - php

I am doing an ajax call..which doesnt output any mysql errors..Basically, I am trying to delete two records...
public function task_delete($task_id)
{
$this->load->database();
$this->db->delete('task_pages', array('id' => $task_id));
$this->db->delete('tasks', array('id' => $task_id));
}
This is the model..
And this is the controller..
class Ajax extends CI_Controller {
public function delete()
{
$this->load->database();
$this->load->model('tasks_model','task_delete');
$result=$this->task_delete-> task_delete($this->input->post('myId'));
echo $result;
}
}
Where does my code fails?
UPDATE:
I get an error..the method deleteTask is being called on none object

Assuming your model is also called task_delete and the function you want to call is deleteTask, then you have given the function in your model the wrong name, it should be declared as this:
public function deleteTask($task_id)

Related

codeigniter restful api returning empty array

I am trying to fetch items by id but instead its returning null array.The code works fine when I fetch all the items.
Here is the controller:
class Search extends REST_Controller
{
function __construct()
{
// Construct the parent class
parent::__construct();
$this->load->model('Courses_model');
$this->load->model('Courses_category_model');
$this->load->model('Courses_lessons_model');
$this->load->model('Courses_photos_model');
$this->load->model('Providers_model');
}
function index_get()
{
$category_id = $this->get('category_id');
$result = $this->Courses_model->get_courses_category($category_id);
$this->response([
'status' => TRUE,
'data' => $result
], REST_Controller::HTTP_OK);
}
}
and the model file is here and the model file is here:
class Courses_model extends CI_Model
{
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
}
public function get_courses_category($category_id)
{
$this->db->where('category_id', $category_id);
$query = $this->db->get('courses');
return $query->result();
}
i have seem u r question and find out ans please check the below link i think help u.
https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php
RestApi Post request using CodeIgniter
hahaa. Finally, after a little snap I solved this. I just defined the routes and it worked like a charm.

Codeigniter - Appending variables names to a method name to run Model function

Not sure if possible but what I am trying to do is pass a query string through a get request to my controller. From here I am getting the query string value using $_GET['name']. What I would like to do then is use that GET value and append it as the name of the method I will be passing to my Model to return the data I need.
This is because multiple action with pass different query string values to the same controller but then use the value to get the data from different functions within my model.
e.g.
Controller
class ControllerName extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
public function index() {
$queryName = $_GET['query_string']
// e.g. $queryName = 'customer'
//Use the query string and append to method to pass to Model function
$result = $this->my_model->$queryName._get_query(); //???
//e.g. $result = $this->my_model->customer_get_query();
}
}
My_model
class My_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function customer_get_query() {
//...run database query and return result
}
}
Any thoughts?
The usual php should work just fine:
$result = call_user_func([$this->my_model, $queryName.'_get_query']);

Codeigniter Call Parent Controller Function Inside Model

I know this is MVC structure incompatible but i need to use this technique:
I have a controller and a model.
I'm calling a function in model from controller.
Model called function calles controllers another function. (This is what generates error).
Example below:
Controller:
public function B($ret=false) {
if(!$ret)$this->Model_model->M($ret);
else echo 'ok';
}
Model:
public function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this->Controller->B(!$ret); //i can't find how can i call this
}
My first trigger function is:
$this->Controller->B(false);
I've moved code in function B of controller to the Model completely and now everything is going in model itself. B function calling another B function in model and at last step model isn't need to call controller; it is calling B function in Model:
Controller:
public function B($ret=false) {
$this->Model_model->B($ret);
}
Model:
public function B($ret){
if(!$ret)$this->M($ret);
else echo 'ok';
}
private function M($ret=false) {
$this->N($ret);
}
private function N($ret=false) {
$this-B(!$ret);
}
My first trigger function is:
$this->B(false);
And my controller is still has short code.

Codeigniter controller model call fail

I am new to CI and I want to update some data in mysql . So here is my controller
class Ci_update extends CI_Controller
{
function __construct() {
parent::__construct();
}
function index()
{
$data = array
(
'title' => 'Data Structure using C',
'text' => 'Data Structure Using C, for, IIIrd Sem VTU CSE students'
);
$id = 4 ;
$this->load->model('ci_update_model');
$this->ci_update_model($data,$id);
}
}
and my model is :
class Ci_update_model extends CI_Model
{
function __construct() {
parent::__construct();
}
function updateData($data,$id)
{
$this->db->where('id',$id);
$this->db->update('data',$data);
}
}
But when I tried to run the program , it says Call to undefined method Ci_update::ci_update_model() in C:\wamp\www\ci\application\controllers\ci_update.php on line 19
What wrom am I doing?
Use as below
$this->load->model('ci_update_model');
$this->ci_update_model->updateData($data,$id);
DO this changes in your Controller code,Rest is same:
$this->load->model('ci_update_model');
$this->ci_update_model->updateData($data,$id);
in your controller's constructer add this line
$this->load->model('Ci_update_model');
and the error will get resolved

Cakephp error:Call to a member function find() on a non-object

I am having problems with my code. When I try to debug my web application I get the following error message....Call to a member function find() on a non-object.....here is my code
class TeamsController extends AppController {
var $name = 'Teams';
function index() {
$this->set('teams', $this->team->find('all'));
}
function Welcome() {
}
}
I am trying to display records from my MySQL database. Now with that said, I did this tutorial and I followed the instructions down to the tee.....but somehow my code has bugs. The only difference between my code and the code of the tutorial I did is the variable names...and the controller names....and the I dont have the hello world function... Here is a sample of the code from the tutorial I did....
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function hello_world() {
}
}
With that said, am I suppose to declare an instance of an object to get this to work?
It's likely a case sensitivity issue:
function index() {
$this->set('teams', $this->Team->find('all'));
}
If not, ensure your controller has access to the Teams model (e.g. $uses).

Categories