So here is my code –
Controller
public function addCategory() {
if($this->session->userdata('logged_in') == TRUE) {
$this->load->model("categories");
echo $this->categories->addCategory();
}
}
Model –
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Categories extends CI_Model {
// Retrieves all category names and id's
function getCategories() {
}
// Add category to database. If there are errors returns message to AJAX controller
function addCategory() {
return "yeah";
das
$categoryName = $_POST['categoryName'];
$parentCategory = $_POST["parentCategory"];
$error = "";
if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
$query = $this->db->get_where("categories", array("name" => $categoryName));
if($query->num_rows() > 0) {
$error = "Category with that name already exists!";
}
else {
$data = array(
'name' => $categoryName,
'parent' => $parentCategory
);
$this->db->insert('categories', $data);
}
}
else {
$error = "Category Name can't be empty.";
}
if($error != "") {
return $error;
}
else {
return "Success! Category has been added!";
}
}
}
As you can see I specially added the return “yeah” and das, to see if it even works, but it doesn’t, it just returns blank page without errors. Also, this is AJAX request and if I add the model content inside the controller it’s working perfectly.
What could be the problem? If you need any other info, just give me a notice.
EDIT: Again, Das is only the thing I tried, and which didn’t show any errors also, here is my current model code without unused entries –
// Add category to database. If there are errors returns message to AJAX controller
function addCategory() {
$categoryName = $_POST['categoryName'];
$parentCategory = $_POST["parentCategory"];
$error = "";
if(isset($categoryName) && isset($parentCategory) && $categoryName != "") {
$query = $this->db->get_where("categories", array("name" => $categoryName));
if($query->num_rows() > 0) {
$error = "Category with that name already exists!";
}
else {
$data = array(
'name' => $categoryName,
'parent' => $parentCategory
);
$this->db->insert('categories', $data);
}
}
else {
$error = "Category Name can't be empty.";
}
if($error != "") {
return $error;
}
else {
return "Success! Category has been added!";
}
}
rename the
class name Categories to Categories_modeland name thefile as categories_model.php.you are probably not able to access the model file because you have put the wrong class name in the model file