• 05-02-2024, 16:53:48
    #1
    bir yardım rica ediyorum
    burayada sürekli yardım konusu açıyorum kusura bakmayın
    sessionda vaar ıolan ürünleri listesini $products tutuyorum. post edilen $product_id1 = $_POST["product_id"]; değişkeni array_key_exists aracılığı ile var ise
    unset($products[$product_id1 ]) ürün idsini ve ürünü silemedim.

    DİZİ YAPISIDA
    /*
    session
    shoppingCart
    products
    kedi maması ...2... 100.....200
    köpek maması ...2....1000....2000
    summary
    adet ürün 400
    //////////////////////////////////////////

    function removeFromCart($product_id){
    //include("ust.php");
    
    session_start();
    
    
    //echo"removeFromCart";
    echo $product_id1 = $_POST["product_id"];
    $shoppingCart = $_SESSION["shoppingCart"];
    $products = $shoppingCart["products"];
    
    echo "<pre>";
    // print_r($_SESSION);
    print($products[$product_id1]['id']);
    echo "</pre>";/**/
    
    
    if (array_key_exists($product_id1, $products)) {
    // Ürün zaten varsa, adedini artır
    // $products[$product_id_key]['a-siparis'] += $siparis_adet;
    // echo '<strong style="color:red;">Ürün adedi artırıldı</strong>';
    unset($products[$product_id1 ]) ;
    // print_r($products[1]);
    /*
    unset($products[$product_id1 ]);*/
    
    } else {echo 'malisef';}
    
    
    //unset ($shopping_products[$product_id1]);
    //session_destroy();
    
    // include("alt.php");
    }
  • 05-02-2024, 17:51:13
    #2
    güzel kalpli emekci kardeeşlerimden yardım bekliyorum
  • 06-02-2024, 12:55:03
    #3
    function removeFromCart($product_id){
        //include("ust.php");
    
        session_start();
    
        //echo"removeFromCart";
    
        // product_id kontrolü
        if(isset($_POST["product_id"])){
            $product_id1 = $_POST["product_id"];
        } else {
            echo "Ürün ID belirtilmedi.";
            return;
        }
    
        if(isset($_SESSION["shoppingCart"]) && isset($_SESSION["shoppingCart"]["products"])){
            $shoppingCart = $_SESSION["shoppingCart"];
            $products = $shoppingCart["products"];
    
            echo "<pre>";
            // print_r($_SESSION);
            print($products[$product_id1]['id']);
            echo "</pre>";/**/
    
            if (array_key_exists($product_id1, $products)) {
                // Ürün zaten varsa, adedini artır
                // $products[$product_id_key]['a-siparis'] += $siparis_adet;
                // echo '<strong style="color:red;">Ürün adedi artırıldı</strong>';
                unset($products[$product_id1]);
                // print_r($products[1]);
                /*
                unset($products[$product_id1 ]);*/
    
            } else {
                echo 'Ürün bulunamadı.';
            }
        } else {
            echo 'Sepetinizde ürün bulunmamaktadır.';
        }
    
        //unset ($shopping_products[$product_id1]);
        //session_destroy();
    
        // include("alt.php");
    }
  • 06-02-2024, 13:43:09
    #4
    hocam çok teşekkürler ilginlendiğin için şimdi silme tamam ama sepetin toplam değeri eski toplam fiyat kaliyor.
    silinen ürünü sepet toplamındann da silebilirmisiniz.
    total_counta güncellenmiyor




    function removeFromcart($product_id) {
    session_start();
    $product_id=$_POST['product_id'];
    if(isset($_SESSION["shoppingCart"])){
    $shoppingCart = $_SESSION["shoppingCart"];
    $products = $shoppingCart["products"];
    //urunu listeden çıkar
    if(array_key_exists($product_id, $products)){
    unset($_SESSION['shoppingCart']['products'][$product_id]);
    echo "Bu ürünü septten çıkardınız";
    }//array_key_exists
    //tekrardan sepete hesapla
    
    
    $total_price =0.0; $total_count=0;
    
    // $product_id_key = $product_id ;
    foreach($products as $key => $product ){
    
    
    $urun_takim_fiyat = $product['a-siparis'] * $product["satis_fiyat"];
    $products[$key]['total_price'] = $urun_takim_fiyat; //products diziye ekle
    
    $total_count += $product['a-siparis'];
    $total_price += $urun_takim_fiyat;
    
    $summary["total_count"]=$total_count;
    $summary["total_price"]=$total_price;
    // $_SESSION["shoppingCart"]["products"] = $products;
    $_SESSION["shoppingCart"]["summary"]= $summary;
    
    
    
    }
    $guncellenenfiyat["Uretim_Adedi"]=$total_count;
    $guncellenenfiyat["Sepet_Toplam_Fiyat"]=$total_price;
    $_SESSION["shoppingCart"]["guncellenenfiyat"]= $guncellenenfiyat;
    return $total_count;
    
    
    } //if
    }//func
  • 07-02-2024, 10:24:04
    #5
    unset ile silersen array index yapin yeniden sıralanmaz, array_splice ile silmen gerekiyor.
    örneğin elinde $cars = array("Volvo", "BMW", "Toyota"); şeklinde bir array varsa normalde bu arrayin çıktısı Array ([0] => "Volvo" [1] => "BMW" [2] => "Toyota ") şeklinde 0,1,2. indexler ile olacaktır.
    Eğer unset kullanırsan ve örneğin unset(1) dersen array çıktısı şu şekilde olacak : Array ([0] => "Volvo" [2] => "Toyota ") yani 1. index yok olmuş oldu. Dolayısıyla loop döndürüyorsan hesaplama sorunu oluşacaktır.
    aynı işlem için array_splice kullanırsan array çıktısı Array ([0] => "Volvo" [1] => "Toyota ") olacak ve array yeniden oluşturulup indexler düzenlenecektir.

    Eğer array yapında fiyat ile siparis id aynı index altında değilse ayrı ayrı silmen gerekiyor.
  • 07-02-2024, 16:04:45
    #6
    kaaNx adlı üyeden alıntı: mesajı görüntüle
    unset ile silersen array index yapin yeniden sıralanmaz, array_splice ile silmen gerekiyor.
    örneğin elinde $cars = array("Volvo", "BMW", "Toyota"); şeklinde bir array varsa normalde bu arrayin çıktısı Array ([0] => "Volvo" [1] => "BMW" [2] => "Toyota ") şeklinde 0,1,2. indexler ile olacaktır.
    Eğer unset kullanırsan ve örneğin unset(1) dersen array çıktısı şu şekilde olacak : Array ([0] => "Volvo" [2] => "Toyota ") yani 1. index yok olmuş oldu. Dolayısıyla loop döndürüyorsan hesaplama sorunu oluşacaktır.
    aynı işlem için array_splice kullanırsan array çıktısı Array ([0] => "Volvo" [1] => "Toyota ") olacak ve array yeniden oluşturulup indexler düzenlenecektir.

    Eğer array yapında fiyat ile siparis id aynı index altında değilse ayrı ayrı silmen gerekiyor.
    ozaman ben
    product_item[$product_id] sildiğimde
    summary[total_price] silmem gerekiyor
    doğru mu?
  • 07-02-2024, 17:45:07
    #7
    product_item[$product_id] içerisinde total priceı barındırmıyorsa aynen onuda ayrıca silmen gerekir. Bu tür durumlara düşmemek için bir tane class oluşturup arayin içinde obje tutabilirsin.