Я очень новичок в php и пытаюсь создать страницу корзины.
Iv получил идентификатор продукта, опцию и цену, отправленные с использованием массива $ _POST.
Я хочу, чтобы $ino
содержал значение Id, что я и делаю.
Я получаю одну повторяющуюся ошибку.
Примечание: неопределенный индекс: 3 в /home/sl0/s3196040/public_html/wp/a3/cart.php в строке 84 Имя: Примечание: неопределенный индекс: 3 в / home / sl0 / s3196040/public_html/wp/a3/cart.php в строке 71 Цена: Примечание: неопределенный индекс: 3 в /home/sl0/s3196040/public_html/wp/a3/cart.php в строке 74 **
Я знаю, что я должен "isset" $ ino, чтобы проверить, установлен он или нет, но я думал, что у меня есть?
Если я набираю значение вручную, например $pumps[3]['Title']
, оно работает, но не работает, если я использую переменную $ ino, как это $pumps[$ino]['Title']
.
Это в массиве $ _POST ([Id] => 3 [option] => english [qty] => 3
define("TITLE", "Product Page");
include_once('tools.php');
topModule('Business Name - Home', 'showSpecials()');
print_r($pumps[3]['Price']);
print_r($_POST);
// if i have the ID of the product, i need to turn it into a variables that the cart can display
if (isset ( $_POST ['Id'] )) {
// Check the item is not already in the cart
if (!in_array($_POST ["Id"], $_SESSION['cart'])) {
// Add new item to cart
$_SESSION ['cart'][] = $_POST["Id"];
}
}
else if (isset ( $_POST ['delete'] )) { // a remove button has been clicked
// Remove the item from the cart
if (false !== $key = array_search($_POST['delete'], $_SESSION['cart'])) {
unset($_SESSION['cart'][$key]);
}
// Empty Cart
else if (isset ( $_POST ["delete"] )) { // remove item from cart
unset ( $_SESSION ['cart'] );
}
}
if (isset ( $_SESSION ["cart"] )) {
if (isset($_POST['submit'])) {
$ino = $_POST['submit'];
}
?>
<form action='(omitted link)'
target='_blank' method='post'
enctype=''>
<table>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Action</th>
</tr>
<?php
// Set a default total
$total = 0;
foreach ( $_SESSION['cart'] as $ino ) {
?>
<tr>
<td>
Name: <?php echo $pumps[$ino]['Title']; ?> // error here
</td>
<td>
Price: <?php echo $pumps[$ino]['Price']; ?> // error here
</td>
<td>
Qty: <?php echo $_POST[$ino]['qty']; ?> // error here
</td>
<td>
<button type='submit' name='delete' value='<?php echo $ino; ?>'>Remove</button>
</td>
</tr>
<?php
$total += $pumps[$ino]['Price']; // error here
} // end foreach
?>
Total: $<?php echo $total; ?>
<tr>
<td colspan="2">Total: $<?php echo($total); ?></td>
<td><input type='submit' value='Checkout' /></td>
</tr>
<tr>
<td><button type='submit' name='clear'>Clear cart</button></td>
</tr>
</table>
</form>
<?php } ?>
<?php
endModule(); // Now a function call
?>
Содержимое моего $pumps
массива
Array
(
[1] => Array
(
[OID] => 01PRO
[Title] => ProphetX
[Description] => The Sequential Prophet X is a potent fusion of samples-plus-synthesis and is Dave Smith�s most ground-breaking evolution of the Prophet yet.
[Option] =>
[Price] => $4,500
[Image] =>
)
[2] => Array
(
[OID] => 03REV
[Title] => ProphetRev 2
[Description] => The Prophet Rev2 is Dave Smith�s reimagining of his Prophet �08 poly synth � a modern classic that has appeared on countless recordings and stages since its debut in 2007
[Option] =>
[Price] => $3,000
[Image] =>
)
[3] => Array
(
[OID] => 03REV
[Title] => Rev2 Desktop
[Description] => The Prophet Rev2 desktop module is just as powerful and easy to use as its counterpart, the Prophet Rev2 keyboard.
[Option] =>
[Price] => $2,500
[Image] =>
)
[4] => Array
(
[OID] => 04OB6
[Title] => OB-6
[Description] => The OB-6� is a once-in-a-lifetime collaboration between the two most influential designers in poly synth history, Dave Smith and Tom Oberheim.
[Option] =>
[Price] => $2,000
[Image] =>
)
[5] => Array
(
[OID] => 05OB6
[Title] => OB-6 Desktop
[Description] => The OB-6� desktop module is just as powerful and easy to use as its counterpart, the OB-6 Keyboard.
[Option] =>
[Price] => $1,500
[Image] =>
)
[6] => Array
(
[OID] => 06PR6
[Title] => Prophet 6
[Description] => The Prophet-6 is Dave Smith�s tribute to the poly synth that started it all�the Sequential Prophet-5. But it�s not simply a reissue of a classic. Rather, as Dave puts it, �It�s the result of our effort to build the most awesome-sounding, modern analog poly synth possible.
[Option] =>
[Price] => $3,500
[Image] =>
)
[7] => Array
(
[OID] => 07PR6
[Title] => Prophet 6 Desktop
[Description] => The Prophet-6 desktop module is every bit as powerful and easy to use as its counterpart, the Prophet-6 Keyboard. The module has all of the same controls as the keyboard version and provides the same immediacy and ease of use � with absolutely no menu diving. As with the Prophet-6 Keyboard, all parameters are at your fingertips, with full-sized knobs and switches and a comfortable, intuitive layout.
[Option] =>
[Price] => $2,000
[Image] =>
)
[8] => Array
(
[OID] => 08PR2
[Title] => Pro 2
[Description] => The Pro 2 is Dave Smith�s flagship mono synth, and as Dave himself puts it, his �most powerful mono synth ever.�
[Option] =>
[Price] => $4,000
[Image] =>
)
[9] => Array
(
[OID] => 09TEM
[Title] => Tempest
[Description] => Tempest is the brainchild of legendary instrument designers, Dave Smith and Roger Linn.
[Option] =>
[Price] => $3,000
[Image] =>
)
)
Содержимое $_SESSION['cart']
Array
(
[0] =>
[1] => 2
[2] => 9
)
Код грязный, но его просто для изучения, спасибо!