skip to Main Content

I have online shop project, when someone click “purchase” -> he buy item.
Now, i save the item (it’s work) and want to update my Database (need to update the amount of those item’s) from the cart.

Controller – look at function insertOrderDataController and indise it the line
$updateCount = $this->product_model->updateItemCount($data,$counter);

<?php

class webs_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('login_model');
        $this->load->model('static_model');
        $this->load->model('product_model');
        $this->load->helper('url_helper');
        $this->load->helper('form');
        $this->load->library('session');
    }

    public function view($page = 'home') {
        if (!file_exists(APPPATH . 'views/pages/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            show_404();
        }
        $data['title'] = ucfirst($page); // Capitalize the first letter
        $this->load->view('templates/header', $data);
        $this->load->view('pages/' . $page, $data);
        $this->load->view('templates/footer', $data);
    }

    public function home() {
        $data['user'] = $this->session->all_userdata();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/home', $data);
        $this->load->view('templates/footer');
    }

    public function products() {
        $data['user'] = $this->session->all_userdata();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/our_products', $data);
        $this->load->view('templates/footer');
    }

    public function contact() {
        $data['user'] = $this->session->all_userdata();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/contact', $data);
        $this->load->view('templates/footer');
    }

    public function our_products() {

        $data['title'] = 'Produts';
        $data['user'] = $this->session->all_userdata();
        $totalBill['total'] = $this->product_model->CalculateTotalBill($data);
        $data['product'] = $this->product_model->get_product();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/our_products', $data);
        $this->load->view('templates/footer');
    }

    public function calcit() {
        $data['user'] = $this->session->all_userdata();
        $data['calc'] = $this->product_model->CalculateTotalBill($data);
    }

    public function google_pie_chart() {
        $data['title'] = 'statistic view';
        $data['user'] = $this->session->all_userdata();
        $data['year_pie'] = $this->static_model->get_Stat();
        $this->load->view('templates/header', $data);
        $this->load->view('pages/google_pie_chart', $data);
        $this->load->view('templates/footer');
    }

    public function check($data, $counterCart) {

        $error = '';
        $data['productDB'] = $this->product_model->get_product();
        $lengthDB = count($data['productDB']);

        for ($i = 0; $i < $counterCart; $i++) {
            $serialCartItem = intval($data[$i]['serial_number']);
            $countCartItem = intval($data[$i]['count_purchase']);

            for ($y = 0; $y < $lengthDB; $y++) {
                $serialDBItem = intval($data['productDB'][$y]['serial_number']);
                $countDBItem = intval($data['productDB'][$y]['item_count']);

                if ($serialDBItem == $serialCartItem) {

                    if ($countDBItem <= $countCartItem) {
                        $error.= "Not enough of this product in stock ! You will move to Homepage.";
                    }
                }
            }
        }
        return $error;
    }

    public function insertOrderDataController() {
        $counter = $this->input->post('counter');
        $dataUser['user'] = $this->session->all_userdata();
        $TheUsername = $dataUser['user']['User_name'];

        for ($i = 0; $i < $counter; $i++) {
            $data[$i] = array(
                'User_name' => $TheUsername,
                'serial_number' => $this->input->post('serial_number[' . $i . ']'),
                'count_purchase' => $this->input->post('count_purchase[' . $i . ']')
            );
        }

        $error = $this->check($data, $counter);
        if ($error == '') {
            $error_db = $this->product_model->insertOrderData($data);
            if ($error_db == NULL) {
                $updateCount = $this->product_model->updateItemCount($data,$counter);
                $data['info'] = array("message" => "1");
                $messageAlert = 'Thanks you for purchase !';
                echo "<script type='text/javascript'>alert('$messageAlert');</script>";
//                $updateCount = $this->product_model->updateItemCount($data,$counter);
            } else {
                $data['info'] = array("message" => "Error. Registration faild: " . $error_db["message"]);
            }
        } else {
            echo "<script type='text/javascript'>alert('$error');</script>";
        }
        redirect("webs_controller/home");  //if activ - there is not alert.
    }

}

model –

<?php

class product_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function get_product() {
        $query = $this->db->query('SELECT * FROM `product`');
        return $query->result_array();
    }

    public function CalculateTotalBill($username) {
        $user= $username['user']['User_name'];
        $query = $this->db->query("SELECT bought_history.serial_number, bought_history.count_purchase, product.item_price FROM bought_history INNER JOIN product ON bought_history.serial_number=product.serial_number WHERE User_name IN (select User_name FROM bought_history WHERE User_name='$user')");
        $temp = $query->result_array();
        $total = 0;
        for ($i = 0; $i < count($temp); $i++) {
            $total = $total + ($temp[$i]['count_purchase'] * $temp[$i]['item_price']);
        }
        echo "The current total pay : <b>",$total,"$</b>";
        return $total;
    }

    public function updateItemCount($data,$counterCart){
       $data['productDB'] = $this->product_model->get_product();
        $lengthDB = count($data['productDB']);

        for ($i = 0; $i < $counterCart; $i++) {
            $serialCartItem = intval($data[$i]['serial_number']);
            $countCartItem = intval($data[$i]['count_purchase']);

            for ($y = 0; $y < $lengthDB; $y++) {
                $serialDBItem = intval($data['productDB'][$y]['serial_number']);
                $countDBItem = intval($data['productDB'][$y]['item_count']);

                if ($serialDBItem == $serialCartItem) {
                    $updatedCalc = $countDBItem-$countCartItem;
                    $query = $this->db->query('UPDATE `product` SET `item_count` = "'.$updatedCalc.'" WHERE `product`.`serial_number` = "'.$serialDBItem.'"');
                    $update = $this->db->mysql_query($query);
                    }
                }
            }
        }


     public function insertOrderData($data){ 
            $this->db->db_debug = FALSE; 

             $error=NULL;
              if (!$this->db->insert_batch('bought_history', $data)){
                  $error=$this->db->error();
              }
              return $error;
        }

}

Database – need to be change : item_count

enter image description here

I succsess update the item when i insert only one item to cart, but i get error :
enter image description here

when i try update more then one item, only the first item update, and i get the same error .

Thanks, Idan.

2

Answers


  1. Remove the line

    $update = $this->db->mysql_query($query);
    

    There is no Codeigniter function like mysql_query, why are you using this?

    Login or Signup to reply.
  2. There is no mysql_query method on Codeigniter. You could remove it and update the model codes like this :

        public function updateItemCount($data,$counterCart){
            $data['productDB'] = $this->product_model->get_product();
            $lengthDB = count($data['productDB']);
    
            for ($i = 0; $i < $counterCart; $i++) {
                $serialCartItem = intval($data[$i]['serial_number']);
                $countCartItem = intval($data[$i]['count_purchase']);
    
                for ($y = 0; $y < $lengthDB; $y++) {
                    $serialDBItem = intval($data['productDB'][$y]['serial_number']);
                    $countDBItem = intval($data['productDB'][$y]['item_count']);
    
                    if ($serialDBItem == $serialCartItem) {
                        $updatedCalc = $countDBItem-$countCartItem;
                        $this->db->query('UPDATE `product` SET `item_count` = "'.$updatedCalc.'" WHERE `product`.`serial_number` = "'.$serialDBItem.'"');
                    }
                }
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search