Adaugare o caracteristica

Discutii despre tot ce nu se incadreaza in celelalte categorii.

Moderators: Moderatori ajutatori, Moderatori

Post Reply
User avatar
sergiu4995
Membru, skill +4
Membru, skill +4
Posts: 1962
Joined: 08 Oct 2011, 20:05
Detinator Steam: Nu
Has thanked: 27 times
Been thanked: 13 times
Contact:

24 Feb 2020, 15:28

Incerc sa mai adaug o carateristica am mai dat in form la html o lista de selectare despre produs gen culori si cand adaug in cos in cosul de cumparaturi imi arata detaliile fiecarui produs am incercat sa mai adaug acea caracteristica dar nu merge,
Atasez mai jos codul car merge la monentul de fata

Pagina produsului
<form action="index.php?page=cart" method="post">
<input type="hidden" name="name" value="<?=$product['name']?>">

<input type="number" name="quantity" value="1" min="1" max="<?=$product['quantity']?>" placeholder="Quantity" required>
<input type="hidden" name="product_id" value="<?=$product['id']?>">
<select name="color">
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Yellow">Yellow</option>
</select>

<input type="submit" value="Add To Cart" name="submit">
</form>


Pagina cu cosul de cumparaturi


echo $color = $_POST['color'];

$tva=10.00;

if (isset($_POST['product_id'], $_POST['quantity'], $color) && is_numeric($_POST['product_id']) && is_numeric($_POST['quantity'])) {

$product_id = (int)$_POST['product_id'];
$quantity = (int)$_POST['quantity'];


$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
$stmt->execute([$_POST['product_id']]);

$product = $stmt->fetch(PDO::FETCH_ASSOC);

if ($product && $quantity > 0) {

if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
if (array_key_exists($product_id, $_SESSION['cart'])) {

$_SESSION['cart'][$product_id] += $quantity;

} else {

$_SESSION['cart'][$product_id] = $quantity;


}
} else {

$_SESSION['cart'] = array($product_id => $quantity); // aici am incercat sa mai adaug culoare gen => array('quantity' => $quantity, 'color' => $_POST['color']));

}
}
}

M-am incurcat...
Sigilat.ro
User avatar
The Kalu
eXtream Mod
eXtream Mod
Posts: 12363
Joined: 09 Oct 2010, 11:39
Detinator Steam: Da
Reputatie: Fost membru Club eXtreamCS (6 luni)
Nume anterior: Terra
Has thanked: 199 times
Been thanked: 339 times

29 Feb 2020, 19:51

Ma uit si revin eu.

Code: Select all

<?php



$tva=10.00;

if(isset($_POST['submit'])){

	$date_formular = array(
		'id_produs'		= filter_input(INPUT_POST, $_POST['product_id'], FILTER_VALIDATE_INT),
		'cantitate'		= filter_input(INPUT_POST, $_POST['quantity'], FILTER_VALIDATE_INT),
		'culoare'		= filter_input(INPUT_POST, $_POST['color'], FILTER_SANITIZE_STRING)
	);


	$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
	$stmt->execute([$date_formular['id_produs']]);

	$product = $stmt->fetch(PDO::FETCH_ASSOC);

	if ($product && $quantity > 0) {

		if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
			if (array_key_exists($date_formular['id_produs'], $_SESSION['cart'])) {

				$_SESSION['cart'][$date_formular['id_produs']] += $date_formular['cantitate'];
				$_SESSION['cart'][$date_formular['id_produs']] = $date_formular['culoare'];

			} else {

				$_SESSION['cart'][$date_formular['id_produs']] = $date_formular['cantitate'];

			}

		}
	}
}

?>
User avatar
sergiu4995
Membru, skill +4
Membru, skill +4
Posts: 1962
Joined: 08 Oct 2011, 20:05
Detinator Steam: Nu
Has thanked: 27 times
Been thanked: 13 times
Contact:

03 Mar 2020, 15:22

@the Kalu
Multumesc frumos
User avatar
The Kalu
eXtream Mod
eXtream Mod
Posts: 12363
Joined: 09 Oct 2010, 11:39
Detinator Steam: Da
Reputatie: Fost membru Club eXtreamCS (6 luni)
Nume anterior: Terra
Has thanked: 199 times
Been thanked: 339 times

03 Mar 2020, 15:26

Cu placere.
Post Reply

Return to “Discutii generale”

  • Information
  • Who is online

    Users browsing this forum: Bing [Bot], Google [Bot] and 202 guests