What's new

PHP Patulong po sa pag-add ng quantity sa cart page

0x0adr

Honorary Poster
Joined
Mar 18, 2017
Posts
340
Reaction
304
Points
179
Age
25
patulong po. eto nalang po talaga yung hindi ko gaanong magets eh.
yung scenario po is kelangan may quantity selector yung cart page.
hindi ko maimplement due to limited time na binigay samin ng prof namin
refer to the screenshots below

eto po yung page:
Screenshot 2021-04-18 14.30.04.png


eto po yung code na nagmamanage sa add item
[CODE lang="php" title="index.php"]<?php
session_start();

require_once ('php/CreateDb.php');
require_once ('php/component.php');

// create instance of Createdb class
$database = new CreateDb("Productdb", "Producttb");

// var
$quantity = 0;

if (isset($_POST['add'])){
if(isset($_SESSION['cart'])){

$item_array_id = array_column($_SESSION['cart'], "product_id");
if(in_array($_POST['product_id'], $item_array_id)){

} else {
$count = count($_SESSION['cart']);
$item_array = array(
'product_id' => $_POST['product_id']
);
$_SESSION['cart'][$count] = $item_array;
}
}else{

$item_array = array(
'product_id' => $_POST['product_id']
);

$_SESSION['cart'][0] = $item_array;
}
}
?>[/CODE]

paano po kaya malalagyan ng proper quantity field yan ?

eto po yung ginamit kong base: You do not have permission to view the full content of this post. Log in or register now.
 

Attachments

dun sa

if(in_array($_POST['product_id'], $item_array_id)){


set ka ng session product id quantity

pero kung kaya mo baguhin

dapat gumamit ka ng array + json

para ayun yung ilagay mo sa session

para ganto kalabasan

array(
{'id':000,'quantity':2},
{'id':111,'quantity':1},
{'id':222,'quantity':2},
)

tas gawa ka function na incart($id)

tas gagawin nun mag loop sya sa value ng session

parang ganto

foreach($_SESSION['cart'] as $i)//kunwari laman nito ay array({'id':000,'quantity':2},{'id':111,'quantity':1},{'id':222,'quantity':2},)

$json = json_decode($i);
if($i->id == $id) {
return true
}else{
return false
}

tas pag true gawin mo nalang add 1 sa quantity
 
Extermin4tor

try mo to explore sana makatulong

PHP:
<?php
session_start();

$productId = 2;
$quantity = 3;// ito yung i add na quantity



//========================================================
// FUNCTION start
//========================================================
function product_exist($id){
    $cart = $_SESSION['cart'];
    foreach($cart as $i){
        $json = json_decode($i);
        if($json->productId == $id){
            return true;
            break;
        }
    }
    return false;
}
function add_quantity($id, $quantity){
    $cart = $_SESSION['cart'];
    $x =0;
    foreach($cart as $i){
        $json = json_decode($i);
        if($json->productId == $id){
            $oldQuantity = $json->quantity;
            break;
        }
        $x+=1;
    }
    $newQuantity = $oldQuantity + $quantity;
    $obj['productId'] = $id;
    $obj['quantity'] = $newQuantity;
    $json = json_encode($obj);
    $cart[$x] = $json;
    $_SESSION['cart'] = $cart;
}
//========================================================
// FUNCTION end
//========================================================



if(!isset($_SESSION['cart'])){//if walang session add natin yung product
    $obj['productId'] = $productId;
    $obj['quantity'] = $quantity;
    $json = json_encode($obj);
    $_SESSION['cart'] = array($json);//set session
}else{//kung may product na sa session
    if(product_exist($productId)){//if yung product ay nag exist na add nalang sa quantity
        add_quantity($productId,$quantity);
    }else{//kung yung product ay wala pa sa session append natin yung id sa session
        $obj['productId'] = $productId;
        $obj['quantity'] = $quantity;
        $json = json_encode($obj);
        $cart = $_SESSION['cart'];
        array_push($cart,$json);
        $_SESSION['cart'] = $cart;
    }
}
$cart = $_SESSION['cart'];
foreach($cart as $i){
    $json = json_decode($i);
    echo $json->productId.' '.$json->quantity;
}
 

Similar threads

Back
Top