Codeigniter not adding the Items to Cart - php

I'm trying to add items to the cart when i tried first time its worked but when i did the update function, the insert function not working, Can Anyone Help? thanks in advance
This is the code
public function addCart($id,$qty,$price,$name,$color,$cat)
{
$size=$this->input->post('size');
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'options' => array('Size' => $size, 'Color' => $color)
);
$res=$this->cart->insert($data);
if($res == true){
$this->session->set_flashdata('success',urldecode($name).' Added in Cart.');
redirect('product/view_all/'.$cat);
}
else{
$this->session->set_flashdata('success','Product Adding Failed :(');
redirect('product/view_all/'.$cat);
}
}
public function viewCart()
{
$data['title']="Products in Cart | DreamShopie.in";
$this->load->view('templates/header',$data);
$this->load->view('templates/menu');
$this->load->view('main_page/cartView',$data);
$this->load->view('templates/footer');
}
public function removeCart($id)
{
$data = array(
'rowid' => $id,
'qty' => 0
);
$this->cart->update($data);
$this->session->set_flashdata('success', 'Item Removed');
redirect('cart/viewCart');
}

Assuming $this->cart is your db , didnt you miss db name?
$this->cart->insert('db_name', $data);

insert() and update() function in the cart model should be like this
public function insert($data){
return $this->db->insert('table', $data);
}
public function update($data){
$this->db->where('column', $value);
$this->db->set($data);
return $this->db->update('table');
}

I own find the answer , the errorr occurred because the name contains illegal characters from the URL , so I use urldecode function for name
Problem solved.

Related

Not working update using ajax with codeigniter

When I update one column, that changes all the columns.
How can I fix it?
Model
public function update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
Controller:
public function ajax_update()
{
$data = array(
'book_title' => $this->input->post('book_title'),
'book_isbn' => $this->input->post('book_isbn'),
'book_yop' => $this->input->post('book_yop'),
'book_active' => $this->input->post('book_active'),
'publisher_name' => $this->input->post('publisher_name'),
'author_name' => $this->input->post('author_name'),
);
$this->user_model->update( $this->input->post('book_id'), $data);
echo json_encode(array("status" => TRUE));
}
Make sure all of your data are ok and especially your id then update like this:
$this->db->where('book_id', $this->input->post('book_id'));
$this->db->update($this->table, $data);
Based on CodeIgniter documentation I would suggest changing the code that defines the 'where' part:
$this->user_model->update( array('book_id' => $this->input->post('book_id')), $data);

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

last inserted id in Php codeIgniter

In my model I have return below function for get record id
function getLastInserted()
{
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration");
return $query->result();
}
Now I want to pass that ID to mycontroller for record insertion
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
Now I want to access model function in controller and pass record id in data function How I do ??
set return in model and in controller write
$insert_id=$this->db->insert_id();
In Controller load your model first using
$this->load->model('model_name');
Then call to model's getLastInserted() function.
$ID = $this->model_name->getLastInserted();//you will get id here
In model return $query->row()->userid; insead return of $query->result().
Then modify the controller:
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $this->model_name->getLastInserted(),
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
hi i worked out the solution on last inserted id dear here the code:
controller:
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
model:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
model:
public function newregistration($sid)
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $sid,
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}

How to load all rows in codeigniter-base-model? REST api

I am trying to load all rows for my REST API through Postman.
I am using codeigniter-base-model MY_Model.php.
https://github.com/jamierumbelow/codeigniter-base-model
This is how my code currently looks like both in my controller/model:
Controller(api_news.php):
class Api_News extends REST_Controller {
function __construct()
{
parent::__construct();
}
function index_get()
{
$id = $this->uri->segment(3);
$this->load->model('News_model');
$news = $this->News_model->get_by(array('id' => $id));
if(isset($news['id'])) {
$this->response(array(
'message' => 'success',
'status' => 'true',
'data' => $news));
} else {
$this->response(array(
'message' => 'unsuccess',
'status' => 'false'));
}
}
}
Model(news_model.php):
class News_model extends MY_Model{
protected $_table = 'news';
protected $primary_key = 'id';
protected $return_type = 'array';
}
At the moment if I access:
localhost/my_api/api_news/id/1, 2, 3, etc...
I can access any record by its individual ID and it shows up which is great.
BUT I also want to be able to see all rows by doing this:
localhost/my_api/api_news/id/
and have all rows showing at once.
But I am not sure how to do this...and am getting an unsuccess/false if I try.
Can you please show me how? I am new to PHP in general and I appreciate any help.
Thank you so much!!
Make some changes in your Controller function as below -
function index_get(){
$id = $this->uri->segment(3);
$this->load->model('News_model');
// pass $id to model
$news = $this->News_model->get_by( $id );
if( !empty( $news ) ) {
$this->response(array(
'message' => 'success',
'status' => 'true',
'data' => $news));
} else {
$this->response(array(
'message' => 'unsuccess',
'status' => 'false'));
}
}
And in your model make id parameter optional and then check that if id is passed get data based on id otherwise return all data as below -
// $id variable is optional here
function get_by( $id = '' ) {
if ( $id == '' ) {
$news = $this->db->get( 'news' );
}
else {
$news = $this->db->get_where( 'news', array( 'id' => $id ) );
}
// return data to controller
return $news->result();
}
So if you enter id in API then data will be based on that id otherwise all data will be returned.

How to insert multiple data using insert_batch in codeigniter

Here i am getting inserted data to database but how to write this in foreach loop to get multiple data please help me as a fresher am totally confused..
My controller
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
//create array for get data from index
//$data=array(
// 'studentname' => $this->input->post('studentname'),
//'gender' => $this->input->post('gender'),
//'phone' => $this->input->post('phone')
// );
$data = array(
array(
'studentname' => 'Reddy' ,
'gender' => 'Male' ,
'phone' => '456879'
),
array(
'studentname' => 'Yalla' ,
'gender' => 'Female' ,
'phone' => '12345678'
)
);
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$id=$this->input->post('id');
$data=array(
'studentname' => $this->input->post('studentname'),
'gender' => $this->input->post('gender'),
'phone' => $this->input->post('phone')
);
$this->db->where('id',$id);
$this->db->update('tblstudent',$data);
redirect("Student/index");
}
function delete($id)
{
$id=$this->db->where('id',$id);
$this->db->delete('tblstudent');
redirect("Student/index");
}
}
My model
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
}
For CodeIgniter 3.x: insert_batch
For CodeIgniter 2.x: insert_batch

Categories