Undefined method in Code Igniter framework - php

I've problem in CodeIgniter framework,
Controller :
public function tambah() {
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
if ($this->form_validation->run() === FALSE) {
$data=array('title'=>'Menambah Berita',
'isi' =>'admin/berita/tambah_berita'
);
$this->load->view('admin/layout/wrapper',$data);
}else{
$tag = url_title($this->input->post('judul'), 'dash', TRUE);
$data = array(
'judul' => $this->input->post('judul'),
'tag' => $tag,//edited
'deskripsi' => $this->input->post('deskripsi'),
'isi' => $this->input->post('isi'),
'status' => $this->input->post('status'),
'id_admin' => $this->input->post('id_admin')
);
$this->berita_model->tambah($data);
redirect(base_url().'admin/berita/');
}
}
Model :
public function tambah($data) {
return $this->db->insert('lm_destination', $data);
}
View : view file has fixed.
Those code yields :
Fatal error: Call to undefined method Berita_model::tambah() in C:.\application\controllers\admin\file.php on line 41
Anybody can help me fix it? thanks

You must write
$this->load->model('berita_model');
before use
$this->berita_model->tambah($data);

I hope this will help YOu
Controller :
public function tambah() {
$this->form_validation->set_rules('judul', 'Judul', 'required');
$this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
$this->form_validation->set_rules('isi', 'Isi', 'required');
if ($this->form_validation->run() === FALSE) {
$data=array('title'=>'Menambah Berita',
'isi' =>'admin/berita/tambah_berita'
);
$this->load->view('admin/layout/wrapper',$data);
}else{
$tag = url_title($this->input->post('judul'), 'dash', TRUE);
$data = array(
'judul' => $this->input->post('judul'),
'tag' => $tag,//edited
'deskripsi' => $this->input->post('deskripsi'),
'isi' => $this->input->post('isi'),
'status' => $this->input->post('status'),
'id_admin' => $this->input->post('id_admin')
);
$this->load->model('berita_model');
$this->berita_model->tambah($data);
redirect(base_url().'admin/berita/');
}
}
Model :
public function tambah($data) {
$this->db->insert('lm_destination', $data);
return true;
}

Related

Model & Controller error on my website project

Please help me, I cannot access my code it always shows the error:
An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Quotation_model::addModels(), 1 passed in C:\xampp\htdocs\sangil\application\controllers\Quotation.php on line 126 and exactly 2 expected
Filename: C:\xampp\htdocs\sangil\application\models\Quotation_model.php
Line Number: 122
Backtrace:
File: C:\xampp\htdocs\sangil\application\controllers\Quotation.php
Line: 126
Function: addModels
File: C:\xampp\htdocs\sangil\index.php
Line: 315
Function: require_once
This is the code:
Controller: Quotation.php
public function addQuotation()
{
$this->form_validation->set_rules('date', 'Date', 'trim|required');
$this->form_validation->set_rules('reference_no', 'Reference No', 'trim|required');
if ($this->form_validation->run() == false) {
$this->add();
} else {
$warehouse_id = $this->input->post('warehouse', true);
$data = array(
"date" => $this->input->post('date'),
"reference_no" => $this->input->post('reference_no'),
"warehouse_id" => $this->input->post('warehouse'),
"customer_id" => $this->input->post('customer'),
"biller_id" => $this->input->post('biller'),
"total" => $this->input->post('grand_total'),
"discount_value" => $this->input->post('total_discount'),
"tax_value" => $this->input->post('total_tax'),
"note" => $this->input->post('note'),
"shipping_city_id" => $this->input->post('city'),
"shipping_state_id" => $this->input->post('state'),
"shipping_country_id" => $this->input->post('country'),
"shipping_address" => $this->input->post('address'),
"shipping_charge" => $this->input->post('shipping_charge'),
"internal_note" => $this->input->post('internal_note'),
"mode_of_transport" => $this->input->post('mode_of_transport'),
"transporter_name" => $this->input->post('transporter_name'),
"transporter_code" => $this->input->post('transporter_code'),
"vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
"user" => $this->session->userdata('user_id')
);
if ($quotation_id = $this->quotation_model->addModel($data)) {
$log_data = array(
'user_id' => $this->session->userdata('user_id'),
'table_id' => $quotation_id,
'message' => 'Quotation Inserted'
);
$this->log_model->insert_log($log_data);
$sales_item_data = $this->input->post('table_data', true);
$js_data = json_decode($sales_item_data);
foreach ($js_data as $key => $value) {
if ($value == null) {
} else {
$product_id = $value->product_id;
$quantity = $value->quantity;
$data = array(
"product_id" => $value->product_id,
"quantity" => $value->quantity,
"price" => $value->price,
"gross_total" => $value->total,
"discount_id" => $value->discount_id,
"discount_value" => $value->discount_value,
"discount" => $value->discount,
"tax_id" => $value->tax_id,
"tax_value" => $value->tax_value,
"tax" => $value->tax,
"quotation_id" => $quotation_id
);
//$this->sales_model->checkProductInWarehouse($product_id,$quantity,$warehouse_id);
if ($this->quotation_model->addQuotationItem($data, $product_id, $warehouse_id, $quantity)) {
} else {
}
}
}
redirect('quotation/view/' . $quotation_id);
} else {
redirect('quotation', 'refresh');
}
}
}
Model: Quotation_model.php
public function addModel($data, $invoice)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
} else {
return FALSE;
}
}
you create model with 2 input
$data and $invoice
public function addModel($data, $invoice)
but only give 1 input in the controller when you call the model
$this->quotation_model->addModel($data)
delete the $invoice in your model if you dont need it, or add one more input when you call it from the controler.
public function addModel($data)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
}
return false;
}
Controller: Quotation.php
$quotation_id = $this->quotation_model->addModel($data);
so in your Model: Quotation_model.php
public function addModel($data)
{
if ($this->db->insert('quotation', $data)) {
return $this->db->insert_id();
} else {
return FALSE;
}
}

Login System Not Working, CodeIgniter

First of all, this post is going to be long, thanks for helping tho.
Hello so I've been trying to create a login and register system on my CodeIgniter Application. So far it works great, I can create and fetch them(if not logged in) wherever I want them to be displayed, the problem that now comes to me is the restricting part.
For example I have an admin_controller to which I need to restrict the access. Rather than adding a code to any controller that needs the same configuration, I created a "MY_Controller" in the core folder, here is my code:
<?php
class MY_Controller extends CI_Controller{
function __construct(){
parent::__construct();
}
}
class Admin_Controller extends MY_Controller{
function __construct(){
parent::__construct();
// Check Login
/*if(!$this->session->userdata('logged_in')){
redirect('admin/login');
}*/
}
}
class Public_Controller extends MY_Controller{
public function __construct(){
parent::__construct();
$this->load->library('menu');
$pages_public /*$this->pages*/ = $this->menu->get_pages();
// Brand/Logo
$this->brand = 'My Website';
// Banner
$this->banner_heading = 'Welcome To Our Website';
$this->banner_text = 'This example is a quick exercise to illustrate how the default, static navbar and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.';
$this->banner_link = 'pages/show/our-team';
}
}
and this is what I have on my admin_controller, so far nothing wrong:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends Admin_Controller {
public function index(){
$data['pages'] = $this->Page_model->get_list();
// Load template
$this->template->load('admin', 'default', 'pages/index', $data);
}
public function add(){
// Field Rules
$this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[3]');
$this->form_validation->set_rules('subject_id', 'Subject', 'trim|required');
$this->form_validation->set_rules('body', 'Body', 'trim|required');
$this->form_validation->set_rules('is_published', 'Publish', 'required');
$this->form_validation->set_rules('is_featured', 'Feature', 'required');
$this->form_validation->set_rules('order', 'Order', 'integer');
if($this->form_validation->run() == FALSE){
$subject_options = array();
$subject_options[0] = 'Select Page Category';
$subject_list = $this->Pages_categories_model->get_list();
foreach($subject_list as $subject){
$subject_options[$subject->id] = $subject->name;
}
$data['subject_options'] = $subject_options;
// Load template
$this->template->load('admin', 'default', 'pages/add', $data);
} else {
$slug = str_replace(' ', '-', $this->input->post('title'));
$slug = strtolower($slug);
// Page Data
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'subject_id' => $this->input->post('subject_id'),
'body' => $this->input->post('body'),
'is_published' => $this->input->post('is_published'),
'is_featured' => $this->input->post('is_featured'),
'in_menu' => $this->input->post('in_menu'),
'user_id' => $this->session->userdata('user_id'),
'order' => $this->input->post('order')
);
// Insert Page
$this->Page_model->add($data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'page',
'action' => 'added',
'user_id' => $this->session->userdata('user_id'),
'message' => 'A new page was added ('.$data["title"].')'
);
// Insert Activity
$this->Activity_model->add($data);
// Set Message
$this->session->set_flashdata('success', 'Page has been added');
// Redirect
redirect('admin/pages');
}
}
public function edit($id){
// Field Rules
$this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[3]');
$this->form_validation->set_rules('subject_id', 'Subject', 'trim|required');
$this->form_validation->set_rules('body', 'Body', 'trim|required');
$this->form_validation->set_rules('is_published', 'Publish', 'required');
$this->form_validation->set_rules('is_featured', 'Feature', 'required');
$this->form_validation->set_rules('order', 'Order', 'integer');
if($this->form_validation->run() == FALSE){
$data['item'] = $this->Page_model->get($id);
$subject_options = array();
$subject_options[0] = 'Select Page Category';
$subject_list = $this->Pages_categories_model->get_list();
foreach($this->Pages_categories_model->get_list() as $subject){
$subject_options[$subject->id] = $subject->name;
}
$data['subject_options'] = $subject_options;
// Load template
$this->template->load('admin', 'default', 'pages/edit', $data);
} else {
$slug = str_replace(' ', '-', $this->input->post('title'));
$slug = strtolower($slug);
// Page Data
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'subject_id' => $this->input->post('subject_id'),
'body' => $this->input->post('body'),
'is_published' => $this->input->post('is_published'),
'is_featured' => $this->input->post('is_featured'),
'in_menu' => $this->input->post('in_menu'),
'user_id' => $this->session->userdata('user_id'),
'order' => $this->input->post('order')
);
// Update Page
$this->Page_model->update($id, $data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'page',
'action' => 'updated',
'user_id' => $this->session->userdata('user_id'),
'message' => 'A page was updated ('.$data["title"].')'
);
// Insert Activity
$this->Activity_model->add($data);
// Set Message
$this->session->set_flashdata('success', 'Page has been updated');
// Redirect
redirect('admin/pages');
}
}
public function delete($id){
$title = $this->Page_model->get($id)->title;
// Delete Page
$this->Page_model->delete($id);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'page',
'action' => 'deleted',
'user_id' => $this->session->userdata('user_id'),
'message' => 'A page was deleted'
);
// Insert Activity
$this->Activity_model->add($data);
// Set Message
$this->session->set_flashdata('success', 'Page has been deleted');
// Redirect
redirect('admin/pages');
}
}
the problem comes from the controller users_controller. I already created an account with some data and that data should at least allow me to have access to the admin_controller which it does not, instead it redirects me to the admin/login form.
I would like to say that for some reason when I tried to add a page, I get an error message saying that user_id cannot be null, but as I'm "supposed" to be logged in that error should not appear. Any knows how to fix it?
Error Message
Error Number: 1048 Column 'user_id' cannot be null INSERT INTO 'pages'
('title', 'slug', 'subject_id', 'body', 'is_published', 'is_featured',
'in_menu', 'user_id', 'order') VALUES ('Page One', 'page-one', '1', '
thrhjtyjrjrj ', '1', '0', '1', NULL, '1')
Filename:
C:/xampp/htdocs/codeigniter/application/models/page_model.php Line
Number: 20
User_Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
// Check Login
if(!$this->session->userdata('logged_in')){
redirect('admin/login');
}
$data['users'] = $this->User_model->get_list();
// Load template
$this->template->load('admin', 'default', 'users/index', $data);
}
public function add(){
// Check Login
if(!$this->session->userdata('logged_in')){
redirect('admin/login');
}
$this->form_validation->set_rules('first_name','First Name','trim|required|min_length[2]');
$this->form_validation->set_rules('last_name','Last Name','trim|required|min_length[2]');
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
$this->form_validation->set_rules('email','Email','trim|required|min_length[7]|valid_email');
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]|matches[password2]');
$this->form_validation->set_rules('password2','Confirm Password','trim|required|min_length[6]|matches[password2]');
if ($this->form_validation->run() == FALSE){
// Load View Into Template
$this->template->load('admin','default','users/add');
} else {
// Create Page Data Array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
// Add User
$this->User_model->add($data);
//Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'user',
'action' => 'added',
'user_id' => $this->session->userdata('user_id'),
'message' => 'A new user was added ('.$data["username"].')'
);
// Add Activity
$this->Activity_model->add($data);
// Create Message
$this->session->set_flashdata('success', 'User has been added');
// Redirect to pages
redirect('admin/users');
}
}
public function edit($id){
// Check Login
if(!$this->session->userdata('logged_in')){
redirect('admin/login');
}
$this->form_validation->set_rules('first_name','First Name','trim|required|min_length[2]');
$this->form_validation->set_rules('last_name','Last Name','trim|required|min_length[2]');
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
$this->form_validation->set_rules('email','Email','trim|required|min_length[7]|valid_email');
if ($this->form_validation->run() == FALSE){
// Get Current Subject
$data['item'] = $this->User_model->get($id);
//Load View Into Template
$this->template->load('admin','default','users/edit', $data);
} else {
// Create User Data Array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'username' => $this->input->post('username')
);
// Update User
$this->User_model->update($id, $data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'user',
'action' => 'updated',
'user_id' => $this->session->userdata('user_id'),
'message' => 'A user was updated ('.$data["username"].')'
);
// Add Activity
$this->Activity_model->add($data);
//Create Message
$this->session->set_flashdata('success', 'User has been updated');
//Redirect to Users
redirect('admin/users');
}
}
public function delete($id){
// Check Login
if(!$this->session->userdata('logged_in')){
redirect('admin/login');
}
// Get Username
$username = $this->User_model->get($id)->username;
// Delete User
$this->User_model->delete($id);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'user',
'action' => 'deleted',
'user_id' => $this->session->userdata('user_id'),
'message' => 'A user was deleted'
);
// Add Activity
$this->Activity_model->add($data);
// Create Message
$this->session->set_flashdata('success', 'User has been deleted');
// Redirect to Subjects
redirect('admin/users');
}
public function login(){
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]');
if ($this->form_validation->run() == FALSE){
//Load View Into Template
$this->template->load('admin','login','users/login');
} else {
// Get Post Data
$username = $this->input->post('username');
$password = $this->input->post('password');
$enc_password = md5($password);
$user_id = $this->User_model->login($username, $enc_password);
if($user_id){
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
// Set Session Data
$this->session->set_userdata($user_data);
// Create Message
$this->session->set_flashdata('success', 'You are logged in');
// Redirect to pages
redirect('admin');
} else {
// Create Error
$this->session->set_flashdata('error', 'Invalid Login');
// Redirect to pages
redirect('admin/users/login');
}
}
}
public function register(){
$this->form_validation->set_rules('first_name','First Name','trim|required|min_length[2]');
$this->form_validation->set_rules('last_name','Last Name','trim|required|min_length[2]');
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
$this->form_validation->set_rules('email','Email','trim|required|min_length[7]|valid_email');
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]|matches[password2]');
$this->form_validation->set_rules('password2','Confirm Password','trim|required|min_length[6]|matches[password2]');
if ($this->form_validation->run() == FALSE){
// Load View Into Template
$this->template->load('admin','login','users/register');
} else {
// Create Page Data Array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
// Add User
$this->User_model->add($data);
//Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'user',
'action' => 'registered',
'user_id' => $this->session->userdata('username'),
'message' => 'A new user was registered ('.$data["username"].')'
);
// Add Activity
$this->Activity_model->add($data);
// Create Message
$this->session->set_flashdata('success', 'User has been registered');
// Redirect to pages
redirect('admin/users/login');
}
}
public function logout(){
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('username');
$this->session->sess_destroy();
// Message
$this->session->set_flashdata('success', 'You are logged out');
redirect(base_url());
}
}
Here is my user_model(in case you would like to check it):
<?php
class User_model extends CI_MODEL{
function __construct(){
parent::__construct();
$this->table = 'users';
}
public function get_list(){
$query = $this->db->get($this->table);
return $query->result();
}
public function get($id){
$this->db->where('id', $id);
$query = $this->db->get($this->table);
return $query->row();
}
public function add($data){
$this->db->insert($this->table, $data);
}
public function update($id, $data){
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
public function delete($id){
$this->db->where('id', $id);
$this->db->delete($this->table);
}
public function login($username, $password){
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->row()->id;
} else {
return false;
}
}
}
1/ You should write a private method for checking logged user like this
private function checkLogin()
{
if(!$this->session->userdata('logged_in')){
redirect('admin/login');
}
}
2/ You must make sure that you loaded the library session.

An uncaught Exception was encountered, Call to undefined method

I'm having this error and been trying to figure whats wrong for like 3 days straight with no luck:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined method Nsbh::post()
Filename: application\controllers\nsbh.php
Line Number: 25
nsbh.php
<?php
Class Nsbh extends CI_Controller{
var $API ="";
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->library('session');
$this->load->library('curl');
$this->load->helper('form');
$this->load->helper('url');
}
function index(){
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh'));
$this->load->view('nsbh/list',$data);
}
function create(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
$insert = $this->curl->simple_post($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($insert)
{
$this->session->set_flashdata('hasil','Insert Succeed');
}else
{
$this->session->set_flashdata('hasil','Insert Failed');
}
redirect('nsbh');
}else{
$this->load->view('nsbh/create');
}
}
function edit(){
if(isset($_POST['submit'])){
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
$update = $this->curl->simple_put($this->API.'/nsbh', $data, array(CURLOPT_BUFFERSIZE => 10));
if($update)
{
$this->session->set_flashdata('hasil','Update Succeed');
}else
{
$this->session->set_flashdata('hasil','Update Failed');
}
redirect('nsbh');
}else{
$params = array('id'=> $this->uri->segment(3));
$data['datansbh'] = json_decode($this->curl->simple_get($this->API.'/nsbh',$params));
$this->load->view('nsbh/edit',$data);
}
}
function delete($id){
if(empty($id)){
redirect('nsbh');
}else{
$delete = $this->curl->simple_delete($this->API.'/nsbh', array('id'=>$id), array(CURLOPT_BUFFERSIZE => 10));
if($delete)
{
$this->session->set_flashdata('hasil','Deleted');
}else
{
$this->session->set_flashdata('hasil','Failed');
}
redirect('nsbh');
}
}
}
And this is what's on line 25
'id'=> $this->post('id'),
You are missing input in 'id' => $this->post('id')
So, the right code is as follow:
'id' => $this->input->post('id')
You have to use input on all other places also.
change from
$data = array( 'id' => $this->post('id'),
'name' => $this->post('name'),
'location' => $this->post('location'),
'tgl' => $this->post('tgl'),
'addrs' => $this->post('addrs'));
to
$data = array('id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'location' => $this->input->post('location'),
'tgl' => $this->input->post('tgl'),
'addrs' => $this->input->post('addrs'));
As you already did in edit function
In the constructor add $this->load->library('form_validation');
So your constructor looks like this:
function __construct() {
parent::__construct();
$this->API="http://localhost/rest_ci/index.php";
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('session');
$this->load->library('curl');
$this->load->library('form_validation');
}

Converting Array to String Codeigniter 3.0

I am trying to insert an element into my 'category' table and use the inserted element's id to insert another element into my 'subcategory' table.
This is my code in my controller
public function insertCategory(){
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category_id = $this->admin_model->insertCategory($category);
$this->admin_model->insertSubcategory($category_id, $subcategory);
}
}
And this is my code in my model
function insertCategory($category){
$data = array(
'category' => $category,
'status' => 1
);
$this->db->insert('category', $data);
$this->db->select('id');
$this->db->from('category');
$this->db->where('category', $category);
$this->db->limit(1);
$query = $this->db->get();
return $query->result();
}
function insertSubcategory($category_id, $subcategory){
$data = array(
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
);
$this->db->insert('subcategory', $data);
}
However I am getting this error
I already tried using $category_id = (array) $this->admin_model->insertCategory($category); but it still doesn't work
How do I overcome this error? Thank you for the help.
$this->admin_model->insertSubcategory($category_id, $subcategory);
$category_id is an array , so , you must do it like this:
$this->admin_model->insertSubcategory($category_id[0]->field, $subcategory);
You should try this
$this->admin_model->insertSubcategory($category_id[0]->id;, $subcategory);
try this code
Model
public function insert($table, $data)
{
if($this->db->insert($table,array $data))
{
return $this->db->insert_id();
}else
return false
}
controller
public function insertCategory(){
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
//data must be an array
$data = [
'category' => $category,
'status' => '1'
];
$category_id = $this->admin_model->insert('tablename',$data)
if($category_id != false)
{
$data = [
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
];
if($this->admin_model->insert('table name', $data) != false)
{
echo 'success';
//or load your success page
}else{
echo 'failed';
//or load your success page
}
}else{
echo 'failed';
//or load your success page
}
}
}

couldn't insert data in codeigniter

i couldn't insert data to database. i don't know where the problem but when i var_dump($this->mberita->get_berita()); the result is array(0){}. I am a newbie in Codeigniter and couldn't really figure out how to solve this.
model
function get_berita()
{
$this->db->order_by('id_berita','asc');
$data = $this->db->get('berita_ukm');
return $data->result();
}
//untuk menambah berita
function insert_berita($data)
{
$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->db->insert('berita_ukm', $data);
}
function validate_berita()
{
$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == TRUE) {
return TRUE;
}
}
controller
function tambah_berita()
{
if ($this->mberita->validate_berita() == TRUE) {
$this->mberita->insert_berita();
redirect('admin/berita/tambah_berita');
}
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('admin/berita/tambah_berita', '', true);
$this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}
Please help me what to do. Thank you.
<?php
#in controller page#
// notes : you should make validation in controller page
if ($this->input->post('send')){ //request submit
// make form validation
$this->form_validation->set_rules('id_berita', 'Id Berita', 'required|numeric');
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == TRUE) {
// request data then put in array
$data = array(
'id_berita' => $this->input->post('id_berita'),
'tanggal' => $this->input->post('tanggal'),
'judul_berita' => $this->input->post('judul_berita'),
'content' => $this->input->post('content')
);
$this->mberita->insert_berita($data);
}
}
//model page
function insert_berita($data)
{
$this->db->insert('berita_ukm', $data);
}

Categories