andreiviziru 0 Junior Poster in Training

this is the code that works

if (isset($_POST['add_to_cart'])) {
                   if (isset($_POST['select_size']) && isset($_POST['select_color']) && isset($_POST['select_quantity'])) {
                       $size = $_POST['select_size'];
                       $color = $_POST['select_color'];
                       $quantity = $_POST['select_quantity'];

                       if (isset($_GET['lang'])) {
                           $the_lang = mysqli_real_escape_string($connection, $_GET['lang']);

                               if ($the_lang == 'ro') {
                                   $product_name = $product_name_ro;
                               } else if ($the_lang == 'en') {
                                   $product_name = $product_name_en;
                               } else if ($the_lang == 'fr') {
                                   $product_name = $product_name_fr;
                               }
                       } else {
                           $product_name = $product_name_ro;
                       }

                       if (isset($_GET['coin'])) {
                           $the_coin = mysqli_real_escape_string($connection, $_GET['coin']);

                           if ($the_coin == 'ron') {
                               $price = $product_price_ron;
                               $the_coin = 'RON';
                           } else if ($the_coin == 'dollar') {
                               $price = $product_price_dollar;
                               $the_coin = '$';
                           } else if ($the_coin == 'euro') {
                               $price = $product_price_euro;
                               $the_coin = '€';
                           }
                       } else {
                           $price = $product_price_ron;
                           $the_coin = 'RON';
                       }
                    }

                    $total_price = $price * $quantity;

                    $_SESSION['cart'][$product_id] = array('product_name' => $product_name, 'coin' => $the_coin, 'price' => $price, 'total_price' => $total_price, 'size' => $size, 'color' => $color, 'quantity' => $quantity);

                    $temp = $_SESSION['cart'];
                    array_push($_SESSION['cart'][$product_id], $temp);

                    header("Location: ".$_SERVER['HTTP_REFERER']);
                   } 

and the print_r of the array is

Array ( [4] => Array ( [product_name] => Pantofi Zara [coin] => RON [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 [0] => Array ( [4] => Array ( [product_name] => Pantofi Zara [coin] => RON [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 ) ) ) )

good
this is the code that's not working

if (isset($_GET['add_to_cart'])) {
                        $the_product_id = mysqli_real_escape_string($connection, $_GET['add_to_cart']);

                        $query = "SELECT * FROM products WHERE product_id = {$the_product_id} AND product_status = 'published'";
                        $select_product_for_cart = mysqli_query($connection, $query);

                        while ($row = mysqli_fetch_assoc($select_product_for_cart)) {
                            $product_id = $row['product_id'];
                            $product_name_ro = $row['product_name_ro'];
                            $product_name_en = $row['product_name_en'];
                            $product_name_fr = $row['product_name_fr'];
                            $product_price_ron = $row['product_price_ron'];
                            $product_price_dollar = $row['product_price_dolar'];
                            $product_price_euro = $row['product_price_euro'];

                           $size = "l";
                           $color = "red";
                           $quantity = 1;

                           if (isset($_GET['lang'])) {
                               $the_lang = mysqli_real_escape_string($connection, $_GET['lang']);

                                   if ($the_lang == 'ro') {
                                       $product_name = $product_name_ro;
                                   } else if ($the_lang == 'en') {
                                       $product_name = $product_name_en;
                                   } else if ($the_lang == 'fr') {
                                       $product_name = $product_name_fr;
                                   }
                           } else {
                               $product_name = $product_name_ro;
                           }

                           if (isset($_GET['coin'])) {
                               $the_coin = mysqli_real_escape_string($connection, $_GET['coin']);

                               if ($the_coin == 'ron') {
                                   $price = $product_price_ron;
                                   $currency = 'RON';
                               } else if ($the_coin == 'dollar') {
                                   $price = $product_price_dollar;
                                   $currency = '$';
                               } else if ($the_coin == 'euro') {
                                   $price = $product_price_euro;
                                   $currency = '€';
                               }
                           } else {
                               $price = $product_price_ron;
                               $currency = 'RON';
                           }
                        }
                        $total_price = $price * $quantity;

                        $_SESSION['cart'][$product_id] = array('product_name' => $product_name, 'coin' => $currency, 'price' => $price, 'total_price' => $total_price, 'size' => $size, 'color' => $color, 'quantity' => $quantity);

                        $temp = $_SESSION['cart'];
                        array_push($_SESSION['cart'][$product_id], $temp);

                        header("Location: ".$_SERVER['HTTP_REFERER']);  
                    }

and the print_r is

Array ( [4] => Array ( [product_name] => Pantofi Zara [coin] => [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 [0] => Array ( [4] => Array ( [product_name] => Pantofi Zara [coin] => [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 ) ) ) [3] => Array ( [product_name] => Pantofi Zara [coin] => [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 [0] => Array ( [4] => Array ( [product_name] => Pantofi Zara [coin] => [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 [0] => Array ( [4] => Array ( [product_name] => Pantofi Zara [coin] => [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 ) ) ) [3] => Array ( [product_name] => Pantofi Zara [coin] => [price] => 11 [total_price] => 11 [size] => l [color] => red [quantity] => 1 ) ) ) )

all this are for a single item
I can't figure it out any help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.