Converting Array to String Codeigniter 3.0 - php

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
}
}
}

Related

unable to access value from data array

i want to access unit_id from get_material() method which return data from the modal product_modal.. The function codes..
public function get_material() {
$query = $this->db->get('tbl_receivings_items');
return $query->result();
}
The second function which I want to use unit_id from the above function is
public function get_material_item_unit($p_unit_id){
$sql = "SELECT * FROM tbl_unit WHERE unit_id = ?";
$q = $this->db->query($sql, $p_unit_id);
if($q->num_rows() >0){
return $q->result();
}else{
return null;
}
}
I access both functions from the controller known as manufacture **, in which both functions have been called inside **add_product() function as follows
public function add_product(){
$formRules = array(
array(
'field' => 'prod_name',
'label' => 'Product Name',
'rules' => 'trim|required',
),array(
'field' => 'partner',
'label' => '',
'rules' => 'trim',
),array(
'field' => 'prod_qty',
'label' => 'Quantity To Produce',
'rules' => 'trim|required|greater_than_equal_to[1]',
)
);
$this->form_validation->set_rules($formRules);
if($this->form_validation->run() == TRUE){
$can_be_consumed_value = $this->input->post('can_be_consumed');
$can_be_solded_value = $this->input->post('can_be_sold');
if($can_be_consumed_value == false){
$can_be_consumed_value ="0";
}else{
$can_be_consumed_value ="1";
}
if($can_be_solded_value == false){
$can_be_solded_value ="0";
}else{
$can_be_solded_value ="1";
}
$data =array(
'prod_name' => $this->input->post('prod_name'),
'can_be_consumed' => $can_be_consumed_value,
'can_be_sold' => $can_be_solded_value,
'prod_partner' => $this->input->post('partner'),
'created_by' => $this->input->post('created_by'),
'date_created' => date('Y-m-d'),
'prod_desc' => $this->input->post('prod_desc'),
'prod_qty' =>$this->input->post('prod_qty')
);
$query = $this->product_model->add_product($data);
if($query){
$data['initial_data'] = $this->product_model->get_product_data($query);
$data['material_list'] = $this->product_model->get_material();
$m_list = $this->product_model->get_material();
if($m_list->num_rows() > 0)
{
foreach($m_list->result() as $m){
$data['unit_list'] = $this->product_model->get_material_item_unit($m->unit_id);
}
}
$this->session->set_flashdata('success_msg', 'Product Created Successfully');
$this->load->view('manufacture/manufacture_step_2', $data,'refresh');
}else{
$this->session->set_flashdata('error_msg', 'Sorry! Fail to create product');
redirect('manufacture');
}
}else{
echo '<div class="alert alert-danger error">'.validation_errors().'</div>';
}
}
The add_product() function works properly means that it send data to db.
My Problem: every time I try to access the unit_id through a while loop I get an error of Undefined property: stdClass::$unit_id. i will appreciate your help.
the while loop
$data['initial_data'] = $this->product_model->get_product_data($query);
$data['material_list'] = $this->product_model->get_material();
$m_list = $this->product_model->get_material();
if($m_list->num_rows() > 0)
{
foreach($m_list->result() as $m){
$data['unit_list'] = $this->product_model->get_material_item_unit($m->unit_id);
}
}
I don't know what is the SQL extension that you are using in PHP but normally when you get the results of a query these results are an array of values.
You are trying to get the data like an object and this is the reason that PHP shows you this error because the var $m is not an object.
You can try to do a var_dump($m) and you can see what is the type and the structure of the variable. If $m is an array you must use $m['columnName'] to get the value.

I can't update data in a database Codeigniter

I can't update data in a database.i don't know what happend.please help me. Display data Press the edit button.But i can not edit data on the database.
I have posted the code below:
Controller.php
public function ajax_update()
{
$data = array(
'placename' => $this->input->post('placename'),
'description' => $this->input->post('description'),
'tel' => $this->input->post('tel'),
'latitude' => $this->input->post('latitude'),
'longtitude' => $this->input->post('longtitude'),
'placetype_id' => $this->input->post('placetype_id'),
'province_id' => $this->input->post('province_id'),
);
$this->tblplace->update(array('placeid' => $this->input->post('placeid')), $data);
echo json_encode(array("status" => TRUE));
}
Modal.php
public function save($data)
{
$this->db->insert('tblplace', $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update('tblplace', $data, $where);
return $this->db->affected_rows();
}
I can get data from the database to show in the textbox. But can't update data.I need to help.Thank you.
Your Controller:
public function ajax_update()
{
//You should also check the post array.
$ajax_post_data = $this->input->post();
log_message('debug', 'Ajax Post Data Array:' . print_r($ajax_post_data, TRUE));
$placeid = $this->input->post('placeid');
$data = array(
'placename' => $this->input->post('placename'),
'description' => $this->input->post('description'),
'tel' => $this->input->post('tel'),
'latitude' => $this->input->post('latitude'),
'longtitude' => $this->input->post('longtitude'),
'placetype_id' => $this->input->post('placetype_id'),
'province_id' => $this->input->post('province_id'),
);
$this->load->model('tblplace');
$updte_status = $this->tblplace->update($placeid, $data);
if($updte_status == TRUE){
echo json_encode(array("status" => true));
} else {
echo json_encode(array("status" => false));
}
}
In Model:
public function update($where, $data)
{
$this->db->where('id', $where)
->update('tblplace', $data);
log_message('debug', 'Last Update Query:' . print_r($this->db->last_query(), TRUE));
log_message('debug', 'Affected row count:' . print_r(count($this->db->affected_rows()), TRUE));
if ($this->db->affected_rows() === 1) {
return TRUE;
}else{
return FALSE;
}
}
Hope this is clear and it will help you to update the data.
There is no where condition in your update function
public function update($where, $data)
{
$this->db->where($where);
$this->db->update('tblplace', $data);
return $this->db->affected_rows();
}

Undefined method in Code Igniter framework

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

Using $insert_id to update two tables - codeigniter

Is there a way that I can update a form input using something like insert_id?
I have a form for adding factories to a table and I use the insert_id to store the factories id in another table too after submit.
this all works great, but is there such a way for $this->db->update?
My model for updating:
function updatebedrijf($id, $data)
{
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijven', $data);
}
My controller function for updating:
function updatebedrijven()
{
$dbres = $this->db->get('categorieen');
$ddmenu = array();
foreach ($dbres->result_array() as $tablerow) {
$ddmenu[$tablerow['idcategorieen']] = $tablerow['Categorie'];
}
$data['opties'] = $ddmenu;
$data['info'] = $this->members_model->getbedrijf($id);
$data['id'] = $this->uri->segment(3);
$this->load->view('members/header');
$this->load->view('members/editform', $data);
$this->load->view('members/footer');
}
for adding factories i use this with insert_id:
controller:
function addbedrijven()
{
$this->members_model->addbedrijf();
redirect('members/index');
}
model:
function addbedrijf()
{
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $this->input->post('logo')
);
$this->db->insert('bedrijven',$data1);
if($this->db->affected_rows() >= 1)
{
$to_bedrijfcategorieen['idbedrijven'] = $this->db->insert_id();
$to_bedrijfcategorieen['idcategorieen'] = $this->input->post('categorieen');
$this->insert_bedrijfcat($to_bedrijfcategorieen);
}else{
return FALSE;
}
}
function insert_bedrijfcat($data1)
{
$this->db->insert('bedrijfcategorieen', $data1);
return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
}
Hope it's clear enough.

Edit Page -> Loading A Blank White Page

I have an issue when I click on a link to edit a sale http://domain/admin/editsale/index/21/sale-name I seem to get a blank page loaded so I have a feeling that it is not getting the $id but I cannot spot my issue.
Controller:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Editsale extends CI_Controller {
function __construct() {
parent::__construct();
}
function index($id) {
if(!$this->session->userdata('logged_in'))redirect('admin/home');
if($this->input->post('submit')) {
#Set The Validation Rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim');
$this->form_validation->set_rules('condition', 'Condition', 'trim');
$this->form_validation->set_rules('description', 'Description', 'trim');
$this->form_validation->set_rules('price', 'Price', 'trim');
if($this->form_validation->run() == FALSE) {
#Set the $data for the view if FALSE
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
$data['sales_pages'] = $this->sales_model->getSalesPages($id);
$data['sale'] = $this->sales_model->getSalesContent($id);
$data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
$this->load->view('admintemplate', $data); #Loads the given template and passes the $data['content'] into it
}
#Form Validation Was Correct So Lets Continue
#Lets Set What We Are Sending To The DB
$content = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE)
);
if($this->sales_model->updateSale($id, $content)) {
$data['success'] = TRUE; #displays sale updated
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
$data['sales_pages'] = $this->sales_model->getSalesPages($id);
$data['sale'] = $this->sales_model->getSalesContent($id);
$data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
} // Sale Update End
}else{
$data['cms_pages'] = $this->navigation_model->getCMSPages($id);
$data['sales_pages'] = $this->sales_model->getSalesPages($id);
$data['sale'] = $this->sales_model->getSalesContent($id);
$data['content'] = $this->load->view('admin/editsale', $data, TRUE); #Loads the "content"
}#Submit End
} #Index End
}
Model:
function getSalesPages($id = NULL) {
$query = $this->db->get('sales');
if($query->num_rows() > 0) return $query->result();
}
function getSalesContent($id = NULL) {
$this->db->where('id', $id);
$query = $this->db->get('sales', 1);
if($query->num_rows() > 0) {
$row = $query->result_array();
return $row;
}else{
return FALSE;
}
}
View:
<?php
//Setting form attributes
$formEditSale = array('id' => 'editSale', 'name' => 'editSale');
$formName = array('id' => 'name', 'name' => 'name');
$formLocation = array('id' => 'location', 'name' => 'location');
$formBedrooms = array('id' => 'bedrooms','name' => 'bedrooms');
$formBathrooms = array('id' => 'bathrooms','name' => 'bathrooms');
$formCondition = array('id' => 'condition','name' => 'condition');
$formDescription = array('id' => 'description','name' => 'description');
$formPrice = array('id' => 'price','name' => 'price');
if($success == TRUE) {
echo '<section id = "validation">Sale Updated</section>';
}
?>
?>
<section id = "validation"><?php echo validation_errors();?></section>
<?php
echo form_open_multipart('admin/editsale/index/'.$sale[0]['id'].'/'.url_title($sale[0]['name'],'dash', TRUE),$formEditSale);
echo form_fieldset();
echo form_label('Name:', 'name');
echo form_input($formName, $sale[0]['name']);
echo form_label ('Location', 'location');
echo form_input($formLocation, $sale[0]['location']);
echo form_label ('Bedrooms', 'bedrooms');
echo form_input($formBedrooms, $sale[0]['bedrooms']);
echo form_label ('Bathrooms', 'bathrooms');
echo form_input($formBathrooms, $sale[0]['bathrooms']);
echo form_label ('Condition', 'condition');
echo form_input($formCondition, $sale[0]['condition']);
echo form_label ('Price', 'price');
echo form_input($formPrice, $sale[0]['sale']);
echo form_label ('Description', 'description');
echo form_textarea($formDescription, $sale[0]['description']);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
Fixed, I was loading the view before the data

Categories